Strange strptime behavior in R

不想你离开。 提交于 2019-12-31 03:05:18

问题


I experience very strange behaviour of strptime parsing.

This is my R session on windows machine

> R.Version()$version.string
[1] "R version 2.15.2 (2012-10-26)"
> a <- ( strptime(  "29-MAR-13 02.26.53.000000000 AM" , "%d-%B-%y %I.%M.%OS %p" ) )
> str(a)
 POSIXlt[1:1], format: "2013-03-29 02:26:53"
> # convert to double: doesn't work
> as.double(a)
[1] NA
> b <- ( strptime(  "29-MAR-13 04.26.53.000000000 AM" , "%d-%B-%y %I.%M.%OS %p" ) )
> #same as `a`, but another hour
> str(b)
 POSIXlt[1:1], format: "2013-03-29 04:26:53"
> #convert to double: works
> as.double(b)
[1] 1364520413
>  #WTF ???

I have tested the same code on another machine (linux) with different minor version of R. The code worked as expected in both cases:

> R.Version()$version.string
[1] "R version 2.15.1 (2012-06-22)"
> a <- ( strptime(  "29-MAR-13 02.26.53.000000000 AM" , "%d-%B-%y %I.%M.%OS %p" ) )
> str(a)
 POSIXlt[1:1], format: "2013-03-29 02:26:53"
> # convert to double: WORKS!
>  as.double(a)
[1] 1364513213
> b <- ( strptime(  "29-MAR-13 04.26.53.000000000 AM" , "%d-%B-%y %I.%M.%OS %p" ) )
> #same as `a`, but another hour
> str(b)
 POSIXlt[1:1], format: "2013-03-29 04:26:53"
>  #convert to double: works
> as.double(b)
[1] 1364520413
>

Can someone tell me what's the problem? Did I find a bug in R?


回答1:


Are you in Israel? Googling 'daylight savings time "march 29"' shows that in 2013 DST started on March 29 in Israel, e.g. http://www.timeanddate.com/worldclock/clockchange.html?n=110 ... differences in locale/time zone settings between the two computers you tested could certainly explain this, if one were set to an Israeli time zone and the other weren't. You can check this by using tz="GMT" ...

PS: I looked at your profile and see that you are indeed in Israel. I didn't look before I initially posted the answer!

PPS: A very large fraction of queries about date-time conversion turn out to be about timezone, and specifically daylight-savings-time, issues, which is what led me to the answer in the first place.



来源:https://stackoverflow.com/questions/17944230/strange-strptime-behavior-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!