I've been writing a time converter to take the systems time_t and convert it into human readable date/time. Oh, and this is my second python script ever. We'll leave that fact aside and move on.
The full script is hosted here.
Writing converters for the year and month were fairly easy, but I've hit a serious brick wall trying to get the day working. As you can see, I've been trying to brute-force my way all the way from 1970 to today. Unfortunately, the day comes out as -105.
Does anyone know of a better way to do it, or a way to fix up what I have attempted here? It's currently 3:30 AM, so it's quite possible I'm missing something obvious.
Sorry, I forgot to note that I'm doing this manually in order to learn python. Doing it via date functions defeats the purpose, unfortunately.
(I'm assuming you do this to learn Python, so I'll point out the errors in your code).
>>> years = SecondsSinceEpoch / 31540000
Nonononono. You can't do that. Some years have 31536000 seconds, others have 31622400 seconds.
>>> if calendar.isleap(yeariterator) == True:
You don't need to test if a true value is true. :-) Do:
>>> if calendar.isleap(yeariterator):
Instead.
Also change:
>>> yeariterator = 1969
>>> iterator = 0
>>> while yeariterator < yearsfordayfunction:
>>> yeariterator = yeariterator + 1
To:
for yeariterator in range(1970, yearsfordayfunction):
That will also fix your error: You don't stop until AFTER 2009, so you get the answer -105, because there is 105 days left of the year.
And also, there's not much point in calculating month by month. Year by year works fine.
for yeariterator in range(1970, yearsfordayfunction):
if calendar.isleap(yeariterator) == True:
days = days - 366
else:
days = days - 365
And an indent of 8 spaces is a lot. 4 is more common.
Also, I'd calculate year and day of year in one method, instead of doing it twice.
def YearDay():
SecondsSinceEpoch = int(time.time())
days = SecondsSinceEpoch // 86400 # Double slash means floored int.
year = 1970
while True:
if calendar.isleap(year):
days -= 366
else:
days -= 365
year += 1
if calendar.isleap(year):
if days <= 366:
return year, days
else:
if days <= 365:
return year, days
def MonthDay(year, day):
if calendar.isleap(year):
monthstarts = [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366]
else:
monthstarts = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365]
month = 0
for start in monthstarts:
if start > day:
return month, day - monthstarts[month-1] + 1
month += 1