0

My timezone is not UTC. When I get the date time with datetime.now() I get the local time, but the tzinfo field has the value none.

I see the same result with python 2.7 and python 3.6.7.

I would expect to get a timezone info or a time offset value. Why is that ? Is there a way to get the time offset as needed for the ISO time format ? 

chmike
  • 20,922
  • 21
  • 83
  • 106

1 Answers1

0

This is because now will get the present time of any particular timezone, by default it will give you the datetime object of current timezone that you're in (or your computer is set to).

You can get the present time of any other timezone, by passing that timezone to now function.

In [1]: from datetime import datetime

In [2]: import pytz  # 3rd party: $ pip install pytz

In [4]: datetime.now()
Out[4]: datetime.datetime(2019, 2, 12, 20, 10, 2, 778532)

In [5]: datetime.now(pytz.utc)
Out[5]: datetime.datetime(2019, 2, 12, 14, 40, 4, 334078, tzinfo=<UTC>)
harshil9968
  • 3,254
  • 1
  • 16
  • 26