rba*_*att 5 timezone posix r strptime
对于输出,规格是%Z(见?strptime).但是对于输入,这是如何工作的?
为了澄清,将时区缩写解析成有用的信息会很好as.POSIXct(),但更多的核心问题是如何让函数至少忽略时区.
这是我最好的解决方法,但是有一个特定的格式代码可以传递给as.POSIXct()所有时区吗?
times <- c("Fri Jul 03 00:15:00 EDT 2015", "Fri Jul 03 00:15:00 GMT 2015")
as.POSIXct(times, format="%a %b %d %H:%M:%S %Z %Y") # nope! strptime can't handle %Z in input
formats <- paste("%a %b %d %H:%M:%S", gsub(".+ ([A-Z]{3}) [0-9]{4}$", "\\1", times),"%Y")
as.POSIXct(times, format=formats) # works
Run Code Online (Sandbox Code Playgroud)
编辑:这是最后一行的输出,以及它的类(来自单独的调用); 输出符合预期.从控制台:
> as.POSIXct(times, format=formats)
[1] "2015-07-03 00:15:00 EDT" "2015-07-03 00:15:00 EDT"
> attributes(as.POSIXct(times, format=formats))
$class
[1] "POSIXct" "POSIXt"
$tzone
[1] ""
Run Code Online (Sandbox Code Playgroud)
简短的回答是:“不,你不能。” 这些都是缩写,不能保证唯一地标识特定时区。
例如,“EST”是美国还是澳大利亚的东部标准时间?“CST”是美国或澳大利亚的中部标准时间,还是中国标准时间,还是古巴标准时间?
我只是注意到您并不是试图解析时区缩写,您只是想避免它。我不知道有什么方法可以告诉strptime忽略任意字符。我确实知道它会忽略格式字符串结束后时间的字符表示中的任何内容。例如:
R> # The year is not parsed, so the current year is used
R> as.POSIXct(times, format="%a %b %d %H:%M:%S")
[1] "2015-07-03 00:15:00 UTC" "2015-07-03 00:15:00 UTC"
Run Code Online (Sandbox Code Playgroud)
除此之外,正则表达式是我能想到的唯一可以解决这个问题的方法。与您的示例不同,我将在输入字符向量上使用正则表达式来删除所有 3-5 个字符的时区缩写。
R> times_no_tz <- gsub(" [[:upper:]]{3,5} ", " ", times)
R> as.POSIXct(times_no_tz, format="%a %b %d %H:%M:%S %Y")
[1] "2015-07-03 00:15:00 UTC" "2015-07-03 00:15:00 UTC"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
483 次 |
| 最近记录: |