How to convert from CEST to UTC? - r

I want to convert number of days to date with time:
> 15525.1+as.Date("1970-01-01")
[1] "2012-07-04" ## correct but no time
I tried this:
> apollo.fmt <- "%B %d, %Y, %H:%M:%S"
> as.POSIXct((15525.1+as.Date("1970-01-01")), format=apollo.fmt, tz="UTC")
[1] "2012-07-04 04:24:00 CEST"
but as you see the results provide in CEST. But I need it it in UTC.
Any hints on this?

For the original conversion, refer to this question: Converting numeric time to datetime POSIXct format in R and these pages: Date-times in R , Date-time conversions and Converting excel dates (number) to R date-time object. Bascially, it depends on your data source, the time origin for that data sources (Excel, Apache etc.) and the units. For example, you may have the total time elapsed in seconds, minutes, hours or days since the time origin for your data source which will be different for Excel or Apache. Once you have this information, you can use strptime or origin arguments and convert to R date-time objects.
If you are only concerned with changing the timezone, you can use attr:
> u <- Sys.time()
> u
[1] "2017-12-21 09:01:35 EST"
> attr(u, "tzone") <- "UTC"
> u
[1] "2017-12-21 14:01:35 UTC"
You may want to check up on the valid timezones for your machine though. A good way to get a time-zone that works with your machine would be googleway::google_timezone. To get the coordinates for your location (or the location from where you're importing data), you can either look those up online or use ggmap::geocode() - useful if converting time stamps in data from different time zones.

I think the problem is as.POSIXct doesn't change anything if the time is already POSIXct, so the tz option has no effect.
Use attr as explained here

Related

Conversion from Date to POSIXct object - error by one hour

I'm trying to convert a date object (obtained from user input via Shiny) to a POSIXct object - I want to get to the bottom of why an hour is added to the obtained POSIXct object in spite of both objects having the same timezone.
See code below. I tried finding and answer on google or the Stackoverflow forum, without success.
> "2019-05-11" %>% as.Date(tz = "UTC") %>% as.POSIXct(tz = "UTC")
[1] "2019-05-11 01:00:00 BST"
> "2019-05-11" %>% as.Date(tz = "GMT") %>% as.POSIXct(tz = "GMT")
[1] "2019-05-11 01:00:00 BST"
I have entered the lines above at the console (RStudio). I would have expected the POSIXct objects to be
"2019-05-11 00:00:00 BST".
Does anyone get this? I want to avoid manipulating the results above by subtracting from the POSIXct object in case the issue above has to do with my computer (clock) and might not exist on a computer where I intend to deploy my Shiny app.
I'm not very much of an expert about R or computers in general, and I would be grateful for an answer without too much jargon.
The output is in your local time zone, which seems to be "BST" = GMT+1, which is why you see that added hour
You can add this line before your code to make your timezone UTC
Sys.setenv(TZ="UTC")
You don't need to change your locales right now. ?timezones yields:
A time zone region may be known by several names: for example
"Europe/London" is also known as GB, GB-Eire, Europe/Belfast,
Europe/Guernsey, Europe/Isle_of_Man and Europe/Jersey.
So we may use strftime after as.POSIXct and set tz="GB" for both calls. Magically "BST" appears as time zone abbreviation after the expected time.
tm <- "2019-05-11"
strftime(as.POSIXct(tm, tz="GB"), format="%Y-%m-%d %H:%M:%S %Z", tz="GB")
# [1] "2019-05-11 00:00:00 BST"

How to convert matlab numeric to POSIXct [duplicate]

I have some MATLAB serial date number that I need to use in R but I havt to convert them to a normal date.
Matlab:
datestr(733038.6)
ans =
27-Dec-2006 14:24:00
you can see it gives the date and time.
Now we try in R:
Matlab2Rdate <- function(val) as.Date(val - 1, origin = '0000-01-01')
> Matlab2Rdate(733038.6)
[1] "2006-12-27"
It gives only the date but I need also the time? Any idea
The trick is Matlab uses "January 01, 0000", a fictional reference date, to calculate its date number. The origin of time for the "POSIXct" class in R is, ‘1970-01-01 00:00.00 UTC’. You can read about how different systems handle dates here.
Before converting, you need to account for this difference in reference from one format to another. The POSIX manual contains such an example. Here's my output:
> val<-733038.6
> as.POSIXct((val - 719529)*86400, origin = "1970-01-01", tz = "UTC")
[1] "2006-12-27 14:23:59 UTC"
Where 719529 is ‘1970-01-01 00:00.00 UTC’ in Matlab's datenum and 86400 the number of seconds in an standard UTC day.

Dealing with twitter timestamps in R

I've got a dataset with tweets and the informations twitter provides about them. I need to transform my dates from the format given to one I can understand properly (preferentially using a function I can choose the format, since I might need to select the tweets by day of the week, time of the day or anything like that) using R, I'm just starting to learn the language.
the format I've got the dates in is:
1420121295000
1420121298000
I've researched a bit before answering and tried to use functions like as.POSIXct, as>POSIXlt and others, they all got me this error:
Error in as.POSIXct.default(date, format = "%a %b %d %H:%M:%S %z %Y", :
do not know how to convert 'date' to class "POSIXct"
The format above is in epochs. Assuming this is in milliseconds since the epoch (you would have to double-check with the Twitter api), you can convert from epoch to UTC time using anytime function from the anytime package as shown below, which returns "2015-01-01 14:08:15 UTC."
anytime(1420121295000*0.001) #times 0.001 to convert to seconds
format(anytime(1420121295000*0.001), tz = "America/New_York", usetz=TRUE) #converting from UTC to EST timezone.

as.POSIXct assigns different timezones [duplicate]

I am trying to convert UTC time to local standard time. I have found many functions which convert to Local Daylight Time, but I was not successful in getting the standard time. Right now, I have the following code which converts to local daylight time at my specific timezone:
pb.date <- as.POSIXct(date,tz="UTC")
format(pb.date, tz="timeZone",usetz=TRUE)
I would appreciate any help.
First, POSIXct date-times are always UCT internally. The print.POSIXt and format.POSIXt methods will appropriately make the TZ shift on output from their internal representations:
pb.date <- as.POSIXct(Sys.Date())
Sys.Date()
#[1] "2015-07-09"
So that was midnight of the current date in Greenwich:
format(pb.date, tz="America/Los_Angeles",usetz=TRUE)
#[1] "2015-07-08 17:00:00 PDT"
When it's midnight in Greenwich, it's 5PM Daylight Time in the previous day on the Left Coast of the US. You need to use the correct character values for your TZ (and your OS) both of which at the moment are unspecified.
The US Pacific timezone is 8 hours behind GMT (in winter months) so you can use a timezone that is Standard/Daylight-agnostic:
> format(pb.date,usetz=TRUE, tz="Etc/GMT+8")
[1] "2015-07-08 16:00:00 GMT+8"
(Notice the reversal of + with "behind" and - with "ahead".)
I know this question has an accepted answer, but in case anyone comes along and this can help. I needed a function to convert UTC times to MTN time (Server is in UTC, we operate in MTN).
Not sure why, but needed to force it to UTC/GMT first and the convert it to MTN. However it does work
mtn_ts = function(utcTime){
library(lubridate)
toTz = "us/mountain"
utcTime = force_tz(utcTime,tzone= 'GMT')
dt = as.POSIXct(format(utcTime,tz = toTz,origin ='GMT', usetz=TRUE))
dt = force_tz(dt,tzone= toTz)
return(dt)
}
mtn_ts(as.POSIXct("2021-09-27 14:48:51.000000000"))

How to extract the time using R from a MATLAB serial date number?

I have some MATLAB serial date number that I need to use in R but I havt to convert them to a normal date.
Matlab:
datestr(733038.6)
ans =
27-Dec-2006 14:24:00
you can see it gives the date and time.
Now we try in R:
Matlab2Rdate <- function(val) as.Date(val - 1, origin = '0000-01-01')
> Matlab2Rdate(733038.6)
[1] "2006-12-27"
It gives only the date but I need also the time? Any idea
The trick is Matlab uses "January 01, 0000", a fictional reference date, to calculate its date number. The origin of time for the "POSIXct" class in R is, ‘1970-01-01 00:00.00 UTC’. You can read about how different systems handle dates here.
Before converting, you need to account for this difference in reference from one format to another. The POSIX manual contains such an example. Here's my output:
> val<-733038.6
> as.POSIXct((val - 719529)*86400, origin = "1970-01-01", tz = "UTC")
[1] "2006-12-27 14:23:59 UTC"
Where 719529 is ‘1970-01-01 00:00.00 UTC’ in Matlab's datenum and 86400 the number of seconds in an standard UTC day.

Resources