current position:Home>Python -- datetime (timedelta class)

Python -- datetime (timedelta class)

2022-02-01 05:35:19 Mourners study in the cold palace

「 This is my participation 11 The fourth of the yuegengwen challenge 5 God , Check out the activity details :2021 One last more challenge 」 Get the real-time time of the page in the previous period , The time of the page is CT Time , What we need to verify is the actual Beijing time , There is a time conversion , datetime The module solves this problem

Basics

Encounter unfamiliar content , I like to see the official documents and source code implementation by myself , Look at the source code written by others , It is very helpful for your coding ability , Sometimes I don't just watch too little , This is the official document link docs.python.org/zh-cn/3/lib…

We can see from the official documents that ,datetime Module and time Modules are classes that deal with dates and times , While supporting the mathematical operation of date and time , Focus more on how to effectively parse its properties for formatting output and data operations .

timedelta class

To sum up timedelta Use , Look at the source

carbon.png

There is a lot of use in the source code python Of typing, Want to know typing You can refer to my previous articles juejin.cn/post/700029…

This class mainly combines other time objects to perform various operations of time , It has days, seconds, mircroseconds,milliseconds Equal parameter , The default values of these parameters are 0, We can arbitrarily assign some or all of these parameters to integers or floating-point numbers , It can also be positive or negative , Only one containing days, seconds, microseconds( subtle ) Class object of , Other parameters will be converted according to a standard The conversion rules of number units are as follows :

  • 1 Milliseconds will be converted to 1000 Microsecond .
  • 1 Minutes will be converted to 60 second .
  • 1 Hours will be converted into 3600 second .
  • 1 Week will be converted to 7 God .

give an example

 picture .png

application

The time zone

Combined with the example at the beginning , Beijing time and CT The mutual transformation of time , Beijing time and CT Time difference 13 Hours Then we can give timedelta Parameters of hours Assign a negative number 13, Get a time difference Specific examples

 picture .png

For the example of changing time zones , You can also use astimezone, Additional libraries are needed at this time pytz

 picture .png

Add or subtract... For the current time

 picture .png

summary

If you roughly estimate the running time difference of the program , We just use datetime.datetime.now() Method to get the time before and after the program runs , Then subtract , If you have some special processing for time, such as time zone, you can use timedelta better

copyright notice
author[Mourners study in the cold palace],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202010535177603.html

Random recommended