python - Convert epoch/unix time into readable format -


in python if to

import time print(time.time()+40) 

it give epoch/unix time 40 seconds but, how make this

import time def difference(x):     time = time.time()+x     difference = time-time.time()     return difference 

now, works great until higher numbers 60 seconds(one minute) , on large scale year can confusing, there method have turn minutes or hours,days,years in simple method can return this?

return "that time %s years %s days %s hours %s minutes , %s seconds now" 

i think of way it, i'm afraid way i'm doing result in large code using alot of if/else's , divmods. in advance.

this same place stopped using time , started using datetime.

from datetime import tzinfo, timedelta, datetime = datetime.utcnow() later = - timedelta(seconds=40)   >>> datetime.datetime(2013, 10, 7, 15, 9, 5, 903000) >>> print 2013-10-07 15:09:05.903000 >>> print later 2013-10-07 15:08:25.903000 

so can subtract difference.

now = datetime.utcnow() later = + timedelta(weeks=78, minutes=85, seconds=128) diff = later - 

unfortunately datetime doesn't translate days months/years. assume because have figure in leapyears , months between 2 dates.

>>> print diff 546 days, 1:27:08 




extra info:

if start getting other timezones, gets little more complicated need provide timezone object. here simple version:

################################################################################ #class definition est timezone since python doesn't have 1 class est(tzinfo):     def utcoffset(self, dt):         return timedelta(-5)      def tzname(self, dt):         return "est"      def dst(self, dt):         return timedelta(0)  ################################################################################ #class definition cst timezone since python doesn't have 1 class cst(tzinfo):     def utcoffset(self, dt):         return timedelta(hours=-6)      def tzname(self, dt):         return "cst"      def dst(self, dt):         return timedelta(0)   nowcst = datetime.now(tz=cst()) nowest = now.replace(tzinfo(est()) 

using timezones should take daylight savings account.
*example code can found in python docs - http://docs.python.org/2/library/datetime.html#tzinfo-objects


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -