时间戳属于哪个3小时时间间隔
Which 3 hour time interval does time stamp belong to
我想判断时间戳属于哪个3小时区间
第一个间隔范围从 00:00:01
到 03:00:00
,第二个间隔范围从 03:00:01
到 6:00:00
... 输入格式为 POSIXct
。
例如:
输入:time<-Sys.time() #"2019-08-23 03:27:20 CEST"
输出:2
(最好是最快的方法)
我们可以通过在 3 小时的序列中定义 breaks
然后将其转换为整数来使用 cut
。
as.integer(cut(time, breaks = seq(as.POSIXct("00:00:00", format = "%T"),
as.POSIXct("23:59:59", format = "%T"), by = "3 hours")))
与findInterval
相同
findInterval(time, seq(as.POSIXct("00:00:00", format = "%T"),
as.POSIXct("23:59:59", format = "%T"), by = "3 hours"))
对于共享的更新数据,我们可以做
x <-structure(-62167133512, class = c("POSIXct", "POSIXt"), tzone = "UTC")
as.integer(cut(as.POSIXct(format(x, "%T"), format = "%T"),
breaks = seq(as.POSIXct(paste(Sys.Date(), "00:00:00")),
as.POSIXct(paste(Sys.Date() + 1, "00:00:00")), by = "3 hours")))
#[1] 8
我想判断时间戳属于哪个3小时区间
第一个间隔范围从 00:00:01
到 03:00:00
,第二个间隔范围从 03:00:01
到 6:00:00
... 输入格式为 POSIXct
。
例如:
输入:time<-Sys.time() #"2019-08-23 03:27:20 CEST"
输出:2
(最好是最快的方法)
我们可以通过在 3 小时的序列中定义 breaks
然后将其转换为整数来使用 cut
。
as.integer(cut(time, breaks = seq(as.POSIXct("00:00:00", format = "%T"),
as.POSIXct("23:59:59", format = "%T"), by = "3 hours")))
与findInterval
findInterval(time, seq(as.POSIXct("00:00:00", format = "%T"),
as.POSIXct("23:59:59", format = "%T"), by = "3 hours"))
对于共享的更新数据,我们可以做
x <-structure(-62167133512, class = c("POSIXct", "POSIXt"), tzone = "UTC")
as.integer(cut(as.POSIXct(format(x, "%T"), format = "%T"),
breaks = seq(as.POSIXct(paste(Sys.Date(), "00:00:00")),
as.POSIXct(paste(Sys.Date() + 1, "00:00:00")), by = "3 hours")))
#[1] 8