6

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.

CephBirk
  • 6,422
  • 5
  • 56
  • 74
  • 3
    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’...* – thelatemail Jun 07 '16 at 23:01
  • 3
    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-01 – Benjamin Jun 07 '16 at 23:58
  • 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. – CephBirk Jun 08 '16 at 00:34
  • "1970-01-01" is not the only possible entry. Excel on Windows uses "1899-12-30" and on Mac "1904-01-01" –  Dec 25 '19 at 10:53
  • 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. – Gordon Oct 14 '20 at 19:06

0 Answers0