What's new in datetime that is a bit more useful in Python 3

I personally think that the datetime module is one of the standard Python libraries that is not very cool because it has many traps and is difficult to use despite its frequent use.

It's such a datetime module, but when I migrated my Python 2 project to Python 3, I was able to organize the code a lot and was a little impressed, so I will introduce it.

Mutual conversion with unixtime

Conversion from unixtime to datetime.datetime is done with the.fromtimestamp ()for local time and the.utcfromtimestamp ()function for UTC.

>>> import time
>>> from datetime import datetime
>>> now = time.time()
>>> now
1415542873.099776

>>> loc = datetime.fromtimestamp(now)
>>> loc
datetime.datetime(2014, 11, 9, 23, 21, 13, 99776)

>>> utc = datetime.utcfromtimestamp(now)
>>> utc
datetime.datetime(2014, 11, 9, 14, 21, 13, 99776)

However, on the contrary, the conversion from datetime to unixtime cannot be done directly, and it is necessary to convert it to time.struct_time type once with the .timetuple () method. What's more, when reverting to unixtime from there, you could go with time.mktime () for local time, but you needed calendar.timegm () for UTC. It's a bit disappointing to import the calendar module just for this.

>>> time.mktime(loc.timetuple())
1415542873.0
>>> import calendar
>>> calendar.timegm(utc.timetuple())
1415542873

The time module only provides an additional timer for each OS to the C language time.h function, and datetime is designed for Python, so when you try to use it in combination, such a mismatch occurs. Was there.

However, Python 3.3 adds a .timestamp () method that creates a unixtime directly on the datetime object.

>>> from datetime import datetime
>>> now = 1415542873.099776
>>> loc = datetime.fromtimestamp(now)
>>> loc.timestamp()
1415542873.099776

However, .timestamp () uses tzinfo that datetime itself has, and if datetime (called naive) that does not have tzinfo, it is calculated as local time. If you want to convert UTC datetime to unixtime, you need to set tz = utc properly. See the next section for this.

timezone

The datetime type of the datetime module that has timezone information is called aware, and the one that does not have timezone information is called naive.

Naive datetime is attractive as a pure type that doesn't get addicted to noisy timezone conversions when interacting with other naive software such as MySQL, but when writing programs that handle both UTC and JST. If you make a mistake and execute +9 twice, it will cause a bug.

IANA publishes a database about the world time zone, but since the timing is not synchronized with the Python version upgrade, the standard library provides only the abstract class, and the actual time zone class that inherits it. Was provided in a non-standard library called pytz.

However, JST and UTC, which do not have daylight saving time so far, are sufficient applications, and it is a little troublesome to use timezone databases all over the world. I feel that I can understand the feelings of ASCII-speaking people who do not want to think about multi-byte environments.

In Python 3, a class called datetime.timezone that handles timezones with a fixed time difference from UTC and no daylight saving time is provided in the standard library, and for UTC, datetime.timezone.utc is provided from the beginning. It is provided.

Use this to try the unixtime and datetime conversions we did in the previous section, this time with a aware datetime.

>>> from datetime import datetime, timezone, timedelta
>>> now = 1415542873.099776
>>> JST = timezone(timedelta(hours=+9), 'JST')

>>> loc = datetime.fromtimestamp(now, JST)
>>> utc = datetime.fromtimestamp(now, timezone.utc)
>>> print(loc)
2014-11-09 23:21:13.099776+09:00
>>> print(utc)
2014-11-09 14:21:13.099776+00:00
>>> loc.timestamp()
1415542873.099776
>>> utc.timestamp()
1415542873.099776

You don't even need a calendar module, and you can now write simpler, less buggy code than in Python 2.

To add timezone information to naive datetime, use the .replace () method.

>>> naiveutc = datetime.utcfromtimestamp(now)
>>> naiveutc
datetime.datetime(2014, 11, 9, 14, 21, 13, 99776)
>>> naiveutc.replace(tzinfo=timezone.utc).timestamp()
1415542873.099776

Recommended Posts

What's new in datetime that is a bit more useful in Python 3
What's new in Python 3.5
What's new in Python 3.6
What's new in Python 3.10 (Summary)
What's in that variable (when running a Python script)
What's new in Python 3.4.0 (2) --enum
What's new in Python 3.9 (Summary)
What's new in python3.9 Merge dictionaries
Play a sound in Python assuming that the keyboard is a piano keyboard
A program that determines whether a number entered in Python is a prime number
Hash in Perl is a dictionary in Python
A memo that I wrote a quicksort in Python
A program that removes duplicate statements in Python
Create a new page in confluence with Python
Create a datetime object from a string in Python (Python 3.3)
Check if the string is a number in python
Hello World is a simple web server that follows WSGI (Web Server Gateway Interface) in Python.
Find the part that is 575 from Wikipedia in Python
In Python, create a decorator that dynamically accepts arguments Create a decorator
New in Python 3.4.0 (1)-pathlib
MALSS, a tool that supports machine learning in Python
How to test that Exception is raised in python unittest
A Python program in "A book that gently teaches difficult programming"
A general-purpose program that formats Linux command strings in python
A function that divides iterable into N pieces in Python
Published a library that hides character data in Python images
Loop through a generator that returns a date iterator in Python
Let's create a script that registers with Ideone.com in Python.
I tried "a program that removes duplicate statements in Python"
Grayscale image is displayed as a color image in OpenCV / Python
Create code that outputs "A and pretending B" in python
[MQTT / Python] Implemented a class that does MQTT Pub / Sub in Python
Is there a bias in the numbers that appear in the Fibonacci numbers?
A set of script files that do wordcloud in Python3
Take a screenshot in Python
[Python] Debugging is more efficient!
Create a function in Python
Create a dictionary in Python
Make a bookmarklet in Python
Draw a heart in Python
What is a python map?
Here's a summary of things that might be useful when dealing with complex numbers in Python
A memo that handles double-byte double quotes in Python regular expressions
Use networkx, a library that handles graphs in python (Part 2: Tutorial)
Easy! Implement a Twitter bot that runs on Heroku in Python
A record that GAMEBOY could not be done in Python. (PYBOY)
[Python] Execution time when a function is entered in a dictionary value
About psd-tools, a library that can process psd files in Python
Delete a particular character in Python if it is the last
How to judge that the cross key is input in Python3
Timezone specification when converting a string to datetime type in python
python3.9 has new features that no one is paying attention to ...
A function that measures the processing time of a method in python
A class that summarizes frequently used methods in twitter api (python)
Self-implementation of something like% timeit, a magic command of jupyter that is convenient for speed measurement, in Python
[Python] What is a zip function?
Maybe in a python (original title: Maybe in Python)
[Python] What is a with statement?
Write a binary search in Python
[python] Manage functions in a list
Hit a command in Python (Windows)