current position:Home>Run through Python date and time processing (Part 2)

Run through Python date and time processing (Part 2)

2022-01-31 11:26:01 Lei Xuewei

「 This is my participation 11 The fourth of the yuegengwen challenge 9 God , Check out the activity details :2021 One last more challenge

ceremonial Python Column No 33 piece , Classmate, stop , Don't miss this from 0 The beginning of the article !

In the previous article, we learned a little Python The acquisition of time , This time continue to learn the time zone conversion of dates , Format and so on .

What other date operations are commonly used in development ?

  • Time zone conversion display
  • Date formatting
  • Number of seconds And date And String conversion

We often use , For example, global business shows different times according to different customers ( Format, etc. )

stay python The following two modules cover common date processing

import time
import calender
 Copy code 

Let's look at these two modules .

Type conversion in time processing :struct_time vs str

Python Create a time in , Specifically, create a struct_time Need one 9 Tuples of elements to construct .

asctime The function helps us format this type of time as a string .

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/11/10 22:49  In the morning 
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat:  Lei Xuewei 
# @XueWeiTag: CodingDemo
# @File : createtime.py
# @Project : hello


import time

# fixed time: time.struct_time(tm_year=2021, tm_mon=11, tm_mday=10, tm_hour=22, tm_min=55, tm_sec=11, tm_wday=16, tm_yday=16, tm_isdst=16)
the9fields = (2021, 11, 10, 22, 55, 11, 16, 16, 16)
fixed = time.struct_time(the9fields)
print("fixed time:", fixed)
print("type:", type(fixed))

result = time.asctime(the9fields)  #  similar struct_time, need 9 Tuple parameters composed of elements .
print("asc time:", result)
print("type:", type(result))

localtime = time.localtime()
print("local time:", localtime)
print("type:", type(localtime))
print("asc time:", time.asctime(localtime))
 Copy code 

The operation effect is as follows :

 Screenshot  2021-11-12  In the morning 12.14.14.png

This ticks It's from 0 Calculate all the time , Cumulative seconds to date .

You can run this program every second , Every time ticks Value plus 1( The approximate )

Specify the input to construct the time :

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/11/10 22:49  In the morning 
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat:  Lei Xuewei 
# @XueWeiTag: CodingDemo
# @File : createtime.py
# @Project : hello


import time

#fixed time: time.struct_time(tm_year=2021, tm_mon=11, tm_mday=10, tm_hour=22, tm_min=55, tm_sec=11, tm_wday=16, tm_yday=16, tm_isdst=16)
fixed = time.struct_time((2021, 11, 10, 22, 55, 11, 16, 16, 16))
print("fixed time:", fixed)
 Copy code 

The operation effect is as follows :

 Screenshot  2021-11-12  In the morning 12.14.14.png

Time and string conversion

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/11/10 22:49  In the morning 
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat:  Lei Xuewei 
# @XueWeiTag: CodingDemo
# @File : createtime2.py
# @Project : hello


import time

sec = 3600  #  An hour after the beginning of the era (GMT 19700101 In the morning )

#
gmtime = time.gmtime(sec)
print("gmtime:", gmtime)  # GMT
print("type:", type(gmtime))
print(time.strftime("%b %d %Y %H:%M:%S", gmtime))
print(time.strftime("%Y-%m-%d %H:%M:%S %Z", gmtime))  #  Print date plus time zone 

print("*" * 16)
localtime = time.localtime(sec)
print("localtime:", localtime)  #  Local time 
print("type:", type(localtime))

print(time.strftime("%b %d %Y %H:%M:%S", localtime))
print(time.strftime("%Y-%m-%d %H:%M:%S %Z", localtime))  #  Print date plus time zone 

# Try another format 
print(time.strftime("%D", localtime))
print(time.strftime("%T", localtime))
 Copy code 

Here are the results :

 Screenshot  2021-11-12  In the morning 1.10.05.png

For time formatting functions (strftime) It doesn't care about the time you pass in (struct_time) What time zone is it , Still output to you , It's also true .

But when we write programs to get data , The time zone information must be returned to the client as it is , Or is it UI End , Finally, the display is adjusted by the local time zone setting of the client .

summary

Python Date processing is still quite sufficient , Practice more .

by the way , like Python Friend, , Please pay attention to Python Basic column or Python From getting started to mastering the big column

Continuous learning and continuous development , I'm Lei Xuewei !
Programming is fun , The key is to understand the technology thoroughly .
Welcome to wechat , Like support collection !

copyright notice
author[Lei Xuewei],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201311126003636.html

Random recommended