I am writing a program that should log the offset of the machine's local time from UTC at regular intervals, if it has changed.
I am aware that it is possible to find the offset at the time the program was started. This can be done by
str(datetime.datetime.now()-datetime.datetime.utcnow())
or
str(datetime.datetime.now().replace(tzinfo=timezone.utc).astimezone())
or other methods.
The problem is that the time returned by datetime.datetime.now() seems to always be in the timezone at which datetime was first imported, rather than the current timezone. That is, changing the local time on my machine is not reflected by the same change in the time given by datetime.datetime.now() .
As an example,
import datetime
datetime.datetime.now()
reports '2014-09-11 12:35:45.415104'.
I now change my timezone to UTC+4 on the machine.
datetime.datetime.now()
still reports '2014-09-11 12:35:50.407779'.
How can I obtain the current time on my machine using python (preferably without external libraries and in a platform-agnostic manner)?
I am using Python 3.4.1 on Windows 8.