I have a vector of numerics representing the number of milliseconds since January 1, 1970. I would like to convert these into a date time object using lubridate
. A sample of data is below:
raw_times <- c(1139689917479, 1139667123031, 1140364113915, 1140364951003,
1139643685434, 1139677091970, 1139691963511, 1140339448413, 1140368308429,
1139686613641, 1139666081813, 1140351488730, 1140346617958, 1141933663183,
1141933207579, 1140360125149, 1140351845108, 1140365079103, 1141933549825,
1140365601476)
Knowing that the documentation for as_date
and as_datetime
indicate they take a numeric vector representing the number of days since January 1, 1970 , I tried the following:
library(lubridate)
as_date(raw_times / (1000 * 60 * 60 * 24))
"2006-02-11" "2006-02-11" "2006-02-19" "2006-02-19" "2006-02-11"
"2006-02-11" "2006-02-11" "2006-02-19" "2006-02-19" "2006-02-11"
"2006-02-11" "2006-02-19" "2006-02-19" "2006-03-09" "2006-03-09"
"2006-02-19" "2006-02-19" "2006-02-19" "2006-03-09" "2006-02-19"
(Obviously using the fact that there are 1000ms in a second, 60 seconds in a minute, 60 minutes in an hour, 24 hours in a day.)
When I run the same the code with as_datetime
, I get the following:
as_datetime(raw_times / (1000 * 60 * 60 * 24))
"1970-01-01 03:39:50 UTC" "1970-01-01 03:39:50 UTC" "1970-01-01 03:39:58 UTC" "1970-01-01 03:39:58 UTC" "1970-01-01 03:39:50 UTC" "1970-01-01 03:39:50 UTC"
"1970-01-01 03:39:50 UTC" "1970-01-01 03:39:58 UTC" "1970-01-01 03:39:58 UTC" "1970-01-01 03:39:50 UTC" "1970-01-01 03:39:50 UTC" "1970-01-01 03:39:58 UTC"
"1970-01-01 03:39:58 UTC" "1970-01-01 03:40:16 UTC" "1970-01-01 03:40:16 UTC" "1970-01-01 03:39:58 UTC" "1970-01-01 03:39:58 UTC" "1970-01-01 03:39:58 UTC"
"1970-01-01 03:40:16 UTC" "1970-01-01 03:39:58 UTC"
The results are different. I would assume there is some other argument which I am missing, but I can't find anything in the documentation which would tell me what that would be.
Session information below:
> sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] lubridate_1.6.0
loaded via a namespace (and not attached):
[1] magrittr_1.5 tools_3.3.2 stringi_1.1.2 stringr_1.1.0
See Question&Answers more detail:
os