convert local dateTime to UTC in R - r

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.

Related

How to turn PM/AM time stamp am/pm

I have a dataset with a column where date and time is stored.
The data I have is:
03/17/2020 09:30:00 PM
I want to convert AM/PM to a 24hour format.
My attempt was using this:
as.POSIXct(df$Date, format="%d/%m/%Y %I:%M:%S %p", tz="UTC")
When I run this with the whole dataset, the majority of dates turns into "NA".
Why is this happening? I am really confused.
Using lubridate:
x <- "03/17/2020 09:30:00 PM"
lubridate::mdy_hms(x)
[1] "2020-03-17 21:30:00 UTC"
Using as.Posixct: note that you need the month / day convention, not the day/month:
as.POSIXct(x, format="%m/%d/%Y %I:%M:%S %p", tz = "UTC")
[1] "2020-03-17 21:30:00 UTC"

using POSIXct in R

I have the following code:
as.POSIXct(c('03/08/2015 03:08:18 AM','03/09/2014 02:01:05 AM'),
format="%m/%d/%Y %l:%M:%S %p")
[1] "2015-03-08 03:08:18 EDT" NA
Why is the 2nd time returning NA when converted?
I see you're working in the EDT (Eastern Daylight Time) time zone
On 09th March 2014 the clocks went forward one hour at 02:00:00. Therefore, the time of 02:01:05 doesn't actually exist.
First you should check the source of the data; should you actually be working in EDT?
Most likely not, so you'll want to set the tz argument to the actual timezone.
For example
as.POSIXct(
c('03/08/2015 03:08:18 AM','03/09/2014 02:01:05 AM')
, format="%m/%d/%Y %l:%M:%S %p"
, tz = "EST" ## change this to the actual timezone you need.
)
#"2015-03-08 03:08:18 EST" "2014-03-09 02:01:05 EST"

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"

why local time dosen't work? POSIXct in specific time zone [R]

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 do you convert time stamp that has AM/PM at the end of the string?

I am trying to convert this time stamp to POSIXct
t1 <- c("19-Jun-13 06.00.00.00 PM")
If I do this:
t1 <- as.POSIXct(t1, format="%d-%b-%y %H:%M:%S")
whould this convert this time stamp right? Does that considder the AM/PM at the end?
Read ?strptime. %p, which only works with %I, not %H. Your time format is also incorrect. Your times are separated by ".", not ":".
as.POSIXct("19-Jun-13 06.00.00.00 PM", format="%d-%b-%y %I.%M.%OS %p")
I don't understad why strptime doesn't recognize properly the %p format. However, the function dmy_hmsfrom package lubridate works well.
lubridate::dmy_hms("19-Jun-13 06.00.00.00 PM") creates the following result:
[1] "2013-06-19 18:00:00 UTC"
which you can "reformat" if you want, say Y-m-d H:M:
as.POSIXct(dmy_hms("19-Jun-13 06.00.00.00 PM"), format="%Y-%m-%d %H:%M")
[1] "2013-06-19 18:00:00 UTC"

Resources