Python timezone handling

Environment: Python-3.5

Python time is created with the time zone in mind. A note about how to handle time zones. The timezone-naive is the time when the timezone is not conscious, and the timezone-aware is the time when the timezone is conscious.

from datetime import datetime,timezone
import  pytz

naive = datetime.now()
aware = datetime.now(timezone.utc)

Passing timezone as an argument to timezone # now creates a timezone-aware current time. The two cannot be compared.

naive > awre 
# TypeError: can't compare offset-naive and offset-aware datetimes

An exception is thrown.

When creating an arbitrary time, pass the timezone as an argument as follows.

naive = datetime(2016,6,1,12,0,0,0)
aware = datetime(2016,6,1,12,0,0,0,timezone.utc)

Use datetime # astimezone if you want to change to your local timezone. Of course, time zone-naive time cannot be used.

print(naive.astimezone())
# ValueError: astimezone() cannot be applied to a naive datetime
print(aware.astimezone())
# 2016-06-01 21:00:00+09:00

It is convenient to use pytz when using your favorite time zone. timezone-Changes a naive time to the specified timezone.

import  pytz

de = pytz.timezone('Europe/Berlin')
print(de.localize(naive))
# 2016-06-01 12:00:00+02:00

jp = pytz.timezone('Asia/Tokyo')
print(jp.localize(naive))
# 2016-06-01 12:00:00+09:00

timezone-Change the time zone of aware time. This does not change the actual time.

print(aware.astimezone(jp))
# 2016-06-01 21:00:00+09:00
print(aware.astimezone(de))
# 2016-06-01 14:00:00+02:00

You can override the timezone with datetime # replace. Naturally, the time to express will change.

print(aware.replace(tzinfo=jp).astimezone(timezone.utc))
# 2016-06-01 03:00:00+00:00
print(aware.replace(tzinfo=de).astimezone(timezone.utc))
# 2016-06-01 11:07:00+00:00

Recommended Posts

Python timezone handling
Python Error Handling
Python exception handling
Python exception handling
Handling json in python
Python, about exception handling
Python decimal point handling
Hexadecimal handling in Python 3
Python exception handling (Python learning memo ⑥)
Python
python> Handling of 2D arrays
About Python datetime and timezone
Relative url handling in python
Python Application: Data Handling Part 3: Data Format
pip install mysql-Error handling during python
Python class definitions and instance handling
Exception handling during Python API communication
Handling of JSON files in Python
Handling regular expressions with PHP / Python
kafka python
I tried to summarize Python exception handling
Python basics ⑤
python + lottery 6
Python Summary
Built-in python
Python comprehension
Python technique
Python 2.7 Countdown
Python memorandum
Python FlowFishMaster
Python service
python tips
python function ①
Python basics
Python memo
ufo-> python (3)
Python comprehension
install python
Python Singleton
Python basics ④
Python Memorandum 2
python memo
Python Jinja2
Python increment
atCoder 173 Python
[Python] function
Python installation
Installing Python 3.4.3.
Try python
Python memo
Python iterative
Python algorithm
Python2 + word2vec
[Python] Variables
Python functions
Python sys.intern ()
Python tutorial
Python decimals
python underscore
Python summary
Start python