[R] Incorrect DateTime using ISOdatetime in R

Luke Miller millerlp at gmail.com
Sat Jan 21 00:09:30 CET 2012


You're running into time zone issues. If you're in a time zone that
recognizes daylight savings time for part of the year, the difference
in the value you calculate and the expected answer will vary.

Check your current system timezone:

Sys.timezone()

If it doesn't return "UTC" (aka GMT), you're going to run into issues.

For instance, if I set my system timezone to CET (for Germany), I can
recreate your problem.

Sys.setenv(TZ = "CET")

> myTime = as.POSIXct(strptime('2010-10-29 6:12:09', format = '%Y-%m-%d %H:%M:%S'))
> myTime
[1] "2010-10-29 06:12:09 CEST"

> myTime2 = (unclass(myTime) - (unclass(ISOdatetime(2009,1,1,0,0,0, tz = "GMT")))) / (24*3600)
> myTime2
[1] 666.1751
attr(,"tzone")
[1] "CET"

> myTime3 = ISOdatetime(2009,1,1,0,0,0, tz = "GMT") + (myTime2*24*3600)
Warning message:
In check_tzones(e1, e2) : 'tzone' attributes are inconsistent
> myTime3
[1] "2010-10-29 04:12:09 GMT"

Note that 'myTime' was created in the CEST timezone. However, myTime2
is created by subtracting
myTime from an ISOdatetime set in the GMT timezone, resulting in the
666.1751 value. That value is almost certainly incorrect for use in
your first pass analysis. When I then try to convert myTime2 back into
a human-readable format, myTime3, the returned value is off by 2
hours, as you've found.

If you change the tz option in the ISOdatetime function to CET, you
should get the correct values back out. For example:

> myTime4 = (unclass(myTime) - (unclass(ISOdatetime(2009,1,1,0,0,0, tz = "CET")))) / (24*3600)
> myTime4
[1] 666.2168
attr(,"tzone")
[1] "CET"

> myTime5 = ISOdatetime(2009,1,1,0,0,0, tz = "CET") + (myTime4*24*3600)
> myTime5
[1] "2010-10-29 06:12:09 CEST"

When I keep everything in CET/CEST timezone with myTime4 and myTime5,
I get back the correct 6:12:09 time stamp.

However, if you intend to only be working on time stamps that are in
the GMT/UTC time zone, then the easiest solution may be to set your
system timezone (for the current R session) to UTC:

Sys.setenv(TZ='UTC')   # This is only active for the current R session

After doing that, I can great a time stamp, without an explicit time
zone, and R automatically sticks it into the UTC time zone.

> myTime6 = as.POSIXct(strptime('2010-10-29 6:12:09', format = '%Y-%m-%d %H:%M:%S'))
> myTime6
[1] "2010-10-29 06:12:09 UTC"

> myTime7 = (unclass(myTime6) - (unclass(ISOdatetime(2009,1,1,0,0,0, tz ="UTC")))) / (24*3600)
> myTime7
[1] 666.2584
attr(,"tzone")
[1] "UTC"

> myTime8 = ISOdatetime(2009,1,1,0,0,0, tz = "UTC") + (myTime7*24*3600)
> myTime8
[1] "2010-10-29 06:12:09 UTC"

That should cure your problems. You should re-run all of your FPT
analyses, since you may have been feeding them incorrect time values
all along.

-Luke




