timezones in R: how to avoid ambiguous terms such as EST? - r

I have a series of character timestamps in R. When I change their class to POSIXct using intuitive methods, R assigns the ambiguous timezone EST.
For example:
as.POSIXct("2012-08-06 15:32:00")
as.POSIXct("2012-08-06 15:32:00", tz = "Australia/Brisbane")
as.POSIXct("2012-08-06 15:32:00", tz = "")
all produce the same output on my two (Mac and Windows) boxes:
"2012-08-06 15:32:00 EST"
The problem here is EST could be any number of timezones: Eastern Standard Time in the USA, or Australian Eastern Standard Time, or another timezone in Canada (from ?timezone):
Beware that some of these designations may not be what you think: in
particular EST is a time zone used in Canada without daylight savings
time, and not EST5EDT nor (Australian) Eastern Standard Time.
There is a method to set the timezone which avoids this EST label. It is alluded to, but not fully explained in the R ?timezone help. Setting x as the time of the Curiosity landing on Mars as reported by an Australian news service:
x <- as.POSIXct("2012-08-06 15:32:00", tz = "Etc/GMT-10")
x
"2012-08-06 15:32:00 GMT-10"
And we can test that this is correct by converting it to a US timezone and checking with a Californian news report:
y <- format(x, tz = "America/Los_Angeles")
y
"2012-08-05 22:32:00"
If using this Etc/GMT+n or Etc/GMT-n notation, please beware of the following caveat from ?timezone :
Many systems support timezones of the form GMT+n and GMT-n, which are
at a fixed offset from UTC (hence no DST). Contrary to some usage (but
consistent with names such as PST8PDT), negative offsets are times
ahead of (east of) UTC, positive offsets are times behind (west of)
UTC.

The 1st and 3rd lines in your first example produce the same output because tz="" is the default for as.POSIXct. The second line is more interesting because the timezone is explicitly defined.
But note that "EST" is only how the timezone is printed by default. The tzone attribute is still unambiguous.
R> x <- as.POSIXct("2012-08-06 15:32:00", tz="Australia/Brisbane")
R> x
[1] "2012-08-06 15:32:00 EST"
R> attr(x, "tzone")
[1] "Australia/Brisbane"

Related

Convert time zone from EST to UTC using R base functions

I need to change my time zone from EST to UTC using the base R functions. I wrote this code but the problem is that although during the daylight saving time the time difference between these two time zones should be 4 hours, the function does not take it into account. Therefore, during the whole year the time difference equals to 5. My dataset is here.
data <- read.csv("data.csv")
data1 <- data
attributes(data1$time)$tzone
data1$time <- as.POSIXct(data1$time, format="%m/%d/%Y %H:%M:%S", tz="EST")
attributes(data1$time)$tzone
attr(data1$time, "tzone") <- "UTC"
attributes(data1$time)$tzone
Using tz = "EST" means you are specifying Eastern standard time, which is only active in autumn/winter. As indicated by #r2evans, tz = "US/Eastern" or tz = "America/New_York" specifies that the time should switch between EST and EDT (Eastern daylight time) on the appropriate dates.

Switch between dates and seconds in a well defined manner

I run discrete event simulations where the time originates from dates. I think that simulations run much faster, when I convert all the dates to integers (relative time in seconds).
What is the best way, to switch between date and seconds in a well definied way where I want to
set the reference time (e.g. "1970-01-01 00:00:00 GMT" or "2016-01-01 00:00:00 GMT") manually,
the time zone and
the origin (Not possible in lubridate?)
I thought I can use the origin for this purpose but it does not influence the result:
> as.numeric(as.POSIXct("2016-01-01 00:00:00 GMT",origin="2016-01-01",tz="GMT"))
> as.numeric(as.POSIXct("2016-01-01 00:00:00 GMT",origin="1970-01-01",tz="GMT"))
both result in [1] 1451606400.
(Only the tz argument changes the result, which is ok of course:
> as.numeric(as.POSIXct("2016-01-01 00:00:00 CEST", tz= "America/Chicago"))
[1] 1451628000)
You can use difftime() to calculate the difference between some timestamp and a reference time:
as.numeric(difftime(as.POSIXct("2016-01-01 00:00:00",tz="GMT"),
as.POSIXct("1970-01-01 00:00:00",tz="GMT"), units = "secs"))
## [1] 1451606400
By choosing another value for units, you could also get the number of minutes, hours, etc.
The reason that you get the same result for both choices of origin is that this argument is only intended to be used when converting a number into a date. Then, the number is interpreted as seconds since the origin that you pass to the function.
Internally, a POSIXct object is always stored as seconds since 1970-01-01 00:00:00, UTC, independent of the origin that you specified when doing the conversion. And accordingly, converting to numeric gives the same result for any choice of origin.
You can have a look at the documentation of as.POSIXct():
## S3 method for class 'character'
as.POSIXlt(x, tz = "", format, ...)
## S3 method for class 'numeric'
as.POSIXlt(x, tz = "", origin, ...)
As you can see, origin is only an argument for the method for numeric, but not for character.

R: Posix (Unix) Time Crazy Conversion

