Problem with converting unix time zone in lubridate - r

I import dataset from treasuredata in a JSON format and then transform unix time column to standard time using Lubridate fuction "as_datetime".
I set tz(timezone) as GMT+7 as I am in Bangkok Thailand, and use that convert datetime in my analysis. Later, I found that it is wrong and for the correct datetime I have to input tz as "GMT-7". I dont understand why.
as_datetime(1565923100)
#[1] "2019-08-16 02:38:20 UTC"
as_datetime(1565923100, tz = "gmt+7")
#[1] "2019-08-15 19:38:20 gmt"
as_datetime(1565923100, tz = "gmt-7")
#[1] "2019-08-16 09:38:20 gmt"
now()
#[1] "2019-08-16 10:16:17 +07"
From the code, the gmt-7 gives the correct time while I think I should use the gmt+7 because Bangkok timezone is gmt+7. I check using 'now' function and got the correct time too. I dont understand the logic behind the code. Thank you for any explanation.

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"

R Timezone from CEST (UTC+2) to UTC

Questions about timezone attributions on R.
From CEST (UTC+2) to UTC
There are some things that I do not get on R regarding the change of timezones.
Let's take an example.
datetime = as.POSIXct(paste(Sys.Date()-7,"00:00:00"), tz="CEST")
[1] "2018-04-04 CEST"
Let's recall that the timezone CEST stands for Central European Summer Time and is equivalent to UTC+2.
Knowing this, I want to assign the timezone UTC to my datetime. I am expecting to get "2018-04-03 22:00:00 UTC". Instead, this is what I get :
attr(datetime,"tzone") <- "UTC"
> datetime
[1] "2018-04-04 UTC"
Another thing is when I assign the timezone UTC+2 to my CEST datetime, I get the result I was looking for :
datetime2 = as.POSIXct(paste(Sys.Date()-7,"00:00:00"), tz="CEST")
[1] "2018-04-04 CEST"
attr(datetime2,"tzone") <- "UTC+2"
> datetime2
[1] "2018-04-03 22:00:00 UTC"
Though, since CEST is equivalent to UTC+2 I was expecting to get the result "2018-04-04 UTC".
Does anyone understand the logic here?
You can set the system time zone to UTC. Otherwise it will always automatically translate your time data to your local operating system time.
Sys.setenv(TZ='UTC')

How to convert from CEST to UTC?

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

Setting time zone in weatherData queries

I am using the weatherData package, specifically, its getDetailedWeather function. It returns a data frame, one of the component of the data frame is Time, of class POSIXct. My problem is that all the Time comes set to the local timezone of the machine I am using. I am pretty sure that this is incorrect, that the data reflects the local time, and the only thing the API does is add the timezone to the data, without changing it. Am I correct? How can I tell the API to stop using my timezone as default?
E.g.:
library(weatherData)
dat <- getDetailedWeather("NRT", "2014-04-29")
dat$Time
# [1] "2014-04-29 00:00:00 EST" ## local timezone, not of the weather station
Looking at the results of the example in ?getDetailedWeather:
library(weatherData)
dat <- getDetailedWeather("NRT", "2014-04-29")
dat$Time
# [1] "2014-04-29 00:00:00 EST" "2014-04-29 00:30:00 EST" "2014-04-29 01:00:00 EST" etc
The returned times seem to be 'correct', in that it goes from 00:00 to 23:30. The timezone for the data is not that of the weather station though, but rather of the host computer system. You may be best off just changing this output data once you have it, as R will always present date/time POSIXct objects in the local timezone by default, e.g.:
as.POSIXct(as.character(dat$Time),tz="UTC")
# [1] "2014-04-29 00:00:00 UTC" "2014-04-29 00:30:00 UTC" "2014-04-29 01:00:00 UTC" etc
The above changes the timezone to a new timezone (in this case "UTC", but you could use one appropriate for the weather station location) without affecting the time of day data. See here: Valid time zones in lubridate for identifying local timezone codes.

Remove an hour from my timestamp

I try to use the strftime function to make readable string from my timestamp. My problem is there is an hour that disapear each time I try to perform the transformation.
I'm in Belgium and it's summertime, so my linux is set to GMT+2
jt#jt-roxane:~$ date
lundi 2 juin 2014, 19:18:48 (UTC+0200)
jt#jt-roxane:~$ R
R version 2.14.1 (2011-12-22)
> Sys.time()
[1] "2014-06-02 19:19:46 CEST"
When my friends try to convert the timestamp 1401729132, it's correctly translated to
as.POSIXct(1401729132, origin="1970-01-01")
"2014-06-02 19:12:12 CEST"
But when I try to do it, the answer is
> as.POSIXct(1401729132, origin="1970-01-01")
[1] "2014-06-02 18:12:12 CEST"
I tried to set the right timezone, but it don't seem to solve the problem.
> strftime(as.POSIXct(1401729132, origin="1970-01-01"), '%H:%M:%S', tz="UTC-2")
[1] "18:12:12"
Do you have any idea about where it come from?
Thanks for any help!
Edit:
I've already tried to set the right TZ in as.POSIXct() - that line give the wrong answer :
> as.POSIXct(1401729132, origin="1970-01-01", tz="GMT")
[1] "2014-06-02 17:12:12 GMT"
Is your friend in the same time zone? Running the same OS?
You should set the timezone parameter (tz=) when you do the as.POSIXct, not when you do the strftime. When you create the date, it will use your local current time zone
as.POSIXct(1401729132, origin="1970-01-01", tz="GMT")
# "2014-06-02 17:12:12 GMT"
Read the ?Sys.timezone help page on how and why these can vary depending on operating system specific time zone data bases.

Resources