Hello StackOverflow Python community!
I want to generate Unix timestamps for the start and end of a month. I'm looking for a way to retrieve the last day (or second, or minute...) in an arbitrary month in Python.
Given datetime.date
values for two consecutive months, I can get to the last value in one month by subtracting a delta value from the next like this:
import datetime
# dates without time
jan = datetime.date(2019, 1, 1)
feb = datetime.date(2019, 2, 1)
# start/end times
start_time = datetime.time(0, 0, 0, 0, tzinfo=datetime.timezone.utc)
end_time = datetime.time(23, 59, 59, 999, tzinfo=datetime.timezone.utc)
jan_start = datetime.datetime.combine(jan, start_time)
last_day = feb - datetime.timedelta (days = 1)
jan_end = datetime.datetime.combine(last_day, end_time)
do_stuff(jan_start, jan_end)
But my minimally-python-literate self is stumbling trying to find a way to plug arbitrary months in and get the same results. I'd like to end up with something like:
import datetime
# start/end times
start_time = datetime.time(0, 0, 0, 0, tzinfo=datetime.timezone.utc)
end_time = datetime.time(23, 59, 59, 999, tzinfo=datetime.timezone.utc)
dates = {
"Jan19": datetime.date(2019, 1, 1),
"Feb19": datetime.date(2019, 2, 1),
"Mar19": datetime.date(2019, 3, 1),
"Apr19": datetime.date(2019, 4, 1)
}
for i in dates:
start = datetime.datetime.combine(dates[i], start_time)
end_day = dates[i].next() - datetime.timedelta (days = 1)
end = datetime.datetime.combine(end_day, end_time)
do_stuff(start, end)
Only, whoops - Python dictionaries are unordered, so there isn't a next() method I can call on i
!
It's possible the conventional way to do this is to use midnight on February 1st as equivalent to the last second in January 31st, but I'm curious as to how one can retrieve the last day (or second, or minute...) in an arbitrary month in Python.
Would I need to use an OrderedList, or is there some module out there immune to my googling that has a month.max()
property I could use instead of working it out dynamically?