Delorean: A very practical Python time format intelligent conversion tool

DeLorean is a third-party module for Python, developed based on pytz and dateutil, to handle the format conversion of date and time in Python.

Since time conversion is a sufficiently subtle problem, DeLorean wanted to provide a cleaner and less hassle solution for shifting, manipulating, and generating datetimes. For example, to instantiate a time object in the form of a string, Delorean only needs to parse the specified string and convert it without declaring its format.

As for the origin of the module name Delorean, Delorean is the very cool gull-wing car in the movie "Back to the Future". The name of the very representative car in this movie is used as the library name. The author also thinks that Express using this library allows you to roam freely in time and space without constraints.

The most intelligent thing about this module is that it can automatically identify the time format of the string and convert it to a Delorean object, and this Delorean object is the same as the Datetime object. If you like this article, remember to bookmark, follow, and like.

from delorean import parse
parse("2011/01/01 00:00:00 -0700")
# Delorean(datetime=datetime.datetime(2011, 1, 1, 0, 0), timezone=pytz.FixedOffset(-420))
parse("2018-05-06")
# Delorean(datetime=datetime.datetime(2018, 6, 5, 0, 0), timezone='UTC')

The following will introduce its basic usage.

1. Prepare

Please choose one of the following ways to enter the command to install dependencies :
1. Open Cmd in Windows environment (Start-Run-CMD).
2. Open Terminal in MacOS environment (command+space to enter Terminal).
3. If you are using the VSCode editor or Pycharm, you can directly use the Terminal at the bottom of the interface.

pip install Delorean

2. Delorean basic use

Get current time easily:

from delorean import Delorean

d = Delorean()
print(d)
# Delorean(datetime=datetime.datetime(2021, 10, 6, 9, 5, 57, 611589), timezone='UTC')

Convert time in datetime format to Delorean:

import datetime
from delorean import Delorean

d = Delorean()
print(d)
d = Delorean(datetime=datetime.datetime(2018, 5, 10, 8, 52, 23, 560811), timezone='UTC')
# 这里默认的是UTC时间
print(d)
# Delorean(datetime=datetime.datetime(2021, 10, 6, 9, 5, 57, 611589), timezone='UTC')
# Delorean(datetime=datetime.datetime(2018, 5, 10, 8, 52, 23, 560811), timezone='UTC')

Convert to domestic time zone:

import datetime
from delorean import Delorean

d = Delorean(datetime=datetime.datetime(2018, 5, 10, 8, 52, 23, 560811), timezone='UTC')
d = d.shift("Asia/Shanghai")
print(d)
# Delorean(datetime=datetime.datetime(2018, 5, 10, 16, 52, 23, 560811), timezone='Asia/Shanghai')

The output is datetime, date is not a problem:

import datetime
from delorean import Delorean

d = Delorean(datetime=datetime.datetime(2018, 5, 10, 8, 52, 23, 560811), timezone='UTC')
d = d.shift("Asia/Shanghai")
print(d.datetime)
print(d.date)
# 2018-05-10 16:52:23.560811+08:00
# 2018-05-10

View timestamp and UTC time:

import datetime
from delorean import Delorean

d = Delorean(datetime=datetime.datetime(2018, 5, 10, 8, 52, 23, 560811), timezone='UTC')
d = d.shift("Asia/Shanghai")
print(d.epoch)
print(d.naive)
# 1525942343.560811
# 2018-05-10 08:52:23.560811

Initialize Delorean with a unix timestamp:

from delorean import epoch
d = epoch(1357971038.102223).shift("Asia/Shanghai")
print(d)
# Delorean(datetime=datetime.datetime(2013, 1, 12, 14, 10, 38, 102223), timezone='Asia/Shanghai')

Delorean supports time addition and subtraction of timedelta. Delorean can use timedelta to add and subtract to get a Delorean object:

import datetime
from delorean import Delorean

d = Delorean(datetime=datetime.datetime(2018, 5, 10, 8, 52, 23, 560811), timezone='UTC')
d = d.shift("Asia/Shanghai")
print(d)
d2 = d + datetime.timedelta(hours=2)
print(d2)
d3 = d - datetime.timedelta(hours=3)
print(d3)
# Delorean(datetime=datetime.datetime(2018, 5, 10, 16, 52, 23, 560811), timezone='Asia/Shanghai')
# Delorean(datetime=datetime.datetime(2018, 5, 10, 18, 52, 23, 560811), timezone='Asia/Shanghai')
# Delorean(datetime=datetime.datetime(2018, 5, 10, 13, 52, 23, 560811), timezone='Asia/Shanghai')

3. Delorean Advanced Use

Usually we don't care how many subtleties or how many seconds, so Delorean provides a very convenient way to filter:

from delorean import Delorean

d = Delorean()
print(d)
# Delorean(datetime=datetime.datetime(2019, 3, 14, 4, 0, 50, 597357), timezone='UTC')
d.truncate('second')
# Delorean(datetime=datetime.datetime(2019, 3, 14, 4, 0, 50), timezone='UTC')
d.truncate('hour')
# Delorean(datetime=datetime.datetime(2019, 3, 14, 4, 0), timezone='UTC')
d.truncate('month')
# Delorean(datetime=datetime.datetime(2019, 3, 1, 0, 0), timezone='UTC')
d.truncate('year')
# Delorean(datetime=datetime.datetime(2019, 1, 1, 0, 0), timezone='UTC')

In addition, when processing strings in datetime format, various formats need to be marked for conversion. In Delorean, we don't need to be so troublesome, just parse it directly:

from delorean import parse
parse("2011/01/01 00:00:00 -0700")
# Delorean(datetime=datetime.datetime(2011, 1, 1, 0, 0), timezone=pytz.FixedOffset(-420))
parse("2018-05-06")
# Delorean(datetime=datetime.datetime(2018, 6, 5, 0, 0), timezone='UTC')

recommended article

Guess you like

Origin blog.csdn.net/qq_34160248/article/details/124215275