1. Introduction
DeLorean is a third-party module for Python, based on pytz and dateutil, that handles date-time format conversion in Python.
Since time conversion is a subtle enough problem, DeLorean wanted to provide a cleaner, less cumbersome solution for shifting, manipulating, and generating datetime. For example, to instantiate a time object in string form, DeLorean simply parse the specified string and convert it without declaring its format.
As for the origin of the module name Delorean, Delorean is the extremely cool gull-winged car in the movie "Back to the Future", using the name of the very representative car in the movie as the name of the library, the author probably also wants to express that using this library will allow you to travel in space and time at will, with no constraints.
The smartest thing to me about this module is that it automatically recognizes the time format of a string, converts it to a Delorean object, and this Delorean object is compatible with the Datetime object:
from delorean import parse parse("2011/01/01 00:00:00 -0700") # Delorean(datetime=(2011, 1, 1, 0, 0), timezone=(-420)) parse("2018-05-06") # Delorean(datetime=(2018, 6, 5, 0, 0), timezone='UTC')
Here's how it's basically used.
2. Preparation
Before you start, you need to make sure that Python and pip have been successfully installed on your computer, if not, visit this article:Super Detailed Python Installation Guide Perform the installation.
(Option 1)If your goal with Python is data analysis, you can just install theAnaconda
(Option 2)In addition, it is recommended to useVSCode EditorIt has many advantages
Please choose one of the following ways to enter commands to install dependencies:
1. Windows environment Open Cmd (Start-Run-CMD).
2. MacOS environment Open Terminal (command+space to enter Terminal).
3. If you are using a VSCode editor or Pycharm, you can use Terminal directly from the bottom of the interface.
pip install Delorean
Basic use
Get the current time easily:
from delorean import Delorean d = Delorean() print(d) # Delorean(datetime=(2021, 10, 6, 9, 5, 57, 611589), timezone='UTC')
Converts time in datetime format to Delorean:
import datetime from delorean import Delorean d = Delorean() print(d) d = Delorean(datetime=(2018, 5, 10, 8, 52, 23, 560811), timezone='UTC') # The default time here is UTC print(d) # Delorean(datetime=(2021, 10, 6, 9, 5, 57, 611589), timezone='UTC') # Delorean(datetime=(2018, 5, 10, 8, 52, 23, 560811), timezone='UTC')
Convert to domestic time zone:
import datetime from delorean import Delorean d = Delorean(datetime=(2018, 5, 10, 8, 52, 23, 560811), timezone='UTC') d = ("Asia/Shanghai") print(d) # Delorean(datetime=(2018, 5, 10, 16, 52, 23, 560811), timezone='Asia/Shanghai')
Outputting datetime and date doesn't matter.
import datetime from delorean import Delorean d = Delorean(datetime=(2018, 5, 10, 8, 52, 23, 560811), timezone='UTC') d = ("Asia/Shanghai") print() print() # 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=(2018, 5, 10, 8, 52, 23, 560811), timezone='UTC') d = ("Asia/Shanghai") print() print() # 1525942343.560811 # 2018-05-10 08:52:23.560811
Initialize Delorean with unix timestamps:
from delorean import epoch d = epoch(1357971038.102223).shift("Asia/Shanghai") print(d) # Delorean(datetime=(2013, 1, 12, 14, 10, 38, 102223), timezone='Asia/Shanghai')
Delorean supports time addition and subtraction using timedelta. delorean can use timedelta to add and subtract to get a delorean object:
import datetime from delorean import Delorean d = Delorean(datetime=(2018, 5, 10, 8, 52, 23, 560811), timezone='UTC') d = ("Asia/Shanghai") print(d) d2 = d + (hours=2) print(d2) d3 = d - (hours=3) print(d3) # Delorean(datetime=(2018, 5, 10, 16, 52, 23, 560811), timezone='Asia/Shanghai') # Delorean(datetime=(2018, 5, 10, 18, 52, 23, 560811), timezone='Asia/Shanghai') # Delorean(datetime=(2018, 5, 10, 13, 52, 23, 560811), timezone='Asia/Shanghai')
Advanced Use
Often we don't care how many subtleties or how many seconds there are, so Delorean provides a very convenient way of filtering:
from delorean import Delorean d = Delorean() print(d) # Delorean(datetime=(2019, 3, 14, 4, 0, 50, 597357), timezone='UTC') ('second') # Delorean(datetime=(2019, 3, 14, 4, 0, 50), timezone='UTC') ('hour') # Delorean(datetime=(2019, 3, 14, 4, 0), timezone='UTC') ('month') # Delorean(datetime=(2019, 3, 1, 0, 0), timezone='UTC') ('year') # Delorean(datetime=(2019, 1, 1, 0, 0), timezone='UTC')
In addition, datetime strings need to be labeled with a variety of formats when they are converted, but in Delorean we don't need to go through all that trouble.parseThat's all:
from delorean import parse parse("2011/01/01 00:00:00 -0700") # Delorean(datetime=(2011, 1, 1, 0, 0), timezone=(-420)) parse("2018-05-06") # Delorean(datetime=(2018, 6, 5, 0, 0), timezone='UTC')
Above is Python + Delorean to achieve intelligent conversion of time format details, more information about Python Delorean time format conversion please pay attention to my other related articles!