On Fri, Jan 20, 2012 at 11:24 AM, Julia Sommerfeld <julia.somma at gmx.de> wrote:
> Hi Luke,
>
> Thank you for the answer.
>
> On 20/01/2012, at 6:11 , Luke Miller wrote:
>
> 666.1751 sure seems like it should return 2010-10-29 04:12:09 based on
> your example.
>
> 666.1751 days from 2009-01-01 is 2010-10-29 + some hours/min/seconds.
>
> 0.1751 days * 24 hrs/day = 4.2024 (i.e. 4:00AM + some minutes).
>
> 0.2024 hours * 60 min/hr = 12.144 (i.e. 12 minutes + some seconds).
>
> 0.144 minutes * 60 sec/min = 8.64 (i.e. 8.64 seconds).
>
> Put it all together and I get 2010-10-29 04:12:08.64 in the GMT time zone.
>
> What makes you think it should be 2010-10-29 06:12:09?
>
>
> The first fix of the original GPS data is at 06:12:09. So maybe the error is
> somewhere else?
>
> I'm calculating first passage time in R. To run FPT we had to transform
> DateTime.
>
> 1.
> #paste Date and Time into one column DateTime
> DT<-paste(GPS$Date, GPS$Time, sep=" ")
>
> GPS$DateTime<-as.POSIXct(strptime(as.character(DT), "%d/%m/%Y %H:%M:%S"))
>
> #paste it together so that it is in the right format for FPT analysis
> loc <- GPS[GPS$Bird==bird,c("LON", "LAT", "DateTime", "Bird")]
> colnames(loc) <- c("Long", "Lat", "gmt", "bird")
>
> 2. ##put date in format that FPT likes
> loc$Date <- (unclass(loc$gmt) - unclass(ISOdatetime(2009, 1, 1, 0, 0, 0, tz
> = "GMT")))/(24*3600)
>
> 3. After running FPT, the date time needs to be transformed back
> from "666.1751" into "yyyy/mm/dd hh:mm:ss"
>
> And I used this function:
>
> d$Date <- ISOdatetime(2009, 1, 1, 0, 0, 0, tz = "GMT")+d$Date*(24*3600)
>
> where "d" is the FPT output file (previously "loc").
>
> THANKS, Julia
>
>
>
> Are you running
> into a time zone issue, such as your GPS adjusting its time zone and
> reported the time based on the time zone where a reading was taken? If
> you crossed between time zones for some of your readings it might
> explain the fluctuating difference between the answer you expect and
> the answer that ISOdatetime gives you.
>
> -Luke
>
>
> On Fri, Jan 20, 2012 at 6:59 AM, Sula2011 <julia.somma at gmx.de> wrote:
>
>
> Dear list,
>
>
> I need to transform the DateTime of my GPS data from:
>
>
> "666.1751" into "yyyy/mm/dd hh:mm:ss"
>
>
> I have the following code:
>
>
> d$Date <- ISOdatetime(2009, 1, 1, 0, 0, 0, tz = "GMT")+d$Date*(24*3600)
>
>
> This gives me: 2010-10-29 04:12:09, which is wrong. It should be 2010-10-29
>
> 06:12:09
>
>
> Another example:
>
>
> 418.3219 corresponds to: 2010-02-23 07:43:30, but it should be 2010-02-23
>
> 08:43:30.
>
>
> However, not always is the difference + 2 h, it's sometimes less or more.
>
>
> There are a lot of postings here regarding ISOdatetime, but I'm still not
>
> able to solve this . Any ideas or suggestions will be very much appreciated.
>
>
> Best regards,
>
>
> Julia
>
>
> PS. I've tried to find the answer in all sorts of R help forums and also in
>
> my R books - no luck so far. Maybe I'm missunderstanding the entire
>
> ISOdatetime function?
>
>
> --
>
> View this message in context:
> http://r.789695.n4.nabble.com/Incorrect-DateTime-using-ISOdatetime-in-R-tp4313470p4313470.html
>
> Sent from the R help mailing list archive at Nabble.com.
>
>
> ______________________________________________
>
> R-help at r-project.org mailing list
>
> https://stat.ethz.ch/mailman/listinfo/r-help
>
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>
> and provide commented, minimal, self-contained, reproducible code.
>
>
> Julia Sommerfeld - PhD Candidate
> Institute for Marine and Antarctic Studies
> University of Tasmania
> Private Bag 129, Hobart
> TAS 7001
>
> Phone: +61 477 289 301
> Email: julia.somma at gmx.de
> Julia.Sommerfeld at utas.edu.au
>



More information about the R-help mailing list