poissonconsulting / dttr2

An R package to manipulate dates, times and date times
https://poissonconsulting.github.io/dttr2/
Other
10 stars 2 forks source link

unexpected behaviour converting PST8PDT to Etc/GMT+8 #28

Open sebdalgarno opened 1 year ago

sebdalgarno commented 1 year ago

I ran into this issue while converting a real world discharge data set that was provided in PST8PDT (i.e. two 1:00:00 entries on November 07 2021). I had to manually change the second 1:00:00 entry prior to adjusting tz, which doesn't seem right...

# a date time on the eve of end of daylight savings time
x <- dtt_date_time("2021-11-06 23:00:00", tz = "PST8PDT")

# dttr2 (magically) does this correctly - assigns the first 1:00 to PDT and the second 1:00 to PST
x_dtt <- reduce(map(1:4, function(y) dtt_add_hours(x, y)), c)
x_dtt

# note this is better behaviour than lubridate so far!

# we can then successfully convert to GMT and keep the sequence
dtt_adjust_tz(x_dtt, "ETC/GMT+8")

# but when we set the tz on a vector it assigns both 1:00 to PDT
x <- c("2021-11-06 00:00:00", "2021-11-07 1:00:00", "2021-11-07 1:00:00", "2021-11-07 2:00:00")
x_pst <- dtt_date_time(x, tz = "PST8PDT")
x_pst
# same like this
x_pst <- dtt_date_time(x) |> dtt_set_tz("PST8PDT")
x_pst

# which causes this to fail (i.e. should be a continuous sequence)
dtt_adjust_tz(x_pst, tz = "ETC/GMT+8")

# to resolve we have to add an hour to the second 1:00
x_pst[3] <- dtt_add_hours(x_pst[3], 1)
dtt_adjust_tz(x_pst, tz = "ETC/GMT+8")
aylapear commented 11 months ago

This behaviour does match the intention of the function since a vector of strings is passed there is no way to tell whether the value should be PST or PDT if the values are independent (not a series). This means that the PST or PDT needs to be assigned consistently to string values and it appears that "2021-11-07 1:00:00" is coded with PDT and "2021-11-07 2:00:00" is coded with PST. This is unfortunately a limitation but is unavoidable when converting a string based on how time/timezones/daylight savings works.

This scenario can be common with various types of data and so users need to be aware of this issue.

aylapear commented 11 months ago

Potential work arounds could be

1) a function that can detect when this occurs in a data set so a person can manually make the change and confirm it was done correctly 2) a function that can identify and make the correction