0

I am not sure what is wrong with the formatting of %a %b %d %H:%M:%S %Z %Y I have attached the code and error message below. Code:

print(data[1][0])
t1 = datetime.strptime(data[1][0], "%a %b %d %H:%M:%S %Z %Y")

Error Message:

Fri Jul 08 12:02:39 PDT 2022
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/tmp/ipykernel_116/256248471.py in <module>
      2 
      3 print(data[1][0])
----> 4 t1 = datetime.strptime(data[1][0], "%a %b %d %H:%M:%S %Z %Y")

/opt/conda/lib/python3.9/_strptime.py in _strptime_datetime(cls, data_string, format)
    566     """Return a class cls instance based on the input string and the
    567     format string."""
--> 568     tt, fraction, gmtoff_fraction = _strptime(data_string, format)
    569     tzname, gmtoff = tt[-2:]
    570     args = tt[:6] + (fraction,)

/opt/conda/lib/python3.9/_strptime.py in _strptime(data_string, format)
    347     found = format_regex.match(data_string)
    348     if not found:
--> 349         raise ValueError("time data %r does not match format %r" %
    350                          (data_string, format))
    351     if len(data_string) != found.end():

ValueError: time data 'Fri Jul 08 12:02:39 PDT 2022' does not match format '%a %b %d %H:%M:%S %Z %Y'
  • `%a` and `%b` depend on locale; maybe your current locale uses a different format for the weekday or month name? – 0x5453 Jul 18 '22 at 21:10
  • 1
    Also, the issue might be in `%Z` (it accepts only`(empty), UTC, GMT`) https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes – Andrej Kesely Jul 18 '22 at 21:19

1 Answers1

0

You are getting this error because for %Z it is expecting GMT or UTC as per the documentation. It can be either one from these (empty), UTC, GMT as per this table https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes. But in your date string Fri Jul 08 12:02:39 PDT 2022, in this exception message ValueError: time data 'Fri Jul 08 12:02:39 PDT 2022' does not match format '%a %b %d %H:%M:%S %Z %Y' you are getting PDT. That's why you are getting this error. To convert the date string containing PDT you can follow this discussion Python : Converting time string with different timezone to host timezone datetime object?

Md Johirul Islam
  • 5,042
  • 4
  • 23
  • 56