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.
Related
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.
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"
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')
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
Very strange things happen while converting to POSIXct:
> as.POSIXct("2008-03-30 02:00:00",format="%Y-%m-%d %H:%M:%S")
[1] NA
but:
> as.POSIXct("2008-02-28 02:00:00",format="%Y-%m-%d %H:%M:%S")
[1] "2008-02-28 02:00:00 CET"
I am clueless. is it a bug in R?
Does it perhaps have to do with my German (Berlin) locale?
I am using R 2.14.2 for windows.
This is the beginning of daylight savings time in Germany in 2008, see this link. That time effectively is equivalent to "2008-03-30 03:00:00" (an hour later), which should work on your system. So, yes, this is related to your locale.