0

I have the following UNIX time string:

"1575824800.169"

If you convert this time you will get: 12/08/2019 17:06 on an online unix converter.

However when trying to convert this in R using the following code:

as.POSIXct("1575824800.169", format='%d/%m/%Y %H:%M', origin = "1970-01-01")

I am returned with the value NA

i'm struggling to see why the above code does not work - i have looked into different answers on here, but have not found one where the unix time string has 3 digits after the period (dot) in the string. Maybe this is the problem?

Beans On Toast
  • 903
  • 9
  • 25
  • Your theory sounds reasonable - have you tried getting rid of the numbers after the decimal place? – SDS0 Feb 20 '20 at 14:18
  • @SDS0 I did try that - but it still never worked - i am unsure as to why POSIXct does not seem to work with this date. I believe that there is a way to solve this issue - but maybe base R POSIXct() is not the best function to use – Beans On Toast Feb 20 '20 at 14:20

2 Answers2

5

You don't have a string encoding a date (as implied by using the format argument of as.POSIXct) but a number. If we re-cast the string as a numeric and get rid of the format argument we get the expected result (although we might need to use the tz argument to specify a timezone)

as.POSIXct(1575824800.169, origin = "1970-01-01")

Returns:

[1] "2019-12-08 18:06:40 CET"

Edit:

Adding timezone argument

as.POSIXct(1575824800.169, origin = "1970-01-01", tz = "UCT")

Returns:

[1] "2019-12-08 17:06:40 UTC"

Edit 2:

Regarding converting the string to numeric with as.numeric: As @IceCreamToucan pointed out, it does not matter. Only the "printed" value changes, the internal representation stays the same and therefore the result is still correct

as.POSIXct(as.numeric("1575824800.169"), origin = "1970-01-01", tz = "UCT")

Returns the same:

[1] "2019-12-08 17:06:40 UTC"
dario
  • 6,415
  • 2
  • 12
  • 26
  • Ah thank you - the issue actually seems to be that when converting from character to numeric - R removes any trailing values after the decimal point - which is why when converting to POSIXct - easily solved with https://stackoverflow.com/questions/29880210/as-numeric-removes-decimal-places-in-r-how-to-change – Beans On Toast Feb 20 '20 at 14:24
  • 1
    I think the problem was that you tried to use the numeric representation of a datetime as if it was a string representation (i.e. "21-12-1999"). The `format` argument works only with the latter while the `origin argument` works only with the former... – dario Feb 20 '20 at 14:32
0

The anytime package aims to help here with some built-in heuristics. So numeric data in that range is automagically taken as (fractional) seconds since the epoch:

R> anytime::anytime(1575824800.169)
[1] "2019-12-08 11:06:40.168 CST"
R> 

There is also a wrapper for UTC and some other options should you need them:

R> anytime::utctime(1575824800.169)
[1] "2019-12-08 17:06:40.168 UTC"
R> 
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725