Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get UTC timestamp from time zone aware datetime object in Python

I have a datetime object that I created withdatetime.datetime(year, month, day, hour, minute, tzinfo=pytz.timezone('US/Pacific')). Please correct me if I'm wrong, but I believe that my datetime object is NOT naïve. How do I convert this datetime object to a UTC timestamp?

like image 354
ILoveGit Avatar asked Sep 20 '25 07:09

ILoveGit


1 Answers

Use datetime.astimezone to get the same datetime in UTC (or any other timezone).

dt = datetime.datetime(year, month, day, hour, minute, tzinfo=pytz.timezone('US/Pacific'))
dt_utc = dt.astimezone(pytz.timezone('UTC'))
like image 162
Tore Eschliman Avatar answered Sep 22 '25 22:09

Tore Eschliman