Go through python date and time processing

Keywords: Python dict

Official Python column article 33, students stop, don't miss this article starting from 0!

In the previous article, we learned a little about time acquisition in Python. This time, we continue to learn the time zone conversion and formatting of dates.

What other date operations are commonly used in development?

  • Time zone conversion display
  • Date formatting
  • Conversion between seconds and date string

We often use it. For example, global businesses display different times (formats, etc.) according to different customers

In python, there are mainly the following two modules covering common date processing

import time
import calender

Let's look at these two modules.

Type conversion in time processing: struct_time vs str

Create a time in Python, specifically, create a struct_time needs a tuple of 9 elements to construct.

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

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time: 22:49 am, November 10, 2021
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: Thunder Science Committee
# @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 to struct_time, a tuple parameter consisting of 9 elements is required.
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))

The operation effect is as follows:

This ticks is the cumulative number of seconds from time 0 to now.

You can run this program every second, adding 1 (approximate) to the ticks value each time

Specify input to construct time:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time: 22:49 am, November 10, 2021
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: Thunder Science Committee
# @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)

The operation effect is as follows:

Time and string conversion

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time: 22:49 am, November 10, 2021
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: Thunder Science Committee
# @XueWeiTag: CodingDemo
# @File : createtime2.py
# @Project : hello


import time

sec = 3600  # One hour after the beginning of the era (GMT 19700101 AM)

#
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))

Here are the results:

For the time formatting function (strftime), it doesn't care which time zone the struct_time you pass in is, and it still outputs it to you. It's also correct.

However, when we write a program to get data, we must return the time zone information to the client or UI as it is, and finally adjust the display by the local time zone setting of the client.

summary

Python date processing is still quite sufficient. Practice more.

By the way, if you like Python, please pay attention to the school committee Python foundation column or Introduction to Python to master the big column

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

Posted by Silver_Eclipse on Thu, 25 Nov 2021 14:13:50 -0800