There is no 1 hour difference between time.gmtime()
and datetime.utcnow()
in Python. They both represent the same time in UTC timezone. time.gmtime()
does not represent time in Europe/London
timezone. There is no DST transition in UTC timezone. The utc offset in UTC timezone is always zero.
Your code is wrong. It is incorrect to use time.mktime()
with time.gmtime()
as an input unless your local timezone is UTC. Your local timezone is not utc as witnessed by time.timezone != 0
.
Isn't all this supposed to be UTC time?
time.gmtime()
returns UTC time as a time tuple
datetime.utcnow()
returns UTC time as a naive datetime
object
time.mktime()
accepts local time and returns "seconds since the epoch"
datetime.fromtimestamp()
accepts "seconds since the epoch" and returns the local time as a naive datetime object
To compare time.gmtime()
and datetime.utcnow()
:
#!/usr/bin/env python
import time
from datetime import datetime
print(abs(datetime(*time.gmtime()[:6]) - datetime.utcnow()))
# -> 0:00:00.524724
The difference is not zero because a time tuple does not store microseconds otherwise both functions return the same time regardless of your local timezone.
If your intent is to find the utc offset of your local timezone then see Getting computer's utc offset in Python.