Cara menggunakan python timezone aware datetime

Datetime is the name of one of the module within python and one of the other classes within the module.

It is an instance that represents a single point in time.
In order to get immediate datetime details, we use now method. It returns the details of time of the moment when now was called.

import datetime datetime.datetime.now()

Output:

datetime.datetime(2019, 10, 30, 19, 9, 31, 900482)

Formatting datetime objects

Here we will learn about extracting attributes and writing different formats of date.
The occasions where we want to display datetime object in a certain way, we use strftime method.
Let us, understand it with examples,

d = datetime.datetime(2019, 10, 30, 20, 15) d.strftime("%Y/%m/%d") > '2019/10/30' d.strftime("%d %b %y") > '30 Oct 19' d.strftime("%Y-%m-%d %H:%M:%S") > '2019-10-30 20:15:00'

Naive v/s Aware in Python

So far, we have only seen formatting datetime objects. That means the object is naive to any sort of time zones.

By default all datetime objects are naive. To make them timezone-aware, you must attach a tzinfo object, which provides the UTC offset and timezone abbreviation as a function of date and time.

Fixed Offset Time Zones

For time zones that are a fixed offset from UTC, in Python 3.2+, the datetime module provides the timezone class, a concrete implementation of tzinfo, which takes a from datetime import datetime, timedelta from dateutil import tz JST = tz.tzoffset('JST', 9 * 3600) # 3600 seconds per hour dt = datetime(2015, 1, 1, 12, 0, tzinfo=JST) print(dt) # 2015-01-01 12:00:00+09:00 print(dt.tzname) # 'JST' 0 and an (optional) name parameter:

Python 3.x3.2

from datetime import datetime, timedelta, timezone JST = timezone(timedelta(hours=+9)) dt = datetime(2015, 1, 1, 12, 0, 0, tzinfo=JST) print(dt) # 2015-01-01 12:00:00+09:00 print(dt.tzname()) # UTC+09:00 dt = datetime(2015, 1, 1, 12, 0, 0, tzinfo=timezone(timedelta(hours=9), 'JST')) print(dt.tzname) # 'JST'

For Python versions before 3.2, it is necessary to use a third party library, such as from datetime import datetime, timedelta from dateutil import tz JST = tz.tzoffset('JST', 9 * 3600) # 3600 seconds per hour dt = datetime(2015, 1, 1, 12, 0, tzinfo=JST) print(dt) # 2015-01-01 12:00:00+09:00 print(dt.tzname) # 'JST' 1. from datetime import datetime, timedelta from dateutil import tz JST = tz.tzoffset('JST', 9 * 3600) # 3600 seconds per hour dt = datetime(2015, 1, 1, 12, 0, tzinfo=JST) print(dt) # 2015-01-01 12:00:00+09:00 print(dt.tzname) # 'JST' 1 provides an equivalent class, from datetime import datetime, timedelta from dateutil import tz JST = tz.tzoffset('JST', 9 * 3600) # 3600 seconds per hour dt = datetime(2015, 1, 1, 12, 0, tzinfo=JST) print(dt) # 2015-01-01 12:00:00+09:00 print(dt.tzname) # 'JST' 3, which (as of version 2.5.3) takes arguments of the form from datetime import datetime, timedelta from dateutil import tz JST = tz.tzoffset('JST', 9 * 3600) # 3600 seconds per hour dt = datetime(2015, 1, 1, 12, 0, tzinfo=JST) print(dt) # 2015-01-01 12:00:00+09:00 print(dt.tzname) # 'JST' 4, where from datetime import datetime, timedelta from dateutil import tz JST = tz.tzoffset('JST', 9 * 3600) # 3600 seconds per hour dt = datetime(2015, 1, 1, 12, 0, tzinfo=JST) print(dt) # 2015-01-01 12:00:00+09:00 print(dt.tzname) # 'JST' 5 is specified in seconds:

Python 3.x3.2

Python 2.x2.7

from datetime import datetime, timedelta from dateutil import tz JST = tz.tzoffset('JST', 9 * 3600) # 3600 seconds per hour dt = datetime(2015, 1, 1, 12, 0, tzinfo=JST) print(dt) # 2015-01-01 12:00:00+09:00 print(dt.tzname) # 'JST'

Zones with daylight savings time

For zones with daylight savings time, python standard libraries do not provide a standard class, so it is necessary to use a third party library. from datetime import datetime, timedelta from dateutil import tz JST = tz.tzoffset('JST', 9 * 3600) # 3600 seconds per hour dt = datetime(2015, 1, 1, 12, 0, tzinfo=JST) print(dt) # 2015-01-01 12:00:00+09:00 print(dt.tzname) # 'JST' 6 and from datetime import datetime, timedelta from dateutil import tz JST = tz.tzoffset('JST', 9 * 3600) # 3600 seconds per hour dt = datetime(2015, 1, 1, 12, 0, tzinfo=JST) print(dt) # 2015-01-01 12:00:00+09:00 print(dt.tzname) # 'JST' 1 are popular libraries providing time zone classes.

