Seconds since epoch to relative date

The method should return the relative date in something like: '2 months 22 days 04:38:47'

You can't do that, since a month is between 28 and 31 days long. The statement "2 months and 22 days" could mean anything between 81 and 84 days. (Or between 78 and 84 days, if the months doesn't have to be consecutive).

So what you want is simply nonsensical. A relative date time can only be counted in days, hours and seconds, until the difference becomes so big that the amount of days no longer matters, in which case you can start counting in months or years (but then you can't include days anymore).

So you can say "five years and two months", or "80 days and three hours", or "two hundred years". But you can not say "two months and three days" or "five years and 20 days". The statements simply make no sense.

Therefore, the correct answer is indeed eumiros

timedelta(seconds=6928727.56235)

But now you also know why.

(Unless of course, you with month actually mean moon-cycles, which do have a fixed length. :))


from datetime import timedelta

a = timedelta(seconds=6928727.56235)

# a is now datetime.timedelta(80, 16727, 562350)

print "%d days %02d:%02d:%02d" % (a.days, a.seconds / 3600, (a.seconds / 60) % 60, a.seconds % 60)

Returns 80 days 04:38:47, which is correct, but not exactly what OP wanted (80 days instead of 2 months 21 days).

Tags:

Python

Time

Epoch