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"
Related
I realize there are several other questions similar to this, but all the answers I have seen suggest dividing a UNIX timestamp in milliseconds by 1000 for R to handle it properly (for example, 1506378449 instead of 1506378448618).
Is there a way to convert it without dividing by 1000 and keep those fractional seconds?
What I have tried so far does not work:
as.POSIXct(1506378448618, origin = "1970-01-01", tz =
"America/Chicago", format = "%Y-%m-%d %H:%M:%OS")
comes back NA, and it similarly does so with %OS1, 2, 3, etc.
anytime(1506378448618, tz = "America/Chicago")
and
as_datetime(1506378448618, tz = "America/Chicago")
both come back with times far into the future ("49705-03-26 12:56:58 CDT")
The best answer I can come up with is to split the string and do something like:
as_datetime(1506378448, tz = "America/Chicago") + .618
Which is fine, but it seems like there must be a better way.
I'm not entirely sure what you're asking.
as.POSIXct(...) does in fact keep track of fractional seconds. You can print fractions of seconds using the appropriate format string.
For example to print the first 3 digits you can do
t <- 1506378448618;
format(as.POSIXct(t / 1000, origin = "1970-01-01", tz = "America/Chicago"), "%H:%M:%OS3")
#[1] "17:27:28.618"
Note: The reason why as.POSIXct(..., format = "%Y-%m-%d %H:%M:%OS") results in NA is simply because your input object 1506378448618 does not have format "%Y-%m-%d %H:%M:%OS". You can however specify a format string when printing the POSIXct object.
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.
I have the same problem as https://stackoverflow.com/questions/15575713/modifying-timezone-of-a-posixct-object-without-changing-the-display. However, I followed the accepted response but I don't get the desired result.
dt is a string ("2017-07-07 15.46.00"). I need to change it to data-time format with CDT time zone ("2017-07-07 15:46:00 CDT"). I can do it by lubridate::ymd_hms and I get my desired result (lub.dt: "2017-07-07 15:46:00 CDT") but it is too slow for my dataset size. I converted dt using fasttime::fastPOSIXct which is very fast but the function assumes the input is in 'GMT'. So I used 'GMT' for the output as well to get the same date-time display (fast.dt: "2017-07-07 15:46:00 GMT"). Finally, I tried to change the time zone by as.POSIXct. I used the same tz (America/Chicago) for origin and the function but I get this result "2017-07-07 16:46:00 CDT" which is (time +1).
library(lubridate)
library(fasttime)
dt <- "2017-07-07 15.46.00"
lub.dt <- ymd_hms(dt, tz = 'America/Chicago')
fast.dt <- fastPOSIXct(dt, tz = 'GMT')
fast.dt.new.tz <- as.POSIXct(x = as.numeric(fast.dt), origin = as.POSIXct("1970-01-01", tz = 'America/Chicago'),tz = 'America/Chicago')
Can anyone guide me what I did wrong?
This is muddled. Your problem is the insistence on the (very fine) fasttime package, and now trying to fudge the TZ adjustment.
I would recommend against. You could use anytime which is also pretty fast (compiled code, but tries different formats):
R> anytime::anytime("2017-07-07 15.46.00")
[1] "2017-07-07 15:46:00 CDT"
R>
It's advantage is that it does what you want here: interpret the string as local time, and set the local time (Chicago for me too).
I am reading a CSV file with two columns specifying a date w/time (fractional seconds). The format is like this: 2015-07-13 09:05:52.761, which is originally a factor.
I tried reading the column in using POSIXlt and several variations of this:
time_d$time_started_visit <- as.POSIXlt(time_d$time_started_visit, format="%Y-%m-%d %H:M%:%OS")
All this did was convert the values of the column to NA. I would really like to convert this so I can get the difference in time between the two columns, any suggestions?
You used M% instead of %M
# If you wish to retain the fractional seconds
options(digits.secs = 3)
as.POSIXlt(x, format="%Y-%m-%d %H:%M:%OS")
You had one little error in your code:
Do not use format="%Y-%m-%d %H:M%:%OS but:
format= "%Y-%m-%d %H:%M:%OS"
You changed %M and M%
The whole code is then:
as.POSIXlt(temp$x, format= "%Y-%m-%d %H:%M:%OS")
I have a simple question regarding R's lubridate package. I've a series of timestamps in seconds since epoch. I want to convert this to YYYY-MM-DD-HH format. In base R, I can do something like this to first convert it to a date format
> x = as.POSIXct(1356129107,origin = "1970-01-01",tz = "GMT")
> x
[1] "2012-12-21 22:31:47 GMT"
Note the above just converts it to a date format, not the YYYY-MM-DD-HH format. How would I do this in lubridate? How would I do it using base R?
Thanks much in advance
lubridate has an as_datetime() that happens to have UNIX epoch time as the default origin time to make this really simple:
> as_datetime(1356129107)
[1] "2012-12-21 22:31:47 UTC"
more details can be found here: https://rdrr.io/cran/lubridate/man/as_date.html
Dirk is correct. However, if you are intent on using lubridate functions:
paste( year(dt), month(dt), mday(dt), hour(dt) sep="-")
If on the other hand you want to handle the POSIXct objects the way they were supposed to be used then this should satisfy:
format(x, format="%Y-%m-%d-%H")
I use the lubridate solution provided by #leerssej
But in case anyone prefers #IRTFM's solution in base R, but also wants minutes and seconds, here's an example of how to do that:
as.POSIXct("2019-03-15 16:17:42" , format="%Y-%m-%d %H:%M:%OS")