In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use the time module and calendar module in Python. Xiaobian thinks it is quite practical, so share it with you as a reference. I hope you can gain something after reading this article.
There are many ways to handle time and date in Python, and converting dates is one of the most common. Time intervals in Python are floating-point decimals in seconds.
1. Time stamp
Python is basically a timestamp to save the current time. Timestamp units are best suited for date operations. However, dates prior to 1970 cannot be represented in this way. UNIX and Windows only support the year 2038.
Time stamp refers to the total number of seconds from 01 January 1970 00:00:00 GMT to the present. In layman's terms, a timestamp is a complete verifiable piece of data that indicates that a piece of data existed at a particular point in time. It is proposed mainly to provide users with an electronic evidence to prove the generation time of certain data of users.
Python uses the time function of the time module to get the current timestamp
Example code:
import timetime_stamp = time.time()print("The current timestamp is: ", time_stamp) #The current timestamp is: 1590585400.68089062, time tuple
Many functions in Python deal with time by assembling nine sets of numbers from a single element.
Sequence attribute values0tm_year20081tm_mon1 to 122tm_mday1 to 313tm_hour0 to 234tm_min0 to 595tm_sec0 to 61 (60 or 61 is a leap second)6tm_wday0 to 6 (0 is Monday)7tm_yday What day of the year, 1 to 3668tm_isdst is daylight saving time, values are: 1(daylight saving time), 0(not daylight saving time),-1(unknown), default-1
Participate in rookie tutorials
3. Get the current time
Converting from the returned timestamp to a time tuple can be done using the localtime() function of the time module; time.gmtime([secs]) also returns a time tuple
Example code:
import timetime_stamp = time.time() #Get 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. Format time
The asctime function of the time module can be used to format the time tuple into the simplest readable pattern, indicating the current time without parameters
time.ctime([secs]) This parameter accepts timestamps as units, returns readable patterns of dates, and does not give parameters equivalent to time.asctime()
Example code:
import timetime_stamp = time.time()localtime = time.asctime(time.localtime(time_stamp))print("current time is: ", localtime) #current time is: Wed May 27 21:47:48 20205, formatted date
Date formatting symbols in Python:
Symbol Description %y Two-digit Year (00-99)%Y Four-digit Year (0000-9999)%m Month (01-12)%d Day of Month (1-31)%H24 Hours (0-23)%I12 Hours (01-12) %M Minutes (00=59) %S Seconds (00- 59) %a Local Simplified Week Name %A Local Full Week Name %b Local Simplified Month Name %B Local Full Month Name %c Local Corresponding Date and Time Representation % j Day of the year (001-366) %p Equivalent of local A.M. or P.M.%U Number of weeks of the year (00-53) Sunday is the beginning of the week %w Week (0-6), Sunday is the beginning of the week %W Number of weeks of the year (00-53) Monday is the beginning of the week %x Local corresponding date representation %X Local corresponding time representation %Z Name of current time zone %%% sign itself
The time mktime(structured time or full 9-byte element) function performs the inverse of gmtime() , localtime() by taking a struct_time object as an argument and returning a floating-point number representing time in seconds. If the value entered is not a valid time, OverflowError or ValueError will be triggered.
Example code:
import timetime_stamp = time.time()print (time_stamp)#1590590683.0062041#4-digit year-month-day24-hour clock: minutesDay of the weekDay of the yearlocaltime = 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 the time tuple to seconds (timestamp) time_stamp = time.mktime(localtime_tuple)print(time_stamp) # 1590590683.0 #Basically equal to the one obtained at the beginning 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 entire system. Since 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 sum of the CPU time spent executing the current process, excluding sleep time. Since the reference point of the return value is undefined, only the difference between the results of successive calls is valid.
The time.sleep() function delays the running of the calling thread, which can be indicated by the secs parameter to indicate the number of seconds, indicating the time when the process is suspended.
Example code:
import time#Get the time the system runs the function print (time.perf_counter()) # 0.0208446time.sleep(2)#Read the time the system runs the function, print (time.perf_counter()) # 2.0208952 #The difference between the two is very small #Get the total time of the current process execution CPU print(time.process_time()) # 0.015625 #Does not include sleep time 7. Calendar module
Calendar module, the functions in this module are calendar-related, such as printing a month's character calendar
calendar.calendar(year,w=2,l=1,c=6) Returns a multi-line annual calendar in string format, 3 months a line, spaced c apart. The daily width interval is w characters. Each row is 21* W+18+2* C. l is the number of rows per week. calendar.month(year,month,w=2,l=1) Returns a multi-line string format for the calendar year month, two lines for the title, and one line for the week. The daily width interval is w characters. The length of each row is 7* w+6. l is the number of rows per week. calendar.monthrange(year,month) Returns two integers. The first is the day of the month and the second is the number of days. The day of the week is from 0 (Monday) to 6 (Sunday). calendar.leapdays(y1,y2) Returns the total number of leap years between Y1 and Y2. calendar.isleap(year) Determines whether it is a leap year, returns True if it is a leap year, false otherwise.
Example code:
import calendar#Print this year's calendar print (calendar.calendar(2020))#Print calendar of the month print (calendar.month(2020, 5))# monthrange method print (calendar.monthrange(2020, 5)) # (4, 31) #The first day of May is Friday. There are 31 days in total. Because Monday is 0, 4 is Friday #Calculate the total number of leap years from 1000 to 2000 print (calendar.leapdays(1000, 2000)) # 242#determine whether this year is a leap year print(calendar.isleap(2020)) # True
time.strftime(fmt[,tupletime]) receives a tuple in time and returns the local time as a readable string, formatted by fmt.
time.strptime(str,fmt ='% a %b %d %H:%M:%S % Y') Parses a time string into time tuples according to the format of fmt.
About "Python time module and calendar module how to use" this article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.