Problem with POSIXct time clock and POSIXct timeout truncation

I have the following R code snippet:

formatString = "%Y-%m-%d %H:%M:%OS"
x = as.POSIXct(strptime("2013-11-23 23:10:38.000000", formatString))
y = as.POSIXct(strptime("2015-07-17 01:43:38.000000", formatString))

      

I have a problem that when I do as.Date(y)

I get 2015-07-16

(although its date is next day!). Apparently the problem is with the timezone. So I checked the timezones:

> x
[1] "2013-11-23 23:10:38 CET"
> y
[1] "2015-07-17 01:43:38 CEST"
> 

      

Ok, so they deviate in their time zone. This is strange, because why does R decide that one timestamp (given without any time zone at all) lies in a different time zone than another (given without any time zone at all)?

Ok, so let's set the timezone. Googling has shown that it attr(y, "tzone") <- "CET"

should close the deal. Let's try this:

> attr(y, "tzone") <- "CET"
> y
[1] "2015-07-17 01:43:38 CEST"
> 

      

Ok, it didn't work. Let's see what the timezone is at the beginning:

> formatString = "%Y-%m-%d %H:%M:%OS"
> x = as.POSIXct(strptime("2013-11-23 23:10:38.000000", formatString))
> y = as.POSIXct(strptime("2015-07-17 01:43:38.000000", formatString))
> unclass(x)
[1] 1385244638
attr(,"tzone")
[1] ""
> unclass(y)
[1] 1437090218
attr(,"tzone")
[1] ""
> 

      

So ... they don't have a time zone at all, but their time zones are different?

-> here are my natural questions:

1) why are they initialized with a different timezone when I don't specify a timezone at all?

2) why do both objects seem to have no time zone and at the same time ... why do they have different time intervals?

3) How can I make it as.Date(y) == "2015-07-17"

true? That is, how can I set both to the current timezone? Sys.timezone()

results in "NA" ... (EDIT: my timezone [Germany] seems to be "CET" -> how can I set both for CET?)

I'm scratching my head here ... Thanks for any thoughts you share on this with me :-)

FW

+3


source to share


2 answers


If you do not specify a timezone, then R will use your system language, since POSIXct objects must have a timezone. The difference between CEST and CET is that one is summer and the other is not. This means that if you specify a date during the daylight saving time portion of the year, then R will decide to use the daylight saving time zone. If you want to set dates that don't use summer versions, then define them as GMT from the beginning.

formatString = "%Y-%m-%d %H:%M:%OS"
x = as.POSIXct(strptime("2013-11-23 23:10:38.000000", formatString), tz="GMT")
y = as.POSIXct(strptime("2015-07-17 01:43:38.000000", formatString), tz="GMT")

      

If you want to truncate time, do not use as.Date

POSIXct for object, as it is as.Date

intended to convert character objects to Date objects (which are not the same as POSIXct objects). If you want to truncate R-base POSIXct objects, you will have to wrap either round

in or trunc

in as.POSIXct

, but I would recommend checking out the package lubridate

for handling dates and times (specifically POSIXct objects).

If you want to keep CET, but never use CEST, you can use a location that doesn't take daylight savings into account. According to http://www.timeanddate.com/time/zones/cet , your options are only Algeria and Tunisia. According to https://en.wikipedia.org/wiki/List_of_tz_database_time_zones the valid tz is "Africa / Algeria". Therefore you can do



 formatString = "%Y-%m-%d %H:%M:%OS"
x = as.POSIXct(strptime("2013-11-23 23:10:38.000000", formatString), tz="Africa/Algiers")
y = as.POSIXct(strptime("2015-07-17 01:43:38.000000", formatString), tz="Africa/Algiers")

      

and both x and y will be in CET.

One more thing about setting time zones. If you say R you want a general time zone then it will not override daytime settings. Therefore, the installation attr(y, "tzone") <- "CET"

did not have the desired result. If you did attr(y, "tzone") <- "Africa/Algiers"

, then it would work as you expected. Be careful with conversions because when you change the time zone it will change the time to account for the new time zone. The package lubridate

has a feature force_tz

that changes the time zone without changing the time for cases where the initial time zone setting was incorrect but the time was correct.

+2


source


Complementary answer:

1) Just use the correct timezone from the beginning. Since I live in Hamburg Germany, the appropriate time zone for me is Europe / Berlin, see this list as Dean said.

2) To extract information from POSIXct like dates, I use



as.Date(format(timeStamp, "%Y-%m-%d"))

      

which is slow, but seems to give the correct answer ... plus I don't have to install new packages [it's a bit tricky in my situation].

+1


source







All Articles