Date-time to numeric and backwards - r

Dates/Times in R
I have this date: "2016-10-29 15:00:00" and i want to convert it to numeric and backwards to the same date and time i had. I used this to convert it to numeric:
as.numeric(as.POSIXct("2016-10-29 15:00:00"))
How i can get back my initial date and time?
"2016-10-29 15:00:00"
as.numeric(as.POSIXct("2016-10-29 15:00:00"))
1477771200
I obtain that answer, but i need it back to "2016-10-29 15:00:00". What should i do?

You can use as.POSIXct() on your number, but you also need to supply the origin and (probably) timezone
as.POSIXct(1477713600, origin = "1970-01-01", tz = "Australia/Melbourne")
"2016-10-29 15:00:00 AEDT"
The as.POSIX() comamnd needs to know from which reference point the numeric starts. This is usually the Unix Epoch of 1970-01-01
The documentation for ?as.POSIXct shows the useage for a numeric object
S3 method for class 'numeric'
as.POSIXlt(x, tz = "", origin, ...)
showing you need to supply the origin

This should work.
as.POSIXct(yourNumeric)
Where yourNumeric is your number.

Related

Convert Windows time into readable format using r?

I have Windows tick time in milliseconds like 13228488488553 (which should be 12/03/2020 13:08:08 at 1 or 2s approximately). As Windows clock the origin is 1/1/1601. I would like to see it in a readable date/hour format using everything (i.e. not rounding up to seconds, but up to milliseconds). A perfect example format would be DD/MM/YY HH:MM:SS.MSS
I did not find any package doing it, and I tried manually but I am stuck with roundings and leap years. Does anyone know a package or did already do it with a handmade function?
Here's an approach with as.POSIXct from base R:
format(as.POSIXct(ticktime/1000,
origin = "1601-01-01", tz = "UTC"),
format = "%d/%m/%Y %H:%M:%OS3")
#[1] "12/03/2020 12:08:08.552"
Edit: Perhaps this is your timezone:
format(as.POSIXct(ticktime/1000,
origin = "1601-01-01", tz = "CET"),
format = "%d/%m/%Y %H:%M:%OS3")
#[1] "12/03/2020 13:08:08.552"

Convert UNIX time string to date in R

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?
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"
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>

Formatting Timezone part of DateTime object in R

Is there a way to extract the timezone or format the timezone part of a datetime object, in the form of, say "+530" instead of "IST" or "Asia/Kolkata"?
(Need this as it is the ISO 8601 format used in javascript)
Example:
as.POSIXct(1499773898,tz="Asia/Kolkata",origin="1970-01-01")
[1] "2017-07-11 17:21:38 IST"
Instead, I'd like to maybe specify the format argument in as.POSIXct, so that the output looks something like this:
[1] "2017-07-11 17:21:38 +530"
Or a function which can pull out the timezone offset in this manner:
timezone_offset("2017-07-11 17:21:38 IST")
[1] "+530"
Does lubridate or any other package have the capability to do this?
You can do both with format, but note that result is character string, no longer POSIXct object.
x <- as.POSIXct(1499773898,tz="Asia/Kolkata",origin="1970-01-01")
"2017-07-11 17:21:38 IST"
e.g., Show timestamp in ISO 8601:
format(x, "%Y-%m-%dT%H:%M:%S%z")
"2017-07-11T17:21:38+0530"
e.g., Show just offset from UTC:
format(x, "%z")
"+0530"
Note that for operations in R this has little consequence because all POSIXct objects are stored internally as numeric in UTC; seconds from 1970-01-01 00:00:00.
To write POSIXct timestamps in ISO 8601 to file, you can use format as described above or fwrite function in data.table, which does so by default (see dateTimeAs argument).

Convert chron to POSIXct in GMT format

Normally, I use chron to represent date/time objects. However, I need to use some functions that work with the POSIX format, so I am trying to go from chron to POSIXct. Using as.POSIXct() seems to work but the result is in localtime instead of GMT (the original data is in GMT).
x <- chron(dates="05/12/15", times="12:30:45")
as.POSIXct(x, tz="GMT")
"2015-05-12 13:30:45 BST"
what I want is:
"2015-05-12 12:30:45 GMT"
but I can't find a way to obtain it.
strptime() won't work because the original input is not a string, but a chron object. Of course I could go from a chron object to a character string and then to POSIXct but it seems a bit convoluted way to do it.
I suppose I could force my R session to use GMT with Sys.timezone(), but I'd prefer not to. Any other suggestion? Thank you.
Just try:
x <- chron(dates="05/12/15", times="12:30:45")
y<-as.POSIXct(x)
attr(y,"tzone")<-"GMT"
y
#[1] "2015-05-12 12:30:45 GMT"

R dates "origin" must be supplied

My code:
axis.Date(1,sites$date, origin="1970-01-01")
Error:
Error in as.Date.numeric(x) : 'origin' must be supplied
Why is it asking me for the origin when I supplied it in the above code?
I suspect you meant:
axis.Date(1, as.Date(sites$date, origin = "1970-01-01"))
as the 'x' argument to as.Date() has to be of type Date.
As an aside, this would have been appropriate as a follow-up or edit of your previous question.
My R use 1970-01-01:
>as.Date(15103, origin="1970-01-01")
[1] "2011-05-09"
and this matches the calculation from
>as.numeric(as.Date(15103, origin="1970-01-01"))
So generally this has been solved, but you might get this error message because the date you use is not in the correct format.
I know this is an old post, but whenever I run this I get NA all the way down my date column. My dates are in this format 20150521 – NealC Jun 5 '15 at 16:06
If you have dates of this format just check the format of your dates with:
str(sides$date)
If the format is not a character, then convert it:
as.character(sides$date)
For as.Date, you won't need an origin any longer, because this is supplied for numeric values only. Thus you can use (assuming you have the format of NealC):
as.Date(as.character(sides$date),format="%Y%m%d")
I hope this might help some of you.
Another option is the lubridate package:
library(lubridate)
x <- 15103
as_date(x, origin = lubridate::origin)
"2011-05-09"
y <- 1442866615
as_datetime(y, origin = lubridate::origin)
"2015-09-21 20:16:55 UTC"
From the docs:
Origin is the date-time for 1970-01-01 UTC in POSIXct format. This date-time is the origin for the numbering system used by POSIXct, POSIXlt, chron, and Date classes.
If you have both date and time information in the numeric value, then use as.POSIXct. Data.table package IDateTime format is such a case. If you use fwrite to save a file, the package automatically converts date-times to idatetime format which is unix time. To convert back to normal format following can be done.
Example: Let's say you have a unix time stamp with date and time info: 1442866615
> as.POSIXct(1442866615,origin="1970-01-01")
[1] "2015-09-21 16:16:54 EDT"
by the way, the zoo package, if it is loaded, overrides the base as.Date() with its own which, by default, provides origin="1970-01-01".
(i mention this in case you find that sometimes you need to add the origin, and sometimes you don't.)

Resources