time Module and calendar Module in Python
- 2021-12-12 04:58:05
- OfStack
In
Python
There are many ways to deal with time and date in, among which converting date is the most common function.
Python
The time interval in is a floating-point decimal in seconds.
1. Timestamp
Python
Basically, the time stamp is used to save the current time. The timestamp unit is most suitable for date calculation. But the date before 1970 can't be expressed by this. Not even a date too far away,
UNIX
And
Windows
Support only to
2038
Year.
The timestamp is the total number of seconds from 00:00 GMT on January 1, 1970 to the present. Generally speaking, a timestamp is a complete verifiable data that can indicate that a data already exists at a specific time point. It is proposed mainly to provide users with an electronic evidence to prove the generation time of some data of users.
Python
Use in
time
Modular
time
Function to get the current timestamp
Sample code:
import time
time_stamp = time.time()
print(" The current timestamp is: ", time_stamp) # The current timestamp is: 1590585400.6808906
2. Time tuple
Python
Many functions in are composed of 9 sets of numbers with 1 meta to deal with time.
序号 | 属性 | 值 |
---|---|---|
0 | tm_year | 2008 |
1 | tm_mon | 1 到 12 |
2 | tm_mday | 1 到 31 |
3 | tm_hour | 0 到 23 |
4 | tm_min | 0 到 59 |
5 | tm_sec | 0 到 61 (60或61 是闰秒) |
6 | tm_wday | 0到6 (0是周1) |
7 | tm_yday | 1年中的第几天,1 到 366 |
8 | tm_isdst | 是否为夏令时,值有:1(夏令时)、0(不是夏令时)、-1(未知),默认 -1 |
3. Get the current time
Converting from the returned timestamp to a time tuple can be used
time
Modular
localtime()
Function;
time.gmtime([secs])
Also returns 1 time tuple
Sample code:
import time
time_stamp = time.time() # Get the current timestamp
localtime = time.localtime(time_stamp)
print(localtime)
# time.struct_time(tm_year=2020, tm_mon=5, tm_mday=27, tm_hour=21, tm_min=36, tm_sec=42, tm_wday=2, tm_yday=148, tm_isdst=0)
4. Formatting time
You can use the
time
Modular
asctime
Function to format the time tuple into the simplest readable mode, denoting the current time without a parameter
time.ctime([secs])
This parameter accepts the timestamp as the unit and returns the readable mode of the date, and does not give the parameter equivalent to the
time.asctime()
Sample code:
import time
time_stamp = time.time()
localtime = time.asctime(time.localtime(time_stamp))
print(" The current time is: ", localtime) # The current time is: Wed May 27 21:47:48 2020
5. Format the date
Date formatting symbols in Python:
符号 | 描述 |
---|---|
%y | 两位数的表示年份(00-99) |
%Y | 4位数的表示年份(0000-9999) |
%m | 月份(01-12) |
%d | 月内中的某1天(1-31) |
%H | 24小时制小时数(0-23) |
%I | 12小时制小时数(01-12) |
%M | 分钟数(00=59) |
%S | 秒(00-59) |
%a | 本地简化星期名称 |
%A | 本地完整星期名称 |
%b | 本地简化的月份名称 |
%B | 本地完整的月份名称 |
%c | 本地相应的日期表示和时间表示 |
%j | 年内的1天(001-366) |
%p | 本地A.M.或P.M.的等价符 |
%U | 1年中的星期数(00-53)星期天为星期的开始 |
%w | 星期(0-6),星期天为星期的开始 |
%W | 1年中的星期数(00-53)星期1为星期的开始 |
%x | 本地相应的日期表示 |
%X | 本地相应的时间表示 |
%Z | 当前时区的名称 |
%% | %号本身 |
time mktime
(Structured time or full 9-byte element) function executes the same as
gmtime()
,
localtime()
The opposite operation, which receives
struct_time
Object as a parameter, returning a floating-point number with time expressed in seconds. If the value entered is not a legal time, OverflowError or OverflowError or OverflowError or OverflowError is triggered
ValueError
.
Sample code:
import time
time_stamp = time.time()
print(time_stamp) # 1590590683.0062041
# 4 Year of digits - Month - Days 24 Hours: Minutes: Seconds Day of the week What day of this year
localtime = time.strftime("%Y-%m-%d %H:%M:%S %A %j", time.localtime(time_stamp))
print(localtime) # 2020-05-27 22:44:43 Wednesday 148
# Convert to time tuple
localtime_tuple = time.strptime(localtime, "%Y-%m-%d %H:%M:%S %A %j")
print(localtime_tuple) # time.struct_time(tm_year=2020, tm_mon=5, tm_mday=27, tm_hour=22, tm_min=44, tm_sec=43, tm_wday=2, tm_yday=148, tm_isdst=-1)
# Convert time tuples to seconds (timestamps)
time_stamp = time.mktime(localtime_tuple)
print(time_stamp) # 1590590683.0 # And 1 Begin to get basic equality
6. Get CPU time
time.perf_counter()
Returns the exact time of the timer (the running time of the system), including the sleep time of the whole system. Because the reference point of the return value is undefined, only the difference between the results of successive calls is valid.
time.process_time()
Returns the current process execution
CPU
The total time of, excluding sleep time. Because the reference point of the return value is undefined, only the difference between the results of successive calls is valid.
time.sleep()
Function to delay the running of the calling thread, you can use the parameter
secs
The number of seconds that indicates how long the process has been suspended.
Sample code:
import time
# Gets the time when the system runs this function
print(time.perf_counter()) # 0.0208446
time.sleep(2)
# Read the time when the system ran the function,
print(time.perf_counter()) # 2.0208952 # The gap between the two is very small
# Gets the current process execution CPU The sum of time of
print(time.process_time()) # 0.015625 # Excluding sleep time
7. Calendar module
Calendar
Calendar module, the functions in this module are all calendar-related, such as printing the character monthly calendar of a certain month
函数 | 描述 |
---|---|
calendar.calendar(year,w=2,l=1,c=6) | 返回1个多行字符串格式的year年年历,3个月1行,间隔距离为c。 每日宽度间隔为w字符。每行长度为21* W+18+2* C。l是每星期行数。 |
calendar.month(year,month,w=2,l=1) | 返回1个多行字符串格式的year年month月日历,两行标题,1周1行。每日宽度间隔为w字符。每行的长度为7* w+6。l是每星期的行数。 |
calendar.monthrange(year,month) | 返回两个整数。第1个是该月的星期几,第2个是该月有几天。星期几是从0(星期1)到 6(星期日)。 |
calendar.leapdays(y1,y2) | 返回在Y1,Y2两年之间的闰年总数。 |
calendar.isleap(year) | 判断是否为闰年,是闰年返回 True,否则为 false。 |
import calendar
# Print this year's calendar
print(calendar.calendar(2020))
# Print the calendar of this month
print(calendar.month(2020, 5))
# monthrange Method
print(calendar.monthrange(2020, 5)) # (4, 31) # 5 The first of the month 1 The day is a week 51 Altogether 31 Days, because weeks 1 Yes 0 So 4 It's a week 5
# Calculation 1000 Year to 2000 Total number of leap years in
print(calendar.leapdays(1000, 2000)) # 242
# Judge whether this year is a leap year
print(calendar.isleap(2020)) # True
time.strftime(fmt[,tupletime])
Receives a time tuple and returns a local time in a readable string, in a format determined by fmt.
time.strptime(str,fmt='%a %b %d %H:%M:%S %Y')
According to
UNIX
0
Parses a time string into a time tuple in the format of.