3

There is something I don't understand. I simply try to convert a date in an other time zone. The date is in the 8601-ISO format. I followed this.

pb.txt <- "2012-09-11T21:23:20Z"
pb.date <- as.POSIXct(pb.txt, tz="UTC")
format(pb.date, tz="WEST",usetz=TRUE)
[1] "2012-09-11 WEST"

Why only the date appears and not anymore the timestamp ? I tried also :

pb.date <- as.POSIXct(pb.txt, origin=ISOdatetime(2012,09,11,21,23,20))
format(pb.date, tz="WEST",usetz=TRUE)
[1] "2012-09-10 22:00:00 WEST

It's better, but the timestamp is rounded. How to convert perfectly an 8601-ISO datetime ?

Community
  • 1
  • 1
jonathan
  • 149
  • 4
  • 11

2 Answers2

10

Use the correct format:

as.POSIXct(pb.txt, "%Y-%m-%dT%H:%M:%S", tz="UTC")
[1] "2012-09-11 21:23:20 UTC"
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • Ok, I get the same result : converting the date in POSIXct is ok. But print it in another time zone doesn'work : format(pb.date, tz="WEST",usetz=TRUE) >> [1] "2012-09-10 22:00:00 WEST". Nothing changes. – jonathan Mar 19 '13 at 15:52
  • @jonathan: `"WEST"` isn't a valid timezone. See `?timezone`. – Joshua Ulrich Mar 19 '13 at 16:12
  • Yes, you're right about that (as plannapus noticed it too). It's not one but two mistakes I've made ! Thanks very much for helping. :) – jonathan Mar 22 '13 at 14:25
1

In addition to @JoshuaUlrich answer, don't use daylight saving time timezone: use regular time zone, the system will convert automatically if the day chosen falls during summer time.

Given pb.date <- as.POSIXct(pb.txt, "%Y-%m-%dT%H:%M:%S", tz="UTC") as per Joshua's answer, this fails:

format(pb.date, tz="WEST",usetz=TRUE)
[1] "2012-09-11 21:23:20 UTC"

but this doesn't:

format(pb.date, tz="WET", usetz=TRUE)
[1] "2012-09-11 22:23:20 WEST"
plannapus
  • 18,529
  • 4
  • 72
  • 94
  • What I get is : > format(pb.date, tz="WET",usetz=TRUE) >> [1] "2012-09-10 23:00:00 WEST" and > format(pb.date, tz="WEST",usetz=TRUE) >> [1] "2012-09-10 22:00:00 WEST" So, nothing changes... :/ – jonathan Mar 19 '13 at 15:56
  • You did literally `pb.txt <- "2012-09-11T21:23:20Z"` followed by `pb.date <- as.POSIXct(pb.txt, "%Y-%m-%dT%H:%M:%S", tz="UTC")` and then `format(pb.date, tz="WET", usetz=TRUE)` gives you "2012-09-10 23:00:00 WEST"? – plannapus Mar 19 '13 at 16:08
  • You're right, it was weird. It seems that I've not correctly done something... I've checked today... and everything works great. Thanks very much. :) – jonathan Mar 22 '13 at 14:21