In addition to static time zones, from datetime import datetime, timedelta from dateutil import tz JST = tz.tzoffset('JST', 9 * 3600) # 3600 seconds per hour dt = datetime(2015, 1, 1, 12, 0, tzinfo=JST) print(dt) # 2015-01-01 12:00:00+09:00 print(dt.tzname) # 'JST' 1 provides time zone classes that use daylight savings time (see the documentation for the from datetime import datetime, timedelta from dateutil import tz JST = tz.tzoffset('JST', 9 * 3600) # 3600 seconds per hour dt = datetime(2015, 1, 1, 12, 0, tzinfo=JST) print(dt) # 2015-01-01 12:00:00+09:00 print(dt.tzname) # 'JST' 9 module). You can use the from datetime import datetime from dateutil import tz local = tz.gettz() # Local time PT = tz.gettz('US/Pacific') # Pacific time dt_l = datetime(2015, 1, 1, 12, tzinfo=local) # I am in EST dt_pst = datetime(2015, 1, 1, 12, tzinfo=PT) dt_pdt = datetime(2015, 7, 1, 12, tzinfo=PT) # DST is handled automatically print(dt_l) # 2015-01-01 12:00:00-05:00 print(dt_pst) # 2015-01-01 12:00:00-08:00 print(dt_pdt) # 2015-07-01 12:00:00-07:00 0 method to get a time zone object, which can then be passed directly to the datetime constructor:

from datetime import datetime from dateutil import tz local = tz.gettz() # Local time PT = tz.gettz('US/Pacific') # Pacific time dt_l = datetime(2015, 1, 1, 12, tzinfo=local) # I am in EST dt_pst = datetime(2015, 1, 1, 12, tzinfo=PT) dt_pdt = datetime(2015, 7, 1, 12, tzinfo=PT) # DST is handled automatically print(dt_l) # 2015-01-01 12:00:00-05:00 print(dt_pst) # 2015-01-01 12:00:00-08:00 print(dt_pdt) # 2015-07-01 12:00:00-07:00

CAUTION: As of version 2.5.3, from datetime import datetime, timedelta from dateutil import tz JST = tz.tzoffset('JST', 9 * 3600) # 3600 seconds per hour dt = datetime(2015, 1, 1, 12, 0, tzinfo=JST) print(dt) # 2015-01-01 12:00:00+09:00 print(dt.tzname) # 'JST' 1 does not handle ambiguous datetimes correctly, and will always default to the later date. There is no way to construct an object with a from datetime import datetime, timedelta from dateutil import tz JST = tz.tzoffset('JST', 9 * 3600) # 3600 seconds per hour dt = datetime(2015, 1, 1, 12, 0, tzinfo=JST) print(dt) # 2015-01-01 12:00:00+09:00 print(dt.tzname) # 'JST' 1 timezone representing, for example from datetime import datetime from dateutil import tz local = tz.gettz() # Local time PT = tz.gettz('US/Pacific') # Pacific time dt_l = datetime(2015, 1, 1, 12, tzinfo=local) # I am in EST dt_pst = datetime(2015, 1, 1, 12, tzinfo=PT) dt_pdt = datetime(2015, 7, 1, 12, tzinfo=PT) # DST is handled automatically print(dt_l) # 2015-01-01 12:00:00-05:00 print(dt_pst) # 2015-01-01 12:00:00-08:00 print(dt_pdt) # 2015-07-01 12:00:00-07:00 4, since this is during a daylight savings time transition.

All edge cases are handled properly when using from datetime import datetime, timedelta from dateutil import tz JST = tz.tzoffset('JST', 9 * 3600) # 3600 seconds per hour dt = datetime(2015, 1, 1, 12, 0, tzinfo=JST) print(dt) # 2015-01-01 12:00:00+09:00 print(dt.tzname) # 'JST' 6, but from datetime import datetime, timedelta from dateutil import tz JST = tz.tzoffset('JST', 9 * 3600) # 3600 seconds per hour dt = datetime(2015, 1, 1, 12, 0, tzinfo=JST) print(dt) # 2015-01-01 12:00:00+09:00 print(dt.tzname) # 'JST' 6 time zones should not be directly attached to time zones through the constructor. Instead, a from datetime import datetime, timedelta from dateutil import tz JST = tz.tzoffset('JST', 9 * 3600) # 3600 seconds per hour dt = datetime(2015, 1, 1, 12, 0, tzinfo=JST) print(dt) # 2015-01-01 12:00:00+09:00 print(dt.tzname) # 'JST' 6 time zone should be attached using the time zone's from datetime import datetime from dateutil import tz local = tz.gettz() # Local time PT = tz.gettz('US/Pacific') # Pacific time dt_l = datetime(2015, 1, 1, 12, tzinfo=local) # I am in EST dt_pst = datetime(2015, 1, 1, 12, tzinfo=PT) dt_pdt = datetime(2015, 7, 1, 12, tzinfo=PT) # DST is handled automatically print(dt_l) # 2015-01-01 12:00:00-05:00 print(dt_pst) # 2015-01-01 12:00:00-08:00 print(dt_pdt) # 2015-07-01 12:00:00-07:00 8 method:

from datetime import datetime, timedelta import pytz PT = pytz.timezone('US/Pacific') dt_pst = PT.localize(datetime(2015, 1, 1, 12)) dt_pdt = PT.localize(datetime(2015, 11, 1, 0, 30)) print(dt_pst) # 2015-01-01 12:00:00-08:00 print(dt_pdt) # 2015-11-01 00:30:00-07:00

Be aware that if you perform datetime arithmetic on a from datetime import datetime, timedelta from dateutil import tz JST = tz.tzoffset('JST', 9 * 3600) # 3600 seconds per hour dt = datetime(2015, 1, 1, 12, 0, tzinfo=JST) print(dt) # 2015-01-01 12:00:00+09:00 print(dt.tzname) # 'JST' 6-aware time zone, you must either perform the calculations in UTC (if you want absolute elapsed time), or you must call from datetime import datetime, timedelta import pytz PT = pytz.timezone('US/Pacific') dt_pst = PT.localize(datetime(2015, 1, 1, 12)) dt_pdt = PT.localize(datetime(2015, 11, 1, 0, 30)) print(dt_pst) # 2015-01-01 12:00:00-08:00 print(dt_pdt) # 2015-11-01 00:30:00-07:00 0 on the result:

Postingan terbaru

LIHAT SEMUA