date from number of days in R? - r

I have col of number of days since 1970-01-01 00:00:00 UTC that I want to convert to date and time UTC.
I tried this:
z <- Sys.time()
j=floor(unclass(z)/86400) # the number of days since 1970-01-01 (UTC)
as.POSIXct(j, origin = "1970-01-01")
[1] "1970-01-01 05:53:22 CET"
But it is not correct. any idea?

You have to pass to the function as.POSIXct the number of seconds, try this code
j<-17148.5625000000
as.POSIXct(j*24*60*60, origin = "1970-01-01", tz="UTC")
[1] "2016-12-13 13:30:00 UTC"
If you want only the day in output
as.Date(as.POSIXct(j*24*60*60, origin = "1970-01-01", tz="UTC"))
[1] "2016-12-13"

Related

lubridate [R]: difference between a date and now() to be added to a date

I am struggling a bit with lubridate
I have a date series in the past in df$mydate variable as POSIXct. I want to take max(df$mydate) subtract it from now(), then subtract 2 more days from that time interval - i.e. make the interval 2 days shorter than the difference between the latest date of the series and today. The obtained time interval then should be added to all dates in df$mydate so that the dates block is brought forward to end 2 days in the past from today.
How can I do this with lubridate?
when I try to convert now() - max(df$mydate) to interval I get an empty interval. So I do not even get to step 2 - shortening the interval by 2 days and to step 3 - trying to then add this time length to dates I have.
The Idee with lubridate is to take care of all the transformation between intervals and dates for you so you don't need to think about it. This simple code does exactly that what you want.
library(lubridate)
my_date <-as.POSIXlt(paste0("2009-08-",1:10))
time_diff <- now() - max(my_date)
time_diff_short = time_diff - 2
my_date + time_diff_short
What I found was that you need my_date to be of the format POSIXlt
You can use difftime from base to get the time difference to now Sys.time() and subtract 2 days.
x <- x + (difftime(Sys.time(), max(x), units = "days") - 2)
x
#[1] "2020-09-11 10:32:20 CEST" "2020-09-12 10:32:20 CEST"
#[3] "2020-09-13 10:32:20 CEST" "2020-09-14 10:32:20 CEST"
Sys.time()
#[1] "2020-09-16 10:32:20 CEST"
Data:
(x <- seq(as.POSIXct("2000-01-01 12:00:00"), length.out = 4, by = "days"))
#[1] "2000-01-01 12:00:00 CET" "2000-01-02 12:00:00 CET"
#[3] "2000-01-03 12:00:00 CET" "2000-01-04 12:00:00 CET"

Change date and time in UTC to UTC-5h and UTC-6h based on daylight saving in R

I have a column of dates and time in UTC. I would like to convert the date and time corresponding to the time zone: UTC-5h when DST start and UTC-6h when DST end., keeping in mind the daylight saving.
Date
2018-06-11 14:48:26 UTC
2018-06-11 14:48:25 UTC
I would be glad if anyone can help me! Thanks!
x <- c("2018-06-11 14:48:26 UTC", "2018-06-11 14:48:25 UTC")
x <- as.POSIXct(x, tz = "UTC")
#find time zone name
OlsonNames()[grepl("Mexico", OlsonNames())]
#[1] "America/Mexico_City" "Mexico/BajaNorte" "Mexico/BajaSur" "Mexico/General"
#change time zone
attr(x, "tzone") <- "America/Mexico_City"
x
#[1] "2018-06-11 09:48:26 CDT" "2018-06-11 09:48:25 CDT"

as.POSIXCt returning incorrect date value in R

I am trying to convert the Eopch time interval using as.POSIXct function in R
into the local timezone and in the Europe/Vienna timezone. But for both the timezone its displaying a weird date
as.POSIXct(1385856600000, origin = "1970-01-01", tz='CET')
[1] "45886-01-17 23:40:00 CET"
> as.POSIXct(1385856600000, origin = "1970-01-01")
[1] "45886-01-18 04:10:00 IST"
What i am typing wrong here ?
you can use anytime package too.. much simpler to use than as.POSIXCt
anytime(1385856600000/1000)
[1] "2013-12-01 05:40:00 IST"
I think that your time is in milliseconds. If you divide by 1000, you get:
as.POSIXct(1385856600, origin = "1970-01-01")
[1] "2013-12-01 05:40:00 IST"

R round a date with timezone

timestamp = 1491800340000
I'm having trouble with some date manipulation in R. The timestamp above is:
2017-04-10T04:59:00.000 GMT
2017-04-09T23:59:00.000 America/Bogota (Local time)
I want to round it to 2017-04-09T00:00:00.000 GMT because my daily aggregations are set to 00:00 GMT.
How can I do that?
Here's what I tried:
> Sys.timezone()
[1] "America/Bogota"
> timestamp = 1491800340000
> date = strptime(timestamp / 1000, "%s");
[1] "2017-04-09 23:59:00 COT"
> midnightLocal = trunc(date, "day");
[1] "2017-04-09 COT"
> midnightUTC = strptime(format(midnightLocal, "%Y-%m-%d"), "%Y-%m-%d", tz = "UTC");
[1] "2017-04-09 UTC"
> truncatedtimestamp = as.integer(format(midnightUTC, "%s"));
[1] 1491714000
which is 2017-04-09T05:00:00.000 GMT (not midnight as I expected). Looks like I failed to specify the timezone somewhere?
I tried many things like POSIXct but did not succeed.
Any hint is appreciated!
Cheers
ps: I'd prefer not to install any package
A little trickery:
timestamp = 1491800340000
ts <- as.POSIXct(timestamp / 1000, origin = "1970-01-01 00:00:00 GMT")
ts2 <- as.Date(trunc(ts, "day"))
attr(ts2, "tzone") <- "GMT"
format(ts2, "%Y-%m-%d %H:%M:%S %Z") # to prove it's midnight
# [1] "2017-04-09 00:00:00 UTC"
class(ts2)
# [1] "Date"
From here you have a couple of options: a little brute-force (numeric conversion) or perhaps the more time-friendly/safe way.
Brute-force numeric:
ts3a <- as.numeric(ts2) * 60*60*24
ts3a
# [1] 1491696000
as.POSIXct(ts3a, origin = "1970-01-01 00:00:00 GMT", tz = "GMT")
# [1] "2017-04-09 GMT"
Time-friendly/safe:
ts3b <- as.POSIXct(ts2)
attr(ts3b, "tzone") <- "GMT"
ts3b
# [1] "2017-04-09 GMT"
(Since they are POSIXct, it's showing the date only because it is midnight; you can easily prove it's correct.)

How to add the time to a date when using as.date?

I have measurements that were taken at this time: 13880 and they represent "days since 1970-01-01 00:00:00"
So now I want to know the dat and time:
as.Date(13880, origin="1970-01-01")
[1] "2008-01-02" # works fine
Now to add the time:
as.Date(13880, origin="1970-01-01",tz = "UTC", format="%Y/%m/%d %H:%M:%S")
[1] NA
or
as.POSIXct(13880, origin="1970-01-01")
[1] "1970-01-01 04:51:20 CET"
as.POSIXlt(13879, origin="1970-01-01")
[1] "1970-01-01 04:51:19 CET"
None of these worked for me. Any idea?
as.POSIXct(as.Date("1970-01-01") + 13880) # returns "2008-01-01 19:00:00 EST"
as.POSIXct(as.Date("1970-01-01") + 13880.5) # returns "2008-01-02 07:00:00 EST"
You can also set your time zone:
How to change the default time zone in R?
also: http://blog.revolutionanalytics.com/2009/06/converting-time-zones.html

Resources