I've run into a problem with managing time zones with POSIXct in R. I have set the TZ
option globally as "Europe/London"
but since we have switched back to GMT have run as.POSIXct
no longer converts the numeric vector back to the right time.
Digging into why I found that differences in time zone can be caused by the object type used to set the origin date.
For example:
# Date time is set as 1 second after 1970-01-01
as.POSIXct(1, origin = "1970-01-01")
# [1] "1970-01-01 01:00:01 BST"
# Same numeric value, but one hour less now that the origin is set using a POSIXct
as.POSIXct(1, origin = as.POSIXct("1970-01-01"))
# [1] "1970-01-01 00:00:01 BST"
The first value doesn't really make sense given that the query was taken outside of British summer time, yet these were taken in GMT (see results below):
Sys.timezone()
# [1] "Europe/London"
Sys.time()
# [1] "2018-10-31 11:05:36 GMT"
Even when you explicitly state the time zone at each stage, the hour difference still persists:
as.POSIXct(1, origin = "1970-01-01", tz = "Europe/London")
# [1] "1970-01-01 01:00:01 BST"
as.POSIXct(1, origin = as.POSIXct("1970-01-01", tz = "Europe/London"), "Europe/London")
# [1] "1970-01-01 00:00:01 BST"
To make matters worse the documentation resulting from ?as.POSIXct
is pretty vague about the management of time zones, specifically:
If a time zone is needed and that specified is invalid on your system, what happens is system-specific but attempts to set it will probably be ignored.
Given this, I have a series of questions:
1) Why does as.POSIXct(1, origin = "1970-01-01", tz = "Europe/London")
add an hour? Even when the origin date would be parsed as a GMT time and the time zone has been set explicitly.
2) What is the best method of ensuring that you time zone in R is consistent when converting from numeric in R?
3) What is the best practice for managing time zones in R? Is there a good reference, especially for POSIXct
date types.