I have working code. My question is why does this work?
#"04/13/05 2:30pm EDT" <- original date string
from datetime import datetime, timedelta
from pytz import timezone
import pytz
import time
T= time.struct_time((2005,4,13,14,30,0,0,0,1))
t = time.mktime(T)
print(t)
this prints
1113417000,
which according to an online converter, http://www.epochconverter.com/, prints
Your time zone: 4/13/2005 2:30:00 PM GMT-4
which is correct. My question is, how does it know the time was in EDT which is GMT-4? The last of the 9-tuple is “dst flag”, but there are numerous timezones where DST is used. So how does it detect the correct timezone?
It asks your operating system for that information, by way of the stdlib C time functions.
To quote the
timemodule documentation:You can pass in a -1 for the DST flag to have the library set it for you, see
time.struct_time. On my Mac OS X 10.7.5 setup, configured for the Europe/Oslo timezone, that gives me:or, as ‘translated’ by epochconverter: