Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seconds since epoch to relative date

Tags:

python

time

epoch

I'm working with dates since epoch, and already got, for example:

date = 6928727.56235

I'd like to transform this into another relative format, so that I'll be able to transform this into something relative to epoch.

Using time.gmtime(date), it returned

year=1970, mon=3, day=22, hour=4, min=38, sec=47

I think epoch starts in '01/01/1970 00:00:00', so the method should return the relative date in something like:

'2 months 21 days 04:38:47'

Anything that help?

like image 426
Gabriel L. Oliveira Avatar asked Jan 14 '11 17:01

Gabriel L. Oliveira


2 Answers

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. :))

like image 55
Lennart Regebro Avatar answered Sep 21 '22 12:09

Lennart Regebro


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).

like image 44
eumiro Avatar answered Sep 18 '22 12:09

eumiro