r - Trouble dealing with POSIXct timezones and truncating the time out of POSIXct objects -


i have following piece of r-code:

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 problem when as.date(y) 2015-07-16 (although date 1 day later!). apparently problem timezone. checked timezones:

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

ok, deviate in timezone. weird, because why r decide 1 timestamp (given without timezone @ all) lies in different timezone (given without timezone @ all)?

ok, lets set timezone. googling revealed attr(y, "tzone") <- "cet" should deal. lets try this:

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

ok, did not work. let see timezone in 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... dont have timezone @ timezones different????

--> here natural questions:

1) why initialized different timezone when dont specify timezone @ all?

2) why both objects apparently not have timezone , @ same time... how come have different timezones?

3) how can make as.date(y) == "2015-07-17" true? i.e. how can set both current timezone? sys.timezone() results in 'na'... (edit: timezone [germany] seems "cet" --> how can set both cet?)

i'm scratching head here... thoughts on share me :-)

fw

if don't specify timezone r use system's locale posixct objects must have timezone. difference between cest , cet 1 summertime , 1 not. means if define date during part of year defined summertime r decide use summertime version of timezone. if want set dates don't use summertime versions define them gmt 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 want truncate out time, don't use as.date on posixct object since as.date meant convert character objects date objects (which aren't same posixct objects). if want truncate posixct objects base r you'll have wrap either round or trunc in as.posixct recommend checking out lubridate package dealing dates , times (specifically posixct objects).

if want keep cet never use cest can use location doesn't observe daylight savings. according http://www.timeanddate.com/time/zones/cet options algeria , tunisia. according https://en.wikipedia.org/wiki/list_of_tz_database_time_zones valid tz "africa/algiers". therefore

 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 , y in cet.

one more thing setting timezones. if tell r want generic timezone won't override daylight savings settings. that's why setting attr(y, "tzone") <- "cet" didn't have desired result. if did attr(y, "tzone") <- "africa/algiers" have worked expected. careful conversions though because when change timezone change time account new timezone. package lubridate has function force_tz changes timezone without changing time cases initial timezone setting wrong time right.


Comments