Reputation: 5082
I am using python's feedparser module, to parse an RSS feed. Once parsed, feedparser returns dates in a python 9 tuple time format (time.struct_time).
I am want to store these values in my mysql database so I can later check the Last-Modified headers of the feed. It's import that if the times tuples are converted, that when converted back they stay the same, so I can later use them for comparison.
I tried this to convert the time tuple to datetime and then back, but it wasn't the same when converted back:
dt = datetime.fromtimestamp(time.mktime(struct))
time_tuple = dt.timetuple()
What do you think is the method to do this?
Upvotes: 2
Views: 1464
Reputation: 880449
I believe the reason why your method is not preserving the time tuple is because the is_dst
value was changed. time.mktime
respected the is_dst
in struct
, but dt.timetuple
changes is_dst
to -1.
One way to avoid this error would be to interprets the time tuple as representing a UTC time. That may not be strictly correct, but it may be good enough for your purposes.
In [1]: import datetime as dt
In [2]: import time
In [3]: import calendar
In [17]: time_tuple=(1970, 1, 1, 0, 0, 0, 3, 1, 1)
In [18]: timestamp=calendar.timegm(time_tuple)
In [19]: timestamp
Out[19]: 0
In [20]: date=dt.datetime.utcfromtimestamp(timestamp)
In [21]: date
Out[21]: datetime.datetime(1970, 1, 1, 0, 0)
In [22]: tuple(date.timetuple())
Out[22]: (1970, 1, 1, 0, 0, 0, 3, 1, -1)
PS. Here is an example showing how the method you posted might fail to preserve the time tuple. Suppose the remote server is in a locale where is_dst = 1, while is_dst = 0 in your locale:
In [11]: time_tuple=(1970, 1, 1, 0, 0, 0, 3, 1, 1)
In [12]: timestamp=time.mktime(time_tuple)
In [13]: timestamp
Out[13]: 14400.0
In [14]: date=dt.datetime.fromtimestamp(timestamp)
In [15]: date
Out[15]: datetime.datetime(1969, 12, 31, 23, 0)
In [16]: tuple(date.timetuple())
Out[16]: (1969, 12, 31, 23, 0, 0, 2, 365, -1)
Upvotes: 1
Reputation: 799150
Store the datetime as UTC in the database, along with the timezone. Convert back to a local time when pulling from the database if needed.
Upvotes: 1