Python: using time functions
-
Back-End
How to use local and UTC time in python
Reading UTC (Coordinated Universal Time aka Greenwich Mean Time - GMT):
- as seconds since the Epoch - 1/1/1970:
>>> import time >>> time.time() 1464775923.631278
- as struct_time structure:
>>> time.gmtime() time.struct_time(tm_year=2016, tm_mon=6, tm_mday=1, tm_hour=10, tm_min=35, tm_sec=39, tm_wday=2, tm_yday=153, tm_isdst=0)
- as datetime structure:
>>> import datetime >>> datetime.datetime.now() datetime.datetime(2016, 6, 1, 13, 38, 53, 477147)
Our absolute reference system time is UTC (GMT).
In order to know local time, we have to make adjustemens (DST = Daylight Saving Time) relative to UTC time.
Time structures could be aware of these adjustements (include utc + local offset adjustement (tzinfo)) or not (in this case we have "naive date").
Setting system local time on raspbian:
- Running sudo raspi-config : Internalization options / Change Timezone
- Manually: sudo cp /usr/share/zoneinfo/Europe/Bucharest /etc/localtime
Reading UTC time as struct_time (no offset info):
>>> time.gmtime() time.struct_time(tm_year=2016, tm_mon=6, tm_mday=1, tm_hour=11, tm_min=13, tm_sec=49, tm_wday=2, tm_yday=153, tm_isdst=0)
Reading local time as struct_time:
>>> time.localtime() time.struct_time(tm_year=2016, tm_mon=6, tm_mday=1, tm_hour=14, tm_min=14, tm_sec=1, tm_wday=2, tm_yday=153, tm_isdst=1)
Reading UTC time as naive datetime:
>>> utc = datetime.datetime.utcnow() >>> print utc, "tzinfo =", utc.tzinfo 2016-06-01 12:15:45.431271 tzinfo = None >>> utc.timetuple() time.struct_time(tm_year=2016, tm_mon=6, tm_mday=1, tm_hour=12, tm_min=15, tm_sec=45, tm_wday=2, tm_yday=153, tm_isdst=-1)
Reading local time as naive datetime:
>>> local = datetime.datetime.now() >>> print local, "tzinfo =", local.tzinfo 2016-06-01 15:17:27.293524 tzinfo = None >>> local.timetuple() time.struct_time(tm_year=2016, tm_mon=6, tm_mday=1, tm_hour=15, tm_min=17, tm_sec=27, tm_wday=2, tm_yday=153, tm_isdst=-1)
Reading UTC time as aware datetime (offset = 0):
>>> import pytz >>> utc = datetime.datetime.now(tz=pytz.utc) >>> print utc, "tzinfo =", utc.tzinfo 2016-06-01 12:18:41.062542+00:00 tzinfo = UTC >>> utc.timetuple() time.struct_time(tm_year=2016, tm_mon=6, tm_mday=1, tm_hour=12, tm_min=18, tm_sec=41, tm_wday=2, tm_yday=153, tm_isdst=0)
Reading local time as aware datetime:
>>> bucharest = pytz.timezone('Europe/Bucharest')
>>> local = datetime.datetime.now(tz=bucharest)
>>> print local, "tzinfo =", local.tzinfo
2016-06-01 15:20:18.439973+03:00 tzinfo = Europe/Bucharest
>>> local.timetuple()
time.struct_time(tm_year=2016, tm_mon=6, tm_mday=1, tm_hour=15, tm_min=20, tm_sec=18, tm_wday=2, tm_yday=153, tm_isdst=1)
Conversions time_struct to/from epochseconds and formatted output:
FROM
TO
FUNCTION
seconds since the epoch
seconds since the epoch
seconds since the epoch
seconds since the epoch
mport time, calendar
utc = time.gmtime()
print "utc time_struct: ", utc
print "formatted output1: ", time.strftime("%a, %d %b %Y %H:%M:%S %Z", utc)
print "formatted output2: ", time.asctime(utc)
epochseconds = calendar.timegm(utc)
print "epochseconds: %s" % (epochseconds)
print "utc time_struct: ", time.gmtime(epochseconds)
print
local = time.localtime()
print "local time_struct: ", local
print "formatted output1: ", time.strftime("%a, %d %b %Y %H:%M:%S %Z", local)
print "formatted output2: ", time.asctime(local)
epochseconds = time.mktime(local)
print "epochseconds: %s" % (epochseconds)
print "local time_struct: ", time.localtime(epochseconds)
result:
utc time_struct: time.struct_time(tm_year=2016, tm_mon=6, tm_mday=2, tm_hour=13, tm_min=48, tm_sec=34, tm_wday=3, tm_yday=154, tm_isdst=0) formatted output1: Thu, 02 Jun 2016 13:48:34 EET formatted output2: Thu Jun 2 13:48:34 2016 epochseconds: 1464875314 utc time_struct: time.struct_time(tm_year=2016, tm_mon=6, tm_mday=2, tm_hour=13, tm_min=48, tm_sec=34, tm_wday=3, tm_yday=154, tm_isdst=0) local time_struct: time.struct_time(tm_year=2016, tm_mon=6, tm_mday=2, tm_hour=16, tm_min=48, tm_sec=34, tm_wday=3, tm_yday=154, tm_isdst=1) formatted output1: Thu, 02 Jun 2016 16:48:34 EEST formatted output2: Thu Jun 2 16:48:34 2016 epochseconds: 1464875314.0 local time_struct: time.struct_time(tm_year=2016, tm_mon=6, tm_mday=2, tm_hour=16, tm_min=48, tm_sec=34, tm_wday=3, tm_yday=154, tm_isdst=1)
Reading different local times as time_struct:
import os, time
try:
print os.environ['TZ']
except:
print "TZ is not defined, using system local settings"
print "Local system time - timezone (%s), tzname (%s)" % (time.timezone, time.tzname)
print time.strftime('%X %x %Z')
print time.localtime()
print "US/Eastern - timezone (%s), tzname (%s)" % (time.timezone, time.tzname)
os.environ['TZ'] = 'US/Eastern'
time.tzset()
print time.strftime('%X %x %Z')
print time.localtime()
print "Australia/Melbourne - timezone (%s), tzname (%s)" % (time.timezone, time.tzname)
os.environ['TZ'] = 'Australia/Melbourne'
time.tzset()
print time.strftime('%X %x %Z')
print time.localtime()
print "Europe/Bucharest - timezone (%s), tzname (%s)" % (time.timezone, time.tzname)
os.environ['TZ'] = "Europe/Bucharest"
time.tzset()
print time.strftime('%X %x %Z')
print time.localtime()
result:
TZ is not defined, using system local settings
Local system time - timezone (-7200), tzname (('EET', 'EEST'))
11:47:13 06/02/16 EEST
time.struct_time(tm_year=2016, tm_mon=6, tm_mday=2, tm_hour=11, tm_min=47, tm_sec=13, tm_wday=3, tm_yday=154, tm_isdst=1)
US/Eastern - timezone (-7200), tzname (('EET', 'EEST'))
04:47:13 06/02/16 EDT
time.struct_time(tm_year=2016, tm_mon=6, tm_mday=2, tm_hour=4, tm_min=47, tm_sec=13, tm_wday=3, tm_yday=154, tm_isdst=1)
Australia/Melbourne - timezone (18000), tzname (('EST', 'EDT'))
18:47:13 06/02/16 AEST
time.struct_time(tm_year=2016, tm_mon=6, tm_mday=2, tm_hour=18, tm_min=47, tm_sec=13, tm_wday=3, tm_yday=154, tm_isdst=0)
Europe/Bucharest - timezone (-36000), tzname (('AEST', 'AEDT'))
11:47:13 06/02/16 EEST
time.struct_time(tm_year=2016, tm_mon=6, tm_mday=2, tm_hour=11, tm_min=47, tm_sec=13, tm_wday=3, tm_yday=154, tm_isdst=1)
08:47:13 06/02/16 UTC
time.struct_time(tm_year=2016, tm_mon=6, tm_mday=2, tm_hour=8, tm_min=47, tm_sec=13, tm_wday=3, tm_yday=154, tm_isdst=0)
Converting date/time values between different zones
import time, os, datetime, pytz
bucharest = pytz.timezone('Europe/Bucharest')
#Using astimezone
year1 = 2016
month1 = 6
day1 = 2
hour1 = 17
min1 = 0
sec1 = 0
microsec1 = 0
utc = datetime.datetime(year1, month1, day1, hour1, min1, sec1, microsec1, tzinfo=pytz.utc)
local = utc.astimezone(bucharest)
print utc, utc.tzinfo
print local, local.strftime('%z'), local.utcoffset(), local.dst(), local.tzinfo
print local - utc
year2 = 2016
month2 = 12
day2 = 2
hour2 = 17
min2 = 0
sec2 = 0
microsec2 = 0
utc = datetime.datetime(year2, month2, day2, hour2, min2, sec2, microsec2, tzinfo=pytz.utc)
local = utc.astimezone(bucharest)
print utc, utc.tzinfo
print local, local.strftime('%z'), local.utcoffset(), local.dst(), local.tzinfo
print local - utc
#Using pytz.localize
#naive dates
date1 = datetime.date(2016,6,2)
date2 = datetime.date(2016,12,2)
time = datetime.time(17,0,0)
dt1 = datetime.datetime.combine(date1,time)
dt2 = datetime.datetime.combine(date2,time)
#aware localized dates
local1 = pytz.timezone('Europe/Bucharest').localize(dt1)
local2 = pytz.timezone('Europe/Bucharest').localize(dt2)
print local1, local1.strftime('%z'), local1.utcoffset(), local1.dst(), local1.tzinfo
print local2, local2.strftime('%z'), local2.utcoffset(), local2.dst(), local2.tzinfo
result:
2016-06-02 17:00:00+00:00 UTC 2016-06-02 20:00:00+03:00 +0300 3:00:00 1:00:00 Europe/Bucharest 0:00:00 2016-12-02 17:00:00+00:00 UTC 2016-12-02 19:00:00+02:00 +0200 2:00:00 0:00:00 Europe/Bucharest 0:00:00 2016-06-02 17:00:00+03:00 +0300 3:00:00 1:00:00 Europe/Bucharest 2016-12-02 17:00:00+02:00 +0200 2:00:00 0:00:00 Europe/Bucharest