How to Parse Date/Time Strings with Abbreviated Timezone Names in Python?

Linda Hamilton
Release: 2024-10-24 08:58:30
Original
461 people have browsed it

How to Parse Date/Time Strings with Abbreviated Timezone Names in Python?

Parsing Date/Time Strings with Abbreviated Timezone Names in Python

One common task when working with date and time data is parsing strings that represent timestamps. These timestamps can often include abbreviated timezone names, which can make parsing them a challenge.

In Python, the dateutil library provides the parse() function for parsing date/time strings. However, by default, parse() does not recognize abbreviated timezone names.

Solution

To handle abbreviated timezone names, you can pass a dictionary of timezone abbreviations to the tzinfos parameter of parse(). This dictionary should map timezone abbreviations to their corresponding GMT offsets in seconds.

Here's how to create the timezone abbreviation dictionary:

<code class="python">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</code>
Copy after login

With the timezone abbreviation dictionary, you can then parse your date/time string:

<code class="python">import dateutil.parser as dp

s = 'Sat, 11/01/09 8:00PM EST'
dt = dp.parse(s, tzinfos=tzd)

print(dt)
# Output: 2009-11-01 20:00:00-05:00</code>
Copy after login

Note

It's important to note that timezone naming is not clearly defined, so some conflicts may arise when parsing abbreviated timezones. It's recommended to carefully consider the context and location of your data when interpreting the parsed timestamps.

The above is the detailed content of How to Parse Date/Time Strings with Abbreviated Timezone Names in Python?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template