[Rd] Date time conversion bug (as.POSIXct)?
Simon Urbanek
simon.urbanek at r-project.org
Fri Jan 11 19:27:15 CET 2013
On Jan 11, 2013, at 5:12 AM, Dries Verheyen wrote:
> There is something wrong, I think, with the date-time conversion from a
> numeric value if you use Central European Time (CET) as timezone.
> Examples from R:
>
> If I use the GMT time zone it is OK, I get the same time back from
> as.POSIXct as I entered
>
>> as.POSIXct(as.numeric(strptime("30/01/2012 13:00:00", format="%d/%m/%Y
>> %H:%M:%S",tz="GMT")),origin="1970-01-01",tz="GMT")
>
> [1] "2012-01-30 13:00:00 GMT"
>
> If I go from CET to GMT there is also no problem:
>
>> as.POSIXct(as.numeric(strptime("30/01/2012 13:00:00", format="%d/%m/%Y
> %H:%M:%S",tz="CET")),origin="1970-01-01",tz="GMT")
>
> [1] "2012-01-30 12:00:00 GMT"
>
> In winter we are 1 hour ahead of Greenwich.
>
> Also in summer it is OK (2 hours ahead of Greenwich):
>
>> as.POSIXct(as.numeric(strptime("30/05/2012 13:00:00", format="%d/%m/%Y
> %H:%M:%S",tz="CET")),origin="1970-01-01",tz="GMT")
>
> [1] "2012-05-30 11:00:00 GMT"
>
> The problem occurs when I use both the same time zone, if it is not GMT:
>
>> as.POSIXct(as.numeric(strptime("30/01/2012 13:00:00", format="%d/%m/%Y
> %H:%M:%S",tz="CET")),origin="1970-01-01",tz="CET")
>
> [1] "2012-01-30 12:00:00 CET"
>
>> as.POSIXct(as.numeric(strptime("30/05/2012 13:00:00", format="%d/%m/%Y
> %H:%M:%S",tz="CET")),origin="1970-01-01",tz="CET")
>
> [1] "2012-05-30 12:00:00 CEST"
>
>> as.POSIXct(as.numeric(strptime("30/05/2012 13:00:00", format="%d/%m/%Y
>> %H:%M:%S",tz="EST")),origin="1970-01-01",tz="EST")
> [1] "2012-05-30 18:00:00 EST"
>
> It looks like the function just puts the name of the time zone behind the
> time calculated in GMT, and does not calculate the time in the asked time
> zone.
It works perfectly fine the way it's supposed to - you just give it wrong input. What you get from as.numeric(...) is the offset from the epoch (1970-01-01 GMT!) yet you are then forcing as.POSIXct to interpret it as offset from the local time zone which is obviously wrong. I suppose what you really wanted is
> .POSIXct(as.numeric(strptime("30/05/2012 13:00:00", format="%d/%m/%Y%H:%M:%S",tz="EST")),"EST")
[1] "2012-05-30 13:00:00 EST"
The problem in your case is that you want to have origin in a different timezone than the result which is a bit convoluted.
Cheers,
Simon
> Is it just how the function works? because for me it is a bit confusing like
> that.
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Date-time-conversion-bug-as-POSIXct-tp4655215.html
> Sent from the R devel mailing list archive at Nabble.com.
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>
More information about the R-devel
mailing list