raphael.felber at agroscope.admin.ch
2014-Feb-14 08:23 UTC
[R] Problem with POSIXt time zone
Hello I have to convert character strings into POSIXt format. And would like to combine two of them. The following code does not what I expect. The single conversions of the character strings, gives a the date and time with time zone "GMT" as I expect. However if I combine two date time with c() the time zone is changed to CET.> strptime(paste("01.01.2013", "20:00:00"),format="%d.%m.%Y %H:%M:%S", tz="GMT")[1] "2013-01-01 20:00:00 GMT"> strptime(paste("02.01.2013", "01:00:00"),format="%d.%m.%Y %H:%M:%S", tz="GMT")[1] "2013-01-02 01:00:00 GMT"> c(strptime(paste("01.01.2013", "20:00:00"),format="%d.%m.%Y %H:%M:%S", tz="GMT"),+ strptime(paste("02.01.2013", "01:00:00"),format="%d.%m.%Y %H:%M:%S", tz="GMT")) [1] "2013-01-01 21:00:00 CET" "2013-01-02 02:00:00 CET" Is that a bug? How can I solve this problem? I really need the time in the time zone "GMT" else I run into troubles when the time changes to summer time. Thanks for any help. Kind regards Raphael Felber PhD Student Eidgenössisches Departement für Wirtschaft, Bildung und Forschung WBF Agroscope Institut für Nachhaltigkeitswissenschaften INH Klima und Lufthygiene Reckenholzstrasse 191, CH-8046 Zürich Tel. +41 44 377 75 11 Fax +41 44 377 72 01 raphael.felber@agroscope.admin.ch<mailto:raphael.felber@agroscope.admin.ch> www.agroscope.ch<http://www.agroscope.ch/> [[alternative HTML version deleted]]
Hi Raphael Bug or not that for others to say. Here is an explanation and a work around. This is on my Windows 8 laptop and R-3.0.2: ## strptime gives you POSIXlt objects (see ?DateTimeClasses or ?striptime)> t1 <- strptime(paste("01.01.2013", "20:00:00"),format="%d.%m.%Y %H:%M:%S", tz="GMT") > t2 <- strptime(paste("02.01.2013", "01:00:00"),format="%d.%m.%Y %H:%M:%S", tz="GMT")## c is probably doing some conversions. It does that for other classes of objects so be aware!> c(t1, t2)[1] "2013-01-01 21:00:00 CET" "2013-01-02 02:00:00 CET" ## see which methods there exists for c()> methods(c)[1] c.bibentry* c.Date c.noquote c.numeric_version [5] c.person* c.POSIXct c.POSIXlt Non-visible functions are asterisked ## since strptime gives POSIXlt this is called> c.POSIXltfunction (..., recursive = FALSE) as.POSIXlt(do.call("c", lapply(list(...), as.POSIXct))) <bytecode: 0x00000000061770e8> <environment: namespace:base> ## c.POSIXcl converts to POSIXct ## and so c.POISXct is called which does an unclass whereby information on time zone is lost ## the unclass gives us the number of second since 1st Janaury 1970> c.POSIXctfunction (..., recursive = FALSE) .POSIXct(c(unlist(lapply(list(...), unclass)))) <bytecode: 0x000000000617bd40> <environment: namespace:base> ## last in c.POSIXct we now call .POSIXct with tz set NULL by default. Is this a bug???? Since tz=NULL by default the time zone information is now read from your locale on your computer (that depends what is set by the OS. On our linux HPC it is set to UTC)> .POSIXctfunction (xx, tz = NULL) structure(xx, class = c("POSIXct", "POSIXt"), tzone = tz) <bytecode: 0x000000000aa25ad0> <environment: namespace:base> ## However when I am dealing with data with timestamps those are usually save in the "UTC" = "GMT" zone format because we get data from all over the world and we do not want to deal with day light saving times. So do this to begin with in the R session:> Sys.setenv(TZ="GMT")## and then we have:> c(t1, t2)[1] "2013-01-01 20:00:00 GMT" "2013-01-02 01:00:00 GMT">Yours sincerely / Med venlig hilsen Frede Aakmann T?gersen Specialist, M.Sc., Ph.D. Plant Performance & Modeling Technology & Service Solutions T +45 9730 5135 M +45 2547 6050 frtog at vestas.com http://www.vestas.com Company reg. name: Vestas Wind Systems A/S This e-mail is subject to our e-mail disclaimer statement. Please refer to www.vestas.com/legal/notice If you have received this e-mail in error please contact the sender.> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] > On Behalf Of raphael.felber at agroscope.admin.ch > Sent: 14. februar 2014 09:24 > To: r-help at r-project.org > Subject: [R] Problem with POSIXt time zone > > Hello > > I have to convert character strings into POSIXt format. And would like to > combine two of them. The following code does not what I expect. The single > conversions of the character strings, gives a the date and time with time zone > "GMT" as I expect. However if I combine two date time with c() the time > zone is changed to CET. > > > strptime(paste("01.01.2013", "20:00:00"),format="%d.%m.%Y %H:%M:%S", > tz="GMT") > [1] "2013-01-01 20:00:00 GMT" > > strptime(paste("02.01.2013", "01:00:00"),format="%d.%m.%Y %H:%M:%S", > tz="GMT") > [1] "2013-01-02 01:00:00 GMT" > > c(strptime(paste("01.01.2013", "20:00:00"),format="%d.%m.%Y > %H:%M:%S", tz="GMT"), > + strptime(paste("02.01.2013", "01:00:00"),format="%d.%m.%Y > %H:%M:%S", tz="GMT")) > [1] "2013-01-01 21:00:00 CET" "2013-01-02 02:00:00 CET" > > Is that a bug? How can I solve this problem? I really need the time in the time > zone "GMT" else I run into troubles when the time changes to summer time. > > Thanks for any help. > > Kind regards > > Raphael Felber > PhD Student > > Eidgen?ssisches Departement f?r > Wirtschaft, Bildung und Forschung WBF > Agroscope > Institut f?r Nachhaltigkeitswissenschaften INH > Klima und Lufthygiene > > Reckenholzstrasse 191, CH-8046 Z?rich > Tel. +41 44 377 75 11 > Fax +41 44 377 72 01 > raphael.felber at agroscope.admin.ch<mailto:raphael.felber at agroscope.admi > n.ch> > www.agroscope.ch<http://www.agroscope.ch/> > > > [[alternative HTML version deleted]]