将POSIX日期和时间(posixct)舍入到相对于时区的日期 [英] Round a POSIX date and time (posixct) to a date relative to a timezone
问题描述
如果我尝试
我想要一个POSIXct,直到当天的相对于一个特定的时区。
round(as.POSIXct(2013-03-05 23:00:00 EST),day)
它返回
2013-03-06
这是有道理的,因为在EST5EDT的2013-03-05 EST为23:00:00时,它是已经2013-03-06在UTC。在逻辑上,我想做的是:
round(as.POSIXct(2013-03-05 23:00:00 EST),day,tz =EST5EDT)
就是说,时间到最近的一天,相对于EST5EDT时区。不幸的是,不采用时区参数。
圆的
将一直到第二天一旦中午过去,这就是为什么我认为你看到2013-03-06。我还必须在调用 as.POSIXct
中显式设置
tz
参数观察:
round(as.POSIXct(2013-03-05 11:00:00 ,tz =EST),day)
[1]2013-03-05 EST
然后一旦中午通过:
round(as.POSIXct(2013-03-05 12:00,tz =EST),day)
[1]2013-03-06 EST
调用格式
将日期提取为没有tz参数的字符串。所以你可以得到你的原始结果没有时区
格式(圆(as.POSIXct(2013-03-05 12: 00:00,tz =EST),day))
[1]2013-03-06
如果您希望在当天的任何时间回到,或许你想要的是 trunc
?
格式(trunc(as.POSIXct(2013-03-05 12:00:00,tz =EST ),day))
[1]2013-03-05
I want to round a POSIXct down to the day, relative to a specific timezone.
If I try
round(as.POSIXct("2013-03-05 23:00:00 EST"), "day")
It returns
2013-03-06
Which makes sense, in that when it's 23:00:00 EST on 2013-03-05 in EST5EDT, it's already 2013-03-06 in UTC. Logically, what I want to do is:
round(as.POSIXct("2013-03-05 23:00:00 EST"), "day", tz="EST5EDT")
That is, "round this date and time to the nearest day, relative to the EST5EDT time zone". Unfortunately, round doesn't take a time zone parameter.
round
will round to the next day once it's past midday, which is why I think you are seeing 2013-03-06. I also have to explicitly set the tz
argument in the call to as.POSIXct
Observe:
round( as.POSIXct("2013-03-05 11:00:00" , tz = "EST" ), "day" )
[1] "2013-03-05 EST"
And then once it passes noon:
round( as.POSIXct("2013-03-05 12:00:00" , tz = "EST" ), "day" )
[1] "2013-03-06 EST"
A call to format
extracts the day as a character string without the tz argument. So you can get your original result without the timezone
format( round( as.POSIXct("2013-03-05 12:00:00" , tz = "EST" ), "day" ) )
[1] "2013-03-06"
If you want to round any time on that day to that day perhaps what you want instead is trunc
?
format(trunc( as.POSIXct("2013-03-05 12:00:00" , tz = "EST" ), "day" ))
[1] "2013-03-05"
这篇关于将POSIX日期和时间(posixct)舍入到相对于时区的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!