在Python中解析带有时区缩写名称的日期时间字符串?

在Python中解析带有时区缩写名称的日期时间字符串?,第1张

在Python中解析带有时区缩写名称的日期/时间字符串?

dateutil的

parser.parse()
接受作为关键字参数tzinfos的类型的字典
{'EST': -5*3600}
(即,区域名称匹配GMT以秒偏移量)。因此,假设我们有,我们可以这样做:

>>> import dateutil.parser as dp>>> s = 'Sat, 11/01/09 8:00PM'>>> for tz_pre in ('PST','PDT','MST','MDT','CST','CDT','EST','EDT'):>>>     dt = s+' '+tz_pre>>>     print dt, '=', dp.parse(dt, tzinfos=tzd)Sat, 11/01/09 8:00PM PST = 2009-11-01 20:00:00-08:00Sat, 11/01/09 8:00PM PDT = 2009-11-01 20:00:00-07:00Sat, 11/01/09 8:00PM MST = 2009-11-01 20:00:00-07:00Sat, 11/01/09 8:00PM MDT = 2009-11-01 20:00:00-06:00Sat, 11/01/09 8:00PM CST = 2009-11-01 20:00:00-06:00Sat, 11/01/09 8:00PM CDT = 2009-11-01 20:00:00-05:00Sat, 11/01/09 8:00PM EST = 2009-11-01 20:00:00-05:00Sat, 11/01/09 8:00PM EDT = 2009-11-01 20:00:00-04:00

关于的内容

tzinfos
,这是我如何填充我的内容:

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 EGT0 Z EGST GMT UTC WET WT1 A CET DFT WAT WEDT WEST2 B CAT CEDT CEST EET SAST WAST3 C EAT EEDT EEST IDT MSK4 D AMT AZT GET GST KUYT MSD MUT RET SAMT SCT5 E AMST AQTT AZST HMT MAWT MVT PKT TFT TJT TMT UZT YEKT6 F ALMT BIOT BTT IOT KGT NOVT OMST YEKST7 G CXT DAVT HOVT ICT KRAT NOVST OMSST THA WIB8 H ACT AWST BDT BNT CAST HKT IRKT KRAST MYT PHT SGT ULAT WITA WST9 I AWDT IRKST JST KST PWT TLT WDT WIT YAKT10 K AEST ChST PGT VLAT YAKST YAPT11 L AEDT LHDT MAGT NCT PONT SBT VLAST VUT12 M ANAST ANAT FJT GILT MAGST MHT NZST PETST PETT TVT WFT13 FJST NZDT11.5 NFT10.5 ACDT LHST9.5 ACST6.5 CCT MMT5.75 NPT5.5 SLT4.5 AFT IRDT3.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_pre in tz_descr[1:]:        tzd[tz_pre] = tz_offset


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5620690.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存