How to get origin from POSIXct object
Asked Answered
V

0

7

I have a function like so:

foo = function(time_in){
    # code here that changes POSIXct to numeric
    time_out = as.POSIXct(time_in, origin = '1970-01-01')
}

where the user enters time_in as a POSIXct object. I use this parameter in a linear model (lm()) which spits my value back out as a numeric. Finally, I want to convert it back to a POSIXct object for my user. The problem is, as.POSIXct() requires an origin which is typically Jan 1, 1970 UTC. But what if my user is working off a different origin? How can I extract the origin from time_in to use it as an argument for time_out?

I have looked for documentation on how to get the origin, but str(time_in) and attributes(time_in) don't give me anything and I haven't found much else. Since this will be in a package, I'd like to stick with base R functions to limit the number of dependencies needed.

Vivle answered 7/6, 2016 at 22:55 Comment(5)
If it's an as.POSIXct object, it's origin is 1970-01-01 as far as I am aware. It doesn't store an origin. From ?as.POSIXct - "...the origin of time for the ‘"POSIXct"’ class, ‘1970-01-01 00:00.00 UTC’...Mizzenmast
For both Date and POSIXct class, identifying the origin should be as simple as x - as.numeric (x). For POSIXct, this should be moot, since the origin is always 1970-01-01Armijo
Okay, thanks guys. I thought that was just the default origin, not the only possible. That would explain why I couldn't find anything on it. Thanks.Vivle
"1970-01-01" is not the only possible entry. Excel on Windows uses "1899-12-30" and on Mac "1904-01-01"Anchylose
This confused me too, because origin is required when calling as.POSIXct with a number, so I thought origin must be stored. No, it's just being pedantic and you must specify 1970 for it to do what you expect.Acceptance

© 2022 - 2024 — McMap. All rights reserved.