0

I am trying to parse a date string like this:

import datetime

document_updated_on = '01-Feb-2024#15:22 WET'
format_str = "%d-%b-%Y#%H:%M %Z"
updated_on_date = datetime.datetime.strptime(document_updated_on, format_str)

I am having issues with the time zone and getting the error:

ValueError: time data '01-Feb-2024#15:22 WET' does not match format '%d-%b-%Y#%H:%M %Z'

3 Answers 3

1

The %Z specifier is deprecated anyway, and WET (Western European Time) simply doesn't seem to be supported.

You can work around this with something like

import datetime
from zoneinfo import ZoneInfo


def parse_datetime(dt_str: str):
    if dt_str.endswith(" WET"):
        dt = datetime.datetime.strptime(dt_str.removesuffix(" WET"), "%d-%b-%Y#%H:%M")
        return dt.replace(tzinfo=ZoneInfo("WET"))
    return datetime.datetime.strptime(dt_str, "%d-%b-%Y#%H:%M %Z")


print(parse_datetime('01-Feb-2024#15:22 WET').timetuple())
print(parse_datetime('01-Feb-2024#15:22 EEST').timetuple())

This prints out

time.struct_time(tm_year=2024, tm_mon=2, tm_mday=1, tm_hour=15, tm_min=22, tm_sec=0, tm_wday=3, tm_yday=32, tm_isdst=0)
time.struct_time(tm_year=2024, tm_mon=2, tm_mday=1, tm_hour=15, tm_min=22, tm_sec=0, tm_wday=3, tm_yday=32, tm_isdst=-1)
Sign up to request clarification or add additional context in comments.

2 Comments

@FObersteiner erm, my answer is assigning the WET zoneinfo into the date afterwards..?
never mind, I assumed that WET is unsupported by zoneinfo, which is BS. There's also a WEST for the standard, non-DST time, and the IANA identifier to use seems to be Europe/Lisabon, according to eggert/tz, l.133 ff.
0

from my experience, you can either remove the time zone information, or create a Custom Parsing Function. To create the custom function it requires manual handling of time zones, which can be complex due to daylight saving time changes and other factors. For more robust timezone handling, consider using the pytz library or the zoneinfo module

It will look something like this:

from datetime import datetime, timedelta
import pytz

def parse_custom_date(date_str):
    # Splitting the string into date and time parts
    date_part, time_part_with_tz = date_str.split('#')
    
    # Parsing the date part
    date_obj = datetime.strptime(date_part, "%d-%b-%Y")
    
    # Parsing the time part and extracting the timezone
    time_part, tz = time_part_with_tz.split()
    time_obj = datetime.strptime(time_part, "%H:%M")
    
    # Adjusting the time_obj based on the timezone if necessary
    if tz == "WET":
        # Assuming WET is UTC+0 for simplicity; adjust as needed
        time_obj += timedelta(hours=0)
    
    # Combining the date and time objects
    updated_on_date = datetime.combine(date_obj.date(), time_obj.time())
    
    return updated_on_date

updated_on_str = '01-Feb-2024#15:22 WET'
updated_on_date = parse_custom_date(updated_on_str)

print(updated_on_date)

Comments

0

Using GMT instead of WET works, if this is a feasible solution.

Since GMT and WET are both UTC 0, this should work.

import datetime

document_updated_on = '01-Feb-2024#15:22 GMT'
format_str = "%d-%b-%Y#%H:%M %Z"
updated_on_date = datetime.datetime.strptime(document_updated_on, format_str)

If you can't use GMT as a substitute, you can take a look at the other solutions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.