Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with make_datetime_100 #1680

Open
kar212 opened this issue Aug 16, 2024 · 0 comments
Open

Issue with make_datetime_100 #1680

kar212 opened this issue Aug 16, 2024 · 0 comments

Comments

@kar212
Copy link

kar212 commented Aug 16, 2024

The make_datetime_100 function in the lubridate chapter does not seem to be entirely accurate. The timezone information is neglected while using the make_datetime function and this leads to the default timezone (UTC) being adopted for all datetimes created using this function. This is not a problem for the examples thereafter, but if one were to compare datetimes from different timezones, especially by using the arr_time and the sched_arr_time, the results would not be correct.

For example, if we were to print the arrival time of all flights according to the departure time zone, using something like the following

flights_dt %>%

mutate(arr_time_in_deptz = with_tz(arr_time, dep_time %>% tz()))

, it would not give the exepected results as the timezones all match in the first place, they are all UTC.

Similarly, if we were to create graphs based on the arrival times rather than the departure times as shown in the examples, one needs to be very careful about the interpretation, as the destinations are all in different timezones. This could also be explained, or simply, the creation of the arr_time and the sched_arr_time removed from the flights_dt data frame to avoid incorrect usage.

The accurate solution is perhaps to join the flights with the airports table which has a timezone column.

use the timezone as string info. Note: a group_by(timezone) is needed with with_tz when using this function. This is because with_tz is not vectorised.

make_datetime_100_tzone <- function(year, month, day, time, timezone) {
make_datetime(year, month, day, time %/% 100, time %% 100, tz = timezone)
}

create a function for composing the date and time when the time zone offset is known

make_datetime_100_tz <- function(year, month, day, time, tzoffset) {
make_datetime(year, month, day, time %/% 100, time %% 100) + hours(tzoffset)
}

Again, this is not really straightforward, as there are limitations to using this in further examples involving the time zone. A problem is while trying to use with_tz later on this datetime; as with_tz is not vectorised, results will not be as expected. (group_by, rowwise or purr seem to be workarounds). Perhaps it is simply better to omit the arrival times from the flights_dt data frame and keep the examples simple, until support for timezones is improved.

Thanks for your attention!
KS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant