I'm using the Python datetime module, i.e.:
>>> import datetime >>> today = datetime.datetime.now() >>> print today 2009-03-06 13:24:58.857946
and I would like to compute the day of year that is sensitive of leap years. e.g. today (March 6, 2009) is the 65th day of 2009. Here's web-based DateTime calculator.
Anyway, I see a two options:
A. Create a number_of_days_in_month array = [31, 28, ...], decide if it's a leap year, manually sum up the days.
datetime.timedelta to make a guess & then binary search for the correct day of year:
>>> import datetime >>> YEAR = 2009 >>> DAY_OF_YEAR = 62 >>> d = datetime.date(YEAR, 1, 1) + datetime.timedelta(DAY_OF_YEAR - 1)
These both feel pretty clunky & I have a gut feeling that there's a more "Pythonic" way of calculating day of year. Any ideas/suggestions?