How can convert a timestamp to local time and date?
I have tried the following options for this specific timestamp: 1594598065352:
x <- as.POSIXct(as.numeric(as.character('1594598065352'))/1000, origin="1970-01-01", tz="UTC")
x
"2020-07-12 23:54:25 UTC"
x <- as.POSIXct(as.numeric(as.character('1594598065352'))/1000, origin="1970-01-01", tz="DST")
x
"2020-07-12 23:54:25 DST"
x <- as.POSIXct(as.numeric(as.character('1594598065352'))/1000, origin="1970-01-01", tz="GMT")
x
"2020-07-12 23:54:25 GMT"
I get the same result in all options:
2020-07-12 23:54:25
According to this timestamp converter page, I should get this below in my local time zone:
Monday, 13 July 2020 01:54:25.352 GMT+02:00 DST
Any ideas of how I can get this right in R?
You get the same output in all 3 cases because 1) and 3) are same (UTC and GMT) whereas 2) (DST) is not a valid tz value.
If you don't mention the timezone value it should by default give you time in your local time zone.
as.POSIXct(as.numeric('1594598065352')/1000, origin="1970-01-01")
Alternatively, you can run OlsonNames() in your console to get list of valid timezones in R. 'Etc/GMT-2' seems to be the one for you.
as.POSIXct(as.numeric('1594598065352')/1000,origin="1970-01-01", tz = 'Etc/GMT-2')
#[1] "2020-07-13 01:54:25 +02"
Related
I want to read this date 1199145600000, which is saved in JSON, in R.
to convert the numerical representation of the date to a string
but when I type:
as.Date(1199145600000,origin = "1904-01-01")
I get the following:
"-5877641-06-23"
When I should be getting this date
1 Jan 2008 00:00:00 GMT
I tried library(lubridate) still with no success.
Any help will be most appreciated.
Your time value is in milliseconds, so you'll need to divide it by 1000 to get it in seconds. Then you can use as.POSIXct() to convert to a datetime
as.POSIXct(1199145600000/1000, origin = "1970-01-01", tz = "GMT")
# [1] "2008-01-01 GMT"
I have a data set containing the following date, along with several others
03/12/2017 02:17:13
I want to put the whole data set into a data table, so I used read_csv and as.data.table to create DT which contained the date/time information in date.
Next I used
DT[, date := as.POSIXct(date, format = "%m/%d/%Y %H:%M:%S")]
Everything looked fine except I had some NA values where the original data had dates. The following expression returns an NA
as.POSIXct("03/12/2017 02:17:13", format = "%m/%d/%Y %H:%M:%S")
The question is why and how to fix.
Just use functions anytime() or utctime() from package anytime
R> library(anytime)
R> anytime("03/12/2017 02:17:13")
[1] "2017-03-12 01:17:13 CST"
R>
or
R> utctime("03/12/2017 02:17:13")
[1] "2017-03-11 20:17:13 CST"
R>
The real crux is that time did not exists in North America due to DST. You could parse it as UTC as UTC does not observer daylight savings:
R> utctime("03/12/2017 02:17:13", tz="UTC")
[1] "2017-03-12 02:17:13 UTC"
R>
You can express that UTC time as Mountain time, but it gets you the previous day:
R> utctime("03/12/2017 02:17:13", tz="America/Denver")
[1] "2017-03-11 19:17:13 MST"
R>
Ultimately, you (as the analyst) have to provide as to what was measured. UTC would make sense, the others may need adjustment.
My solution is below but ways to improve appreciated.
The explanation for the NA is that in the mountain time zone in the US, that date and time is in the window of the switch to daylight savings where the time doesn't exist, hence NA. While the time zone is not explicitly specified, I guess R must be picking it up from the computer's time, which is in "America/Denver"
The solution is to explicitly state the date/time string is in UTC and then convert back as follows:
time.utc <- as.POSIXct("03/12/2017 02:17:13", format = "%m/%d/%Y %H:%M:%S", tz = "UTC")
> time.utc
[1] "2017-03-12 02:17:13 UTC"
>
Next, add 6 hours to the UTC time which is the difference between UTC and MST
time.utc2 <- time.utc + 6 * 60 * 60
> time.utc2
[1] "2017-03-12 08:17:13 UTC"
>
Now convert to America/Denver time using daylight savings.
time.mdt <- format(time.utc2, usetz = TRUE, tz = "America/Denver")
> time.mdt
[1] "2017-03-12 01:17:13 MST"
>
Note that this is in standard time, because daylight savings doesn't start until 2 am.
If you change the original string from 2 am to 3 am, you get the following
> time.mdt
[1] "2017-03-12 03:17:13 MDT"
>
The hour between 2 and 3 is lost in the change from standard to daylight savings but the data are now correct.
I'm having difficulties formatting a datetime variable that originally came from Excel:
The data was read from Excel using package openxlsx with the detectDates = FALSE option. In the original Excel file they look like this:
udate utime
1/30/2015 4:48:44 PM
1/29/2015 4:17:23 PM
And this is what they look like when imported into R with the detectDates = FALSE
#-----------------------------------------------------------------------------------------#
# EXAMPLE DATA
#-----------------------------------------------------------------------------------------#
udate <- c(42034, 42033)
utime <- c(0.7005093, 0.6787384)
#-----------------------------------------------------------------------------------------#
# FORMAT DATE
#-----------------------------------------------------------------------------------------#
udate <- as.Date(udate - 25569, origin = "1970-01-01")
> udate
[1] "2015-01-30" "2015-01-29"
#-----------------------------------------------------------------------------------------#
# FORMAT TIME
#-----------------------------------------------------------------------------------------#
utime <- as.POSIXct((utime - 25569) * 86400, tz="GMT", origin="1970-01-01")
> utime
[1] "1899-12-30 16:48:45 GMT" "1899-12-30 16:17:23 GMT"
As one can see the time doesn't fully work (that is, the date component of the time does not work).
How can I properly have a single variable with the correct date AND time? It seems like simply adding 116 years may do the trick but I know it's not that simple because I suspect datetime formats are measures in millisec.
There is no time object. POSIXct is a datetime class, i.e., must contain a date and a time.
as.POSIXct(
as.POSIXlt(
as.Date(udate, origin = "1899-12-30"), #see ?as.Date
tz = "GMT"),
tz = "GMT") + utime * 3600 * 24
#[1] "2015-01-30 16:48:44 GMT" "2015-01-29 16:17:22 GMT"
Times without dates don't work due to fun things like DST or leap seconds.
the time my data are in EST time zone, and I try to use this time zone.
I want to count the week (in local time, not GMT), so I manually define an originTime in EDT
originTime = as.POSIXlt('2000-01-02 00:00:00 EDT')
dt2 = data.frame(time=c(as.POSIXlt('2000-01-09 00:00:05 EDT')))
dt2$week = as.integer( floor( ( as.numeric(dt2$time) - as.numeric(originTime) ) /(3600*24*7) ) )
dt2$wday = weekdays(dt2$time)
This works.
Now I want to find out, what's one week after a given time?
> as.POSIXlt( 1 * 3600*24*7 , origin = originTime)
[1] "2000-01-08 19:00:00 EST"
Here's the problem, R seems to think originTime is in GMT. Can somebody help? Thanks
Two serious problems. EDT does not really exist, and even if it did it would not be appropriate for a January date. The (US) Eastern timezone is "EST5EDT" to make it distinct from the Ozzie EST. (Furthermore these may be different on different OSes.) Safest would be tz="America/New_York". For data entry, you need to use the 'tz' parameter AND change the default (FALSE) setting of 'usetz':
(originTime = as.POSIXlt('2000-01-02 00:00:00', format="%Y-%m-%d %H:%M:%S",
tz="EST5EDT", usetz=TRUE) )
[1] "2000-01-02 EST"
A test using "%Z" which is only for output:
> format( as.POSIXlt('2000-01-02 00:00:00', format="%Y-%m-%d %H:%M:%S",
tz="America/New_York", usetz=TRUE) ,
format="%Y-%m-%d %H:%M:%S %Z")
[1] "2000-01-02 00:00:00 EST"
I've never used the origin argument in as.POSIXlt so cannot really explain why it fails to deliver the expected result I've always used +.POSIXt or seq.POSIXt to construct intervals:
format(as.POSIXlt('2000-01-02 00:00:00', tz="America/New_York", usetz=TRUE)+ 1*3600*24*7,
format="%Y-%m-%d %H:%M:%S %Z") # %Z is only used for output
# [1] "2000-01-09 00:00:00 EST" A week later at midnight.
I think the reason one needs to force a time printing with a format argument is that midnight-times are shortened to just printing the date with no further H:M:S information.
How can I convert local DateTime in the following format "12/31/2014 6:42:52 PM" to UTC in R? I tried this
as.POSIXct(as.Date("12/31/2014 6:42:52 PM", format="%m/%d/%Y %H:%M:%S"),tz="UTC")
but it doesn't seem to be valid.
If you want to shift a datetime from your current timezone to UTC, you need to
import in your local timezone, then just shift the display timezone to "UTC". e.g.: in Australian EST I am UTC+10.
out <- as.POSIXct("12/31/2014 6:42:52 PM", format="%m/%d/%Y %H:%M:%S")
out
#"2014-12-31 06:42:52 EST"
#(Australian Eastern Standard Time)
as.numeric(out)
#[1] 1419972172
Now shift the timezone for display purposes:
attr(out, "tzone") <- "UTC"
out
#[1] "2014-12-30 20:42:52 UTC"
# display goes 10 hours backwards as I'm UTC+10
as.numeric(out)
#[1] 1419972172
Note that this doesn't affect the underlying numeric data (seconds since 1970-01-01), it only changes what is displayed.