as.POSIXct produces 'unknown timezone' error after Mavericks and R upgrade in Mac OSX

故事扮演 提交于 2019-12-12 02:26:37

问题


I'm trying to convert a string to POSIXct in R v3.1.1 on Mac OS X Mavericks (10.9.4). This worked before upgrading Mavericks then R. My very simple code is now giving a warning and I don't understand why :-

as.POSIXct("2014-05-24 12:45", "%Y-%m-%d %hh:%mm")
[1] "2014-05-24 12:45:00 GMT"

Warning messages:
1: In strptime(xx, f <- "%Y-%m-%d %H:%M:%OS", tz = tz) :
  unknown timezone '%Y-%m-%d %hh:%mm'
2: In as.POSIXct.POSIXlt(x) : unknown timezone '%Y-%m-%d %hh:%mm'
3: In strptime(xx, f <- "%Y/%m/%d %H:%M:%OS", tz = tz) :
  unknown timezone '%Y-%m-%d %hh:%mm'
4: In as.POSIXct.POSIXlt(x) : unknown timezone '%Y-%m-%d %hh:%mm'
5: In strptime(xx, f <- "%Y-%m-%d %H:%M", tz = tz) :
  unknown timezone '%Y-%m-%d %hh:%mm'
6: In as.POSIXct.POSIXlt(x) : unknown timezone '%Y-%m-%d %hh:%mm'
7: In strptime(x, f, tz = tz) : unknown timezone '%Y-%m-%d %hh:%mm'
8: In as.POSIXct.POSIXlt(as.POSIXlt(x, tz, ...), tz, ...) :
  unknown timezone '%Y-%m-%d %hh:%mm'
9: In as.POSIXlt.POSIXct(x, tz) : unknown timezone '%Y-%m-%d %hh:%mm'

I tried defining the timezone using the following but got an NA returned :-

as.POSIXct("2014-05-24 12:45", "%Y-%m-%d %hh:%mm", tz="Europe/London")

[1] NA

Not sure what I'm doing wrong here.

Thanks in advance for any help.


回答1:


First of all, If you''ll look into the ?strptime documentation, you will see that %h is for month abbreviation and similarly %m is for month decimal number. For hours and minutes you should use %H and %M

Second of all, if you'll type as.POSIXct in console, you'll see that it's second parameter is tz rather than format, thus you need to specify format = when passing a format argument to it. Other wise you are passing it to tz by default

as.POSIXct
# function (x, tz = "", ...) 
# UseMethod("as.POSIXct")
# <bytecode: 0x0000000008ee6000>
# <environment: namespace:base>

Thus the solution would be

as.POSIXct("2014-05-24 12:45", format = "%Y-%m-%d %H:%M")
## [1] "2014-05-24 12:45:00 IDT"
as.POSIXct("2014-05-24 12:45", "%Y-%m-%d %H:%M", tz = "Europe/London")
## [1] "2014-05-24 12:45:00 BST"


来源:https://stackoverflow.com/questions/25749040/as-posixct-produces-unknown-timezone-error-after-mavericks-and-r-upgrade-in-ma

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