I was confused when dealing with datetime with the time zone in mind, so I will summarize it for myself.
Naive,Aware
Date and time objects in Python are classified into the following two types. The rough difference is with or without time zone information
.
Naive object
No timezone information
Aware object
Has timezone information (tzinfo attribute)
The tzinfo attribute can be set to an instance of a subclass of tzinfo
Since dateitme.timezone is a subclass of tzinfo, you can set objects of this class.
For example, if you create an object as datetime.timezone (timedelta (hours = + 9),'JST')
, this is an object that represents the JST timezone.
Therefore, if you want to perform time zone-aware processing in some program, you need to use Aware object.
There are four main objects below.
Depending on how each object is created, it may be Naive or Aware. For example
>>> print('Python %s on %s' % (sys.version, sys.platform))
Python 3.7.4 (default, Oct 10 2019, 12:40:25)
[Clang 10.0.1 (clang-1001.0.46.4)] on darwin
>>> import datetime
>>> aware_dt = datetime.datetime.now(tz=datetime.timezone.utc)
>>> aware_dt
datetime.datetime(2020, 4, 4, 5, 49, 51, 291393, tzinfo=datetime.timezone.utc)
>>> naive_dt = datetime.datetime.utcnow()
>>> naive_dt
datetime.datetime(2020, 4, 4, 5, 50, 8, 499521)
>>> aware_dt.tzinfo
datetime.timezone.utc
>>> print(naive_dt.tzinfo)
<class 'NoneType'>
When creating the utc current time as an object, there are methods to specify the time zone and methods to not.
strptime
A method that makes a character string representing a date a datetime type.
>>> str_dt = '2020-3-01 10:11:12'
>>> strp_dt = datetime.datetime.strptime(str_dt, "%Y-%m-%d %H:%M:%S")
>>> strp_dt
datetime.datetime(2020, 3, 1, 10, 11, 12)
>>> print(type(strp_dt.tzinfo))
<class 'NoneType'>
The created object does not contain the timezone attribute. However, if the string before conversion contains a time zone and the format is set appropriately
>>> str_dt_utc = '2020-3-01 10:11:12+00:00'
>>> strp_dt_utc = datetime.datetime.strptime(str_dt_utc, "%Y-%m-%d %H:%M:%S%z")
>>> strp_dt_utc
datetime.datetime(2020, 3, 1, 10, 11, 12, tzinfo=datetime.timezone.utc)
>>> str_dt_jst = '2020-3-01 10:11:12+09:00'
>>> strp_dt_jst = datetime.datetime.strptime(str_dt_jst, "%Y-%m-%d %H:%M:%S%z")
>>> strp_dt_jst
datetime.datetime(2020, 3, 1, 10, 11, 12, tzinfo=datetime.timezone(datetime.timedelta(seconds=32400)))
It seems that the datetime object is generated with the time zone attribute set as well.
astimezone
A method that transforms a datetime object according to the specified timezone information.
>>> strp_dt.astimezone(datetime.timezone.utc)
datetime.datetime(2020, 3, 1, 1, 11, 12, tzinfo=datetime.timezone.utc)
The time difference between JST and UTC, -9 hours, was reflected, and the datetime object with tzinfo added was returned. (Is this a comparison with the local timezone?)
replace
A method that converts the time zone of a datetime object to the specified time zone.
>>> strp_dt.replace(tzinfo=datetime.timezone.utc)
datetime.datetime(2020, 3, 1, 10, 11, 12, tzinfo=datetime.timezone.utc)
Note that only tzinfo is converted when compared to astimezone.
Regarding the creation of the timezone object, you can simply create it like datetime.timezone (timedelta (hours = + 9),'JST')
, or use a third-party module (pytz, dateutil). There are various methods.
Recommended Posts