0

In my file I have columns of date in the ISO 8061 format with timezone information, here is the sample input

0001-01-01T00:00:00-05:00
2009-01-29T07:00:00-05:00
2009-01-29T13:00:00-05:00
2009-01-29T18:55:00-05:00
2010-01-22T12:00:00-05:00
2010-07-20T00:25:00-04:00
2010-08-14T00:30:00-04:00
2011-06-19T08:25:00.41683-04:00

so the format is YYYY-MM-DDTHH:MM:SS[f]-AB:CD

I want to know how I can format the given data to format required by RFC 2445 iCalendar specification [DTSTART], which are example :

19970714T133000                    ;Local time
19970714T173000Z                   ;UTC time
TZID=US-Eastern:19970714T133000    ;Local time and time

I currently have following code for just parsing

>>> import dateutil.parser
>>> import dateutil
>>> import datetime
>>> dateutil.parser.parse('2009-01-29T07:00:00-05:00')
datetime.datetime(2009, 1, 29, 7, 0, tzinfo=tzoffset(None, -18000))

I want to know how to identify time zone ['-04:00','-05:00'] and how to convert them into UTC.

Thank you.

Jenn Cole
  • 99
  • 1
  • 2
  • 10
  • I am a little confused.dateutil has already parsed timezone info for you. tzinfo=tzoffset(None, -18000). an hour has 3600s. -5:00 is -18000s – Leonardo.Z Oct 30 '13 at 15:38
  • @Leonardo.Z so if I just subtract 5 hours from the original datetime, I would get UTC datetime ? – Jenn Cole Oct 30 '13 at 15:45
  • No, you should not do the subtract manually. `dt = dateutil.parser.parse('2009-01-29T07:00:00-05:00')`, datatime object has a method to do the timezone conversion: `dt.astimezone(pytz.utc)` – Leonardo.Z Oct 30 '13 at 15:55
  • @Leonardo.Z Thanks Leo :) Hope you have a relaxing day :) – Jenn Cole Oct 30 '13 at 16:07
  • Leonardo is right. Besides, you would *add* 5 hours to get back to UTC. The offset means that the time is 5 hours *behind* UTC. Also recognize that just because you have -5 or -4 doesn't mean you can know that this is US Eastern Time. – Matt Johnson-Pint Oct 30 '13 at 16:11
  • Given that `-05:00` maps to several different timezones, and that set of timezones can differ based on the time of year (due to daylight savings time), this is not likely to give you great results, but it depends on how picky you are... `-05:00` could be US/Eastern, but it is also US/Central during part of the year (during which US/Eastern is `-04:00`)... But maybe you should be looking at the central or south American zones, or ...... ? – twalberg Oct 30 '13 at 16:27
  • @twalberg There is also one hour per year where US Eastern and Central are *both* at -5. [See here for details](http://www.timeanddate.com/worldclock/converted.html?iso=20131103T01&p1=64&p2=179). – Matt Johnson-Pint Oct 30 '13 at 16:34
  • @MattJohnson Yep... Probably also one hour where neither are at -5... In general time zone handling is tricky... – twalberg Oct 30 '13 at 16:36
  • @twalberg - Yes. During the spring-forward transition, they are two hours apart. One is -4 and the other is -6. [Details here](http://www.timeanddate.com/worldclock/converted.html?iso=20130310T01&p1=64&p2=179) – Matt Johnson-Pint Oct 30 '13 at 16:41

0 Answers0