Unix time is 1435617000.
as.Date(1435617000,origin="01-01-1970")
[1] "3930586-11-23"
Which is wrong. I'm trying to (a) get the correct date, which, per epoch converter is GMT: Mon, 29 Jun 2015 22:30:00 GMT.
How do I get R to tell me the month, day, year, hour, minute & second? Thank you.
I think the reason why that happen is because as.Date converts arguments to class date objects. In this case you do not need a date but a class POSIXct object because your input, the x vector, contains other informations that as.Date is not able to manage. Another problem that even with the right function could appear, is that if when you do not specify the right time zone with the tz argument (except the case where your time zone is the same as the original time).
The following code does the job.
x <- 1435617000
as.POSIXct(x, origin = "1970-01-01", tz ="GMT")
[1] "2015-06-29 22:30:00 GMT"
Use as.Date
Just in the case you wanted only the date but you have a complete Unix time like x, you have to just divide by 86400 (which is the number of seconds in a day!) to get only the right date.
as.Date(x/86400L, origin = "1970-01-01")
[1] "2015-06-29"
Another important detail
The origin argument has to be supplied with YYYY-MM-DD and not like you did DD-MM-YYYY I am not sure but I think that the former is the only accepted and correct way.

as.POSIXct assigns different timezones [duplicate]

I am trying to convert UTC time to local standard time. I have found many functions which convert to Local Daylight Time, but I was not successful in getting the standard time. Right now, I have the following code which converts to local daylight time at my specific timezone:
pb.date <- as.POSIXct(date,tz="UTC")
format(pb.date, tz="timeZone",usetz=TRUE)
I would appreciate any help.
First, POSIXct date-times are always UCT internally. The print.POSIXt and format.POSIXt methods will appropriately make the TZ shift on output from their internal representations:
pb.date <- as.POSIXct(Sys.Date())
Sys.Date()
#[1] "2015-07-09"
So that was midnight of the current date in Greenwich:
format(pb.date, tz="America/Los_Angeles",usetz=TRUE)
#[1] "2015-07-08 17:00:00 PDT"
When it's midnight in Greenwich, it's 5PM Daylight Time in the previous day on the Left Coast of the US. You need to use the correct character values for your TZ (and your OS) both of which at the moment are unspecified.
The US Pacific timezone is 8 hours behind GMT (in winter months) so you can use a timezone that is Standard/Daylight-agnostic:
> format(pb.date,usetz=TRUE, tz="Etc/GMT+8")
[1] "2015-07-08 16:00:00 GMT+8"
(Notice the reversal of + with "behind" and - with "ahead".)
I know this question has an accepted answer, but in case anyone comes along and this can help. I needed a function to convert UTC times to MTN time (Server is in UTC, we operate in MTN).
Not sure why, but needed to force it to UTC/GMT first and the convert it to MTN. However it does work
mtn_ts = function(utcTime){
library(lubridate)
toTz = "us/mountain"
utcTime = force_tz(utcTime,tzone= 'GMT')
dt = as.POSIXct(format(utcTime,tz = toTz,origin ='GMT', usetz=TRUE))
dt = force_tz(dt,tzone= toTz)
return(dt)
}
mtn_ts(as.POSIXct("2021-09-27 14:48:51.000000000"))

Converting UTC time to local standard time in R

I am trying to convert UTC time to local standard time. I have found many functions which convert to Local Daylight Time, but I was not successful in getting the standard time. Right now, I have the following code which converts to local daylight time at my specific timezone:
pb.date <- as.POSIXct(date,tz="UTC")
format(pb.date, tz="timeZone",usetz=TRUE)
I would appreciate any help.
First, POSIXct date-times are always UCT internally. The print.POSIXt and format.POSIXt methods will appropriately make the TZ shift on output from their internal representations:
pb.date <- as.POSIXct(Sys.Date())
Sys.Date()
#[1] "2015-07-09"
So that was midnight of the current date in Greenwich:
format(pb.date, tz="America/Los_Angeles",usetz=TRUE)
#[1] "2015-07-08 17:00:00 PDT"
When it's midnight in Greenwich, it's 5PM Daylight Time in the previous day on the Left Coast of the US. You need to use the correct character values for your TZ (and your OS) both of which at the moment are unspecified.
The US Pacific timezone is 8 hours behind GMT (in winter months) so you can use a timezone that is Standard/Daylight-agnostic:
> format(pb.date,usetz=TRUE, tz="Etc/GMT+8")
[1] "2015-07-08 16:00:00 GMT+8"
(Notice the reversal of + with "behind" and - with "ahead".)
I know this question has an accepted answer, but in case anyone comes along and this can help. I needed a function to convert UTC times to MTN time (Server is in UTC, we operate in MTN).
Not sure why, but needed to force it to UTC/GMT first and the convert it to MTN. However it does work
mtn_ts = function(utcTime){
library(lubridate)
toTz = "us/mountain"
utcTime = force_tz(utcTime,tzone= 'GMT')
dt = as.POSIXct(format(utcTime,tz = toTz,origin ='GMT', usetz=TRUE))
dt = force_tz(dt,tzone= toTz)
return(dt)
}
mtn_ts(as.POSIXct("2021-09-27 14:48:51.000000000"))

Resources