简体   繁体   English

如何使用 lubridate 将日期转换为日期时间到 R 中的 UNIX 纪元以来的秒数?

[英]How to convert date to datetime to seconds since UNIX epoch in R with lubridate?

I'm noticing this very confusing behavior.我注意到这种非常令人困惑的行为。

library(lubridate)
x = as_date(-25567)
as.integer(as_datetime(x)) # Returns NA

How can I get this to return the seconds since (or in this case before) UNIX epoch?我怎样才能让它返回自(或在这种情况下之前)UNIX 时代以来的秒数?

This works with base R, now that we covered that you really want as.Date("1970-01-01") .这适用于基础 R,现在我们已经介绍了您真正想要的as.Date("1970-01-01")

R> as.POSIXct("1900-01-01 00:00:00")    
[1] "1900-01-01 CST"                       
R> as.numeric(as.POSIXct("1900-01-01 00:00:00"))
[1] -2208967200        
R> 

I vaguely recall some OS-level irritations for dates prior to the epoch.我依稀记得对时代之前的日期的一些操作系统级别的刺激。 This may fail for you on the world's most commonly used OS but that is not really R's fault...在世界上最常用的操作系统上,这对您来说可能会失败,但这并不是 R 的错......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM