Search code examples
pythondatetimegmailutcstrptime

Convert date string (from gmail) to timestamp | Python


I want to save the received date of emails from a Gmail account into a time-series database.

The problem is that I cannot convert the string that I got from the email to timestamp.

I tried this:

from datetime import datetime

date1 = 'Thu, 28 May 2020 08:15:58 -0700 (PDT)' 

date1_obj = datetime.strptime(date1, '%a, %d %b %Y %H:%M:%S %z %Z')

print(date1_obj)

But got this error:

Traceback (most recent call last):
  File "/format_date.py", line 11, in <module>
    date1_obj = datetime.strptime(date1, '%a, %d %b %Y %H:%M:%S %z %Z')
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/_strptime.py", line 577, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/_strptime.py", line 359, in _strptime
    (data_string, format))
ValueError: time data 'Thu, 28 May 2020 08:15:58 -0700 (PDT)' does not match format '%a, %d %b %Y %H:%M:%S %z %Z'

Tried with or without parenthesis wrapping Timezone. Read a lot, but nothing about how to deal with date strings containing "(PDT)" or any other timezones. It's very important to get the right date... If I run the same code without "(PDT)", got an incorrect time (because of my local time).

I know I can use string methods to manipulate it and convert to a right datetime, but I feel like this would be flexible.

Sorry for my terrible English.

Thank you!


Solution

  • Well, after all your answers, which were very helpful, I finally solved.

    This is how:

    >>> from email.utils import parsedate_tz, mktime_tz
    >>> date = 'Thu, 28 May 2020 08:15:58 -0700 (PST)'
    >>> timestamp = mktime_tz(parsedate_tz(date))
    >>> timestamp
    1590678958
    >>>
    

    I checked that timestamp, and stands to 12:15:58 local time, what it's exactly what I was looking for.

    Thank you very much to everybody who took a minute to answer.