在R中,使用lubridate将hms对象转换为秒

时间:2022-10-04 14:56:35

simple question in lubridate--I want to convert an hms object into its appropriate number of seconds since the start of the day.

在lubridate中的简单问题 - 我想将hms对象转换为自当天开始以来的适当秒数。

For instance

library(lubridate)
hms("12:34:45")

then I want to know exactly how long 12 hours, 34 minutes, and 45 seconds is, in seconds

然后我想确切地知道12小时34分45秒是多长时间

something obvious like

一些明显的东西

seconds(hms("12:34:45"))

just returns

45s

which is not what I want. How do I convert these hms values into seconds? I'd like to use lubridate

这不是我想要的。如何将这些hms值转换为秒?我想用lubridate

2 个解决方案

#1


9  

It doesn't matter which package you use -- it will have convert a date / datetime object into a POSIXct representation of seconds since the epoch. So you may as well do it in base R -- so here deploy ISOdatetime() with an arbitrary day, using today:

使用哪个包并不重要 - 它会将日期/日期时间对象转换为自纪元以来秒数的POSIXct表示。所以你也可以在基础R中做 - 所以这里使用今天使用任意一天部署ISOdatetime():

R> difftime(ISOdatetime(2012,7,2,12,34,45), ISOdatetime(2012,7,2,0,0,0))
Time difference of 12.5792 hours

So we want seconds:

所以我们要秒:

R> difftime(ISOdatetime(2012,7,2,12,34,45), ISOdatetime(2012,7,2,0,0,0), 
+          unit="secs")
Time difference of 45285 secs

And we can cast to numbers:

我们可以投射到数字:

R> as.numeric(difftime(ISOdatetime(2012,7,2,12,34,45), +
                       ISOdatetime(2012,7,2,0,0,0), unit="secs"))
[1] 45285

Edit: And getting back to lubridate, this is arguably a bug:

编辑:回到lubridate,这可以说是一个错误:

> hms("12:34:45") - hms("00:00:00")
[1] 12 hours, 34 minutes and 45 seconds
R> as.numeric(hms("12:34:45") - hms("00:00:00"))
[1] 45
R>

#2


22  

R>lubridate::period_to_seconds(hms("01:00:00")) 

gives expected 3600 seconds as numeric counting from 00:00:00 or in the case above:

从00:00:00或在上面的情况下,预期3600秒为数字计数:

R>period_to_seconds(hms("12:34:45")) 

#1


9  

It doesn't matter which package you use -- it will have convert a date / datetime object into a POSIXct representation of seconds since the epoch. So you may as well do it in base R -- so here deploy ISOdatetime() with an arbitrary day, using today:

使用哪个包并不重要 - 它会将日期/日期时间对象转换为自纪元以来秒数的POSIXct表示。所以你也可以在基础R中做 - 所以这里使用今天使用任意一天部署ISOdatetime():

R> difftime(ISOdatetime(2012,7,2,12,34,45), ISOdatetime(2012,7,2,0,0,0))
Time difference of 12.5792 hours

So we want seconds:

所以我们要秒:

R> difftime(ISOdatetime(2012,7,2,12,34,45), ISOdatetime(2012,7,2,0,0,0), 
+          unit="secs")
Time difference of 45285 secs

And we can cast to numbers:

我们可以投射到数字:

R> as.numeric(difftime(ISOdatetime(2012,7,2,12,34,45), +
                       ISOdatetime(2012,7,2,0,0,0), unit="secs"))
[1] 45285

Edit: And getting back to lubridate, this is arguably a bug:

编辑:回到lubridate,这可以说是一个错误:

> hms("12:34:45") - hms("00:00:00")
[1] 12 hours, 34 minutes and 45 seconds
R> as.numeric(hms("12:34:45") - hms("00:00:00"))
[1] 45
R>

#2


22  

R>lubridate::period_to_seconds(hms("01:00:00")) 

gives expected 3600 seconds as numeric counting from 00:00:00 or in the case above:

从00:00:00或在上面的情况下,预期3600秒为数字计数:

R>period_to_seconds(hms("12:34:45"))