Reputation: 81
There is a post that shows how use lubridate
to go from time to seconds since midnight. How about going from seconds since midnight to time of day?
So, instead of using 06:52:32 to get 24752.05, how do I use 24752.05 to get 06:52:32?
Upvotes: 5
Views: 6203
Reputation: 368409
Use as.POSIXct()
on the sum of the midnight timestamp and your offset, possibly adjusting by setting TZ.
In my default time zone:
R> as.POSIXct(trunc(Sys.time(), units="days") + 24752.05)
[1] "2013-01-16 06:52:32.04 CST"
R>
Note that the answer (just like the preceding question) can be solved just fine in base R, persistent rumors that every time question has to involve lubridate not withstanding.
Upvotes: 9
Reputation: 42669
And of course, you can compute the hours, minutes and seconds directly. This is based on the definition of POSIX time. Midnight always has a time_t
value that is divisible by 86400
.
x <- 24752.05
paste(sprintf("%02d", floor(x / 3600)),
sprintf("%02d", floor(x %% 3600 / 60)),
sprintf("%02d", floor(x %% 60 )),
sep=":"
)
## [1] "06:52:32"
Upvotes: 3
Reputation: 44614
Another option:
format(as.POSIXct('1900-1-1') + 24752.05, '%H:%M:%S')
# [1] "6:52:32"
Upvotes: 4
Reputation: 43265
Using lubridate
just for variety:
library(lubridate)
Sys.time() + seconds(24752.05)
or from midnight:
as.Date(Sys.time()) + seconds(24752.05)
Upvotes: 3