I have a database that stores datetime as UTC. I need to look at information from a certain time, but the date and time are given in local time, say, "Europe / Copenhagen." I have been given the following:
year = 2012; month = 12; day = 2; hour = 13; min = 1;
So, I need to convert them to UTC so that I can find them in the database. I want to do this with pytz . I look localize :
local_tz = timezone('Europe/Copenhagen') t = local_tz.localize(datetime.datetime(year, month, day, hour, min))
But I'm confused about localize() . Is it assumed that year, etc. Provided to me by local time? Or, are they supposed to be given in UTC, and now he has converted them to local time?
print t gives me:
2012-12-02 13:01:00+01:00
So it seems that he suggested that the original year, etc. was in utc; hours now 13 + 1 instead of 13. So what should I do instead? I read the pytz documentation and it does not make me more clear to me. It says a lot that everything is complicated, so I'm not sure if pytz really solves these problems. And I donβt always know if examples show what things work or what wonβt work.
I tried to normalize:
print local_tz.normalize(t)
This gives me the same result as print t.
EDIT: with the figures above for the year, etc. It must match the information in the database on 2012-12-2 12:01. (since Copenhagen is utc + 1 on this date)
python timezone pytz
user984003
source share