From mars.lenjoy at gmail.com Tue Aug 23 15:39:47 2016 From: mars.lenjoy at gmail.com (Lenjoy) Date: Tue, 23 Aug 2016 12:39:47 -0700 Subject: [Datetime-SIG] local and utc time/date in python Message-ID: Hi, Python date time experts, summary for getting UTC time in sec: print time.time() print calendar.timegm(time.gmtime()) print time.mktime(time.localtime()) print calendar.timegm(datetime.datetime.utcnow().timetuple()) print time.mktime(datetime.datetime.now().timetuple()) Are all the functions above expected? See code below: ################################################### """Try the time date util for local and gm. Usage: $ date -u +%s && python aa.py 1471980937 * time: time.struct_time(tm_year=2016, tm_mon=8, tm_mday=23, tm_hour=19, tm_min=35, tm_sec=37, tm_wday=1, tm_yday=236, tm_isdst=0) time.gmtime() 1472009737.0 time.mktime 1471980937 calendar.timegm <-- right time.struct_time(tm_year=2016, tm_mon=8, tm_mday=23, tm_hour=12, tm_min=35, tm_sec=37, tm_wday=1, tm_yday=236, tm_isdst=1) time.localtime() 1471980937.0 time.mktime <-- right 1471955737 calendar.timegm * datetime.utcnow(): time.struct_time(tm_year=2016, tm_mon=8, tm_mday=23, tm_hour=19, tm_min=35, tm_sec=37, tm_wday=1, tm_yday=236, tm_isdst=-1) datetime.utcnow() 1472006137.0 time.mktime 1471980937 calendar.timegm <-- right time.struct_time(tm_year=2016, tm_mon=8, tm_mday=23, tm_hour=12, tm_min=35, tm_sec=37, tm_wday=1, tm_yday=236, tm_isdst=-1) datetime.now() 1471980937.0 time.mktime <-- right 1471955737 calendar.timegm * summary for getting UTC time in sec: 1471980937.24 1471980937 1471980937.0 1471980937 1471980937.0 """ import calendar import datetime import time def to_timestamp(dt): return calendar.timegm(dt.timetuple()) def to_timestamp2(dt): return time.mktime(dt.timetuple()) print '\n* time:' gmt = time.gmtime() localt = time.localtime() utc_now = datetime.datetime.utcnow() local_now = datetime.datetime.now() print gmt, '\t time.gmtime()' print time.mktime(gmt), '\t time.mktime' print calendar.timegm(gmt), '\t calendar.timegm <-- right' print localt, '\t time.localtime()' print time.mktime(localt), '\t time.mktime <-- right' print calendar.timegm(localt), '\t calendar.timegm' print '\n* datetime.utcnow():' print utc_now.timetuple(), '\t datetime.utcnow()' print to_timestamp2(utc_now), '\t time.mktime' print to_timestamp(utc_now), '\t calendar.timegm <-- right' print local_now.timetuple(), '\t datetime.now()' print to_timestamp2(local_now), '\t time.mktime <-- right' print to_timestamp(local_now), '\t calendar.timegm' print '\n* summary for getting UTC time in sec:' print time.time() print calendar.timegm(time.gmtime()) print time.mktime(time.localtime()) print calendar.timegm(datetime.datetime.utcnow().timetuple()) print time.mktime(datetime.datetime.now().timetuple()) ################################################### -- Lenjoy -------------- next part -------------- An HTML attachment was scrubbed... URL: From alexander.belopolsky at gmail.com Tue Aug 23 16:32:01 2016 From: alexander.belopolsky at gmail.com (Alexander Belopolsky) Date: Tue, 23 Aug 2016 16:32:01 -0400 Subject: [Datetime-SIG] local and utc time/date in python In-Reply-To: References: Message-ID: On Tue, Aug 23, 2016 at 3:39 PM, Lenjoy wrote: > summary for getting UTC time in sec: > > print time.time() > print calendar.timegm(time.gmtime()) > print time.mktime(time.localtime()) > print calendar.timegm(datetime.datetime.utcnow().timetuple()) > print time.mktime(datetime.datetime.now().timetuple()) > > > Are all the functions above expected? > Yes. The time and calendar module precede the datetime module for about a decade. If you use the datetime module, the "one obvious way" to get time in "seconds since UNIX epoch" is to call the .timestamp() method of the datetime object: >>> datetime.now().timestamp() # Works in Python 3.6a 1471983108.547574 >>> datetime.now(timezone.utc).timestamp() # Faster and works since Python 3.5 1471983125.307036 Note that among the functions that you listed only the first will return fractions of a second. If you don't need the datetime module functionality and want to work with floating point (or integer) timestamps - use time.time() method to get the current time. The other functions of the time and calendar modules will come handy when you need to convert between timestamps and human-readable time formats. The datetime module was designed to operate on times directly in the human-readable form and you are not expected to need to convert datetime instances to timestamps except for interoperability with external libraries. PS: From the use of the print statement in your summary, I conclude that you still use Python 2.x. The datetime module has seen several enhancements in the 3.x series and more features are still being added. -------------- next part -------------- An HTML attachment was scrubbed... URL: