Question

I have a little problem parsing a date in python

This is the date I have to parse:

Sun Sep 15, 2013 12:10pm EDT

And that is the code I'm using to parse it:

datetime.strptime( date, "%a %b %d, %Y %I:%M%p %Z")

Everything is fine but the time-zone parsing, which always return a ValueError exception. I've also tried pytz but without any success.

So how can i parse this kind date using python?

Was it helpful?

Solution

Using dateutil:

import dateutil.parser
import pytz

tz_str = '''-12 Y
-11 X NUT SST
-10 W CKT HAST HST TAHT TKT
-9 V AKST GAMT GIT HADT HNY
-8 U AKDT CIST HAY HNP PST PT
-7 T HAP HNR MST PDT
-6 S CST EAST GALT HAR HNC MDT
-5 R CDT COT EASST ECT EST ET HAC HNE PET
-4 Q AST BOT CLT COST EDT FKT GYT HAE HNA PYT
-3 P ADT ART BRT CLST FKST GFT HAA PMST PYST SRT UYT WGT
-2 O BRST FNT PMDT UYST WGST
-1 N AZOT CVT EGT
0 Z EGST GMT UTC WET WT
1 A CET DFT WAT WEDT WEST
2 B CAT CEDT CEST EET SAST WAST
3 C EAT EEDT EEST IDT MSK
4 D AMT AZT GET GST KUYT MSD MUT RET SAMT SCT
5 E AMST AQTT AZST HMT MAWT MVT PKT TFT TJT TMT UZT YEKT
6 F ALMT BIOT BTT IOT KGT NOVT OMST YEKST
7 G CXT DAVT HOVT ICT KRAT NOVST OMSST THA WIB
8 H ACT AWST BDT BNT CAST HKT IRKT KRAST MYT PHT SGT ULAT WITA WST
9 I AWDT IRKST JST KST PWT TLT WDT WIT YAKT
10 K AEST ChST PGT VLAT YAKST YAPT
11 L AEDT LHDT MAGT NCT PONT SBT VLAST VUT
12 M ANAST ANAT FJT GILT MAGST MHT NZST PETST PETT TVT WFT
13 FJST NZDT
11.5 NFT
10.5 ACDT LHST
9.5 ACST
6.5 CCT MMT
5.75 NPT
5.5 SLT
4.5 AFT IRDT
3.5 IRST
-2.5 HAT NDT
-3.5 HNT NST NT
-4.5 HLV VET
-9.5 MART MIT'''

tzd = {}
for tz_descr in map(str.split, tz_str.split('\n')):
    tz_offset = int(float(tz_descr[0]) * 3600)
    for tz_code in tz_descr[1:]:
        tzd[tz_code] = tz_offset

date = 'Sun Sep 15, 2013 12:10pm EDT'
dateutil.parser.parse(date, tzinfos=tzd) # => datetime.datetime(2013, 9, 15, 12, 10, tzinfo=tzoffset(u'EDT', -14400))

tzd generation code comes from this answer.

UPDATE

NOTE The list of time zone abbreviations is not accurate as Matt Johnson commented. See his answer.

OTHER TIPS

You can't. Not reliably anyway. Time zone abbreviations are ambiguous and contradictory. There are no standards.

For example "CST" has 5 distinctly different meanings.

  • (UTC-06:00) Central Standard Time (America)
  • (UTC-05:00) Cuba Standard Time
  • (UTC+08:00) China Standard Time
  • (UTC+09:30) Central Standard Time (Australia)
  • (UTC+10:30) Central Summer Time (Australia)

See this list for additional examples.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top