Convert from Unix Epoch time (in microseconds) to DateTime in R [duplicate] - r

This question already has an answer here:
Converting timestamp in microseconds to data and time in r
(1 answer)
Closed 3 years ago.
I have dates formatted like so: 1.475534e+15, which when converted via https://www.epochconverter.com/ convert to Monday, October 3, 2016 10:33:20 PM
However, I cannot replicate this in R.
For example:
library(anytime)
anytime(1.475534e+15)
Yields "46759781-01-30 14:33:20 EST"
The same is true if I do something like
as.POSIXct(1.475534e+15 / 1000, origin="1970-01-01")
The epochconverter site suggests that the time is in microseconds, but I haven't figured out how to convert from microseconds to a human-readable date.

a <- 1.475534e+15
as.POSIXct(a/1000000, origin="1970-01-01")
#[1] "2016-10-03 15:33:20 PDT" # interpreted in my local tz
With 7 significant digits in the scientific notation, that gets us around 20 minutes of time resolution. If you need more than that, you'll need to get the data in different format upstream.

Related

R - Convert Float number to Date [duplicate]

This question already has answers here:
How to convert a numeric value into a Date value
(4 answers)
Closed 2 years ago.
Wondering if you can give me some light about an outstanding problem I have in R, version 4.0.2. I am moving data from an excel file in a given sharepoint to a local DB. The problem facing is that when reading the file using readxl the columns in the original file as dates (datetime) are being returned as floating point numbers. I need to change the number back to datetime.
Libraries used:
library(readxl)
library(odbc)
library(lubridate)
Number
44047.8149884259
44055.2403009259
44048.504537037
Expected result
8/4/2020 7:33:35 PM
8/12/2020 5:46:02 AM
8/5/2020 12:06:32 PM
I've tried to use as_date with different formats and as.POSIXct but don't seem to have an answer.
Thanks in advance for your kindly inputs.
We could use
format(as.POSIXct(v1 * 60 *60 * 24, origin = '1899-12-30', tz = 'UTC'),
'%m/%d/%Y %I:%M:%S %p')
#[1] "08/04/2020 07:33:34 pm" "08/12/2020 05:46:01 am" "08/05/2020 12:06:31 pm"
data
v1 <- c(44047.8149884259, 44055.2403009259, 44048.504537037)

Change Timestamp in R [duplicate]

This question already has answers here:
Round a POSIX date (POSIXct) with base R functionality
(3 answers)
How to drop minutes in R?
(2 answers)
Closed 4 years ago.
I added a column to record the timestamp in R by using the following code
data$Date <- Sys.Date() 2018-08-10 18:06:21
This pastes a time-stamp with the current system date and time.
However, I want to set the time in the time-stamp to 00:00:00.
I tried using strptime and replacing the text. Thanks in advance.
There are multiple ways to do this , one way is to format your time part from Sys.time() to the required time.
data$Date <- format(Sys.time(), "%Y-%m-%d 00:00:00")
Sys.time returns current system date and time
Sys.time()
#[1] "2018-10-10 05:12:56 GMT"
format(Sys.time(), "%Y-%m-%d 00:00:00")
#[1] "2018-10-10 00:00:00"
Or if you want to use Sys.Date you can wrap it in as.POSIXct
data$Date <- as.POSIXct(Sys.Date())

Number of seconds to date conversion [duplicate]

This question already has answers here:
Convert UNIX epoch to Date object
(2 answers)
Closed 5 years ago.
I have a data set where one of the columns is sales date. Don't know why, but R converts it to numeric why performing any operation. I would like to convert it back to POSIXct date format in R. To do the same, I am using below code, but getting an unexpected result
x= as.Date(1448208000, origin = "1970-01-01")
[1] "3967028-10-31"
x= as.POSIXct(x,"%Y-%m-%d")
I am not good with dates format in R and would appreciate any kind of help in this regard.
1448208000 is the number of seconds since the unix epoch, and is the numeric representation of a POSIX object. To convert it back to POSIXct you want
as.POSIXct(1448208000, origin = "1970-01-01")
You'll also probably want to ensure the timezone is correct too; see the difference between these two commands
as.POSIXct(1448208000, origin = "1970-01-01", tz = "UTC")
# [1] "2015-11-22 16:00:00 UTC"
as.POSIXct(1448208000, origin = "1970-01-01", tz = "Australia/Melbourne")
# [1] "2015-11-23 03:00:00 AEDT"

In R, use lubridate to convert hms objects into seconds

simple question in lubridate--I want to convert an hms object into its appropriate number of seconds since the start of the day.
For instance
library(lubridate)
hms("12:34:45")
then I want to know exactly how long 12 hours, 34 minutes, and 45 seconds is, in seconds
something obvious like
seconds(hms("12:34:45"))
just returns
45s
which is not what I want. How do I convert these hms values into seconds? I'd like to use lubridate
R>lubridate::period_to_seconds(hms("01:00:00"))
gives expected 3600 seconds as numeric counting from 00:00:00
or in the case above:
R>period_to_seconds(hms("12:34:45"))
It doesn't matter which package you use -- it will have convert a date / datetime object into a POSIXct representation of seconds since the epoch. So you may as well do it in base R -- so here deploy ISOdatetime() with an arbitrary day, using today:
R> difftime(ISOdatetime(2012,7,2,12,34,45), ISOdatetime(2012,7,2,0,0,0))
Time difference of 12.5792 hours
So we want seconds:
R> difftime(ISOdatetime(2012,7,2,12,34,45), ISOdatetime(2012,7,2,0,0,0),
+ unit="secs")
Time difference of 45285 secs
And we can cast to numbers:
R> as.numeric(difftime(ISOdatetime(2012,7,2,12,34,45), +
ISOdatetime(2012,7,2,0,0,0), unit="secs"))
[1] 45285
Edit: And getting back to lubridate, this is arguably a bug:
> hms("12:34:45") - hms("00:00:00")
[1] 12 hours, 34 minutes and 45 seconds
R> as.numeric(hms("12:34:45") - hms("00:00:00"))
[1] 45
R>

microsecond time stamps in R

in a CSV file I have a few columns. One column has timestamps, where each stamp is the microseconds passed midnight of today (each csv file only have data within a day), so this is not ambiguous.
My question is, how do I parse these microseconds time stamps into R? thanks a lot!
part of my CSV file:
34201881666,250,10.8,2612,10.99,11,460283,11.01,21450,,,,,
34201883138,23712,10.02,562,10.03,10.04,113650,11,460283,,,,,
34201883138,23712,10.02,562,10.03,10.04,113650,10.05,57811,,,,,
The first column is the time stamps (the microseconds passed midnight of today). I want to construct a time series, for example in xts package, so that the time stamps of that series is from the first column.
Here is what I would do:
Create an 'anchor' timestamp of midnight using, e.g ISOdatetime(). Keep as POSIXct, or convert using as.numeric().
Add you microseconds-since-midnight to it, properly scaled.
Convert to POSIXct (if needed), and you're done.
Quick example using your first three timestamps:
R> ISOdatetime(2011,8,2,0,0,0) + c(34201881666, 34201883138, 34201883138)*1e-6
[1] "2011-08-02 09:30:01.881665 CDT" "2011-08-02 09:30:01.883137 CDT"
[3] "2011-08-02 09:30:01.883137 CDT"
R>

Resources