SoFunction
Updated on 2024-10-29

python time module timestamps and structured time in detail

time module

1: Overview

Classification of Time Representation

timestamp

Formatted time string

Structured time

Timestamp: Timestamp represents the offset from 0:00 on January 1, 1970 to the current second, the data type is floating point, mainly used to let the computer to see the

Formatted time strings: e.g. 2019-01-26, etc. , the data type is a string, which is mainly human-readable

Structured Time: Structured time groups time, such as year, month, day, hour, minute, second, etc. The data type is a meta-anchor, which mainly does time conversions

Importing related libraries

import time

1. Time stamps

1.1 ()

() can be obtained as a timestamp . That is, the offset from 0:0:0 on January 1, 1970 to the present time s

t1 = ()
print('t1:', t1)

1.2 Time stamp to string

Convert timestamps toA fixed format strings, you can use the () method. (but less commonly used)

t = (())
print(t)

            

2. Structuring time

Using structured time is easier for us

2.1 Local time

Local time is related to the time zone.

t2 = (())  
print("t2:", t2)

2.2 Greenwich Mean Time (Zero Time Zone)

Greenwich Mean Time (0 time zone) is exactly 8 hours behind Beijing time (global benchmark, use this time to eliminate the effect of writing code in different places)

t3 = (())
print("t3:", t3)

2.3 Structured time to string

The string form is the one generally acceptable to humans

t4 = ("%Y/%m/%d %H:%M:%S", t2)
print(t4)
print(type(t4))

2.4 String to structured time

Reverse of the above

t5 = ('2022/06/22 20:30:35', "%Y/%m/%d %H:%M:%S")
print(t5)

2.5 Attributes of structured data

print("Year:", t2.tm_year)
print("Moon:", t2.tm_mon)
print("Day:", t2.tm_mday)
print("Time.", t2.tm_hour)
print("Points.", t2.tm_min)
print("Seconds.", t2.tm_sec)
print("Days elapsed this week:", t2.tm_wday)
print("Number of days elapsed this year:", t2.tm_yday)
print("Is it daylight saving time?", t2.tm_isdst)

2.6 Structured time to timestamp

t7 = (t2)
print(t7)

to this article on the python time module timestamp and structured time of the article is introduced to this, more related python time module content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!