Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a datetime.date object into a time.struct_time object?

I have a python script which I need to compare two dates. I have a list dates as time.struct_time objects which I need to compare to a few datetime.date objects.

How do I convert the datetime.date objects into a time.struct_time objects? Or can I just use them as is for comparison?

like image 885
Incognito Avatar asked Apr 14 '11 00:04

Incognito


1 Answers

Try using date.timetuple(). From the Python docs:

Return a time.struct_time such as returned by time.localtime(). The hours, minutes and seconds are 0, and the DST flag is -1. d.timetuple() is equivalent to time.struct_time((d.year, d.month, d.day, 0, 0, 0, d.weekday(), yday, -1)), where yday = d.toordinal() - date(d.year, 1, 1).toordinal() + 1 is the day number within the current year starting with 1 for January 1st.

like image 171
Lucas Jones Avatar answered Sep 17 '22 20:09

Lucas Jones