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?
I believe the reason why your method is not preserving the time tuple is because the
is_dstvalue was changed.time.mktimerespected theis_dstinstruct, butdt.timetuplechangesis_dstto -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.
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: