Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the meaning of time tuple and time date in Python

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge about "what is the meaning of time tuple and time date in Python". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

Time tuple (time.struct_time)

Python time tuple is a relatively important type, through the time tuple we can get, the day of the week, the day of the year and other information.

index attribute attribute meaning attribute value 0tm_year4 digit year0000 -99991tm_mon month1 - 122tm_mday1 -313tm_hour hour0 - 234tm_minmin0 -595tm_sec seconds0 -61, 60, 61 leap seconds6tm_wday0 -6, 0 is monday7tm_ydayDay day1 to 366, 366 leap year8tm_isdstDaylight Saving Identification1-Daylight Saving Time, 0-NonDaylight Saving Time-1-Uncertain, Probably because it was created manually.

import time# localtime = time.localtime()# time.struct_time(tm_year=2019, tm_mon=11, tm_mday=3, tm_hour=9, tm_min=8, tm_sec=21, tm_wday=0, tm_yday=307, tm_isdst=0)print(type(localtime))print(localtime)for attr in localtime: print(attr) 2. Time formatting

Time formatting is a very common feature, whether it is from string to struct_time, date, datetime, or struct_time, date, datetime to string.

Format mainly involves two functions: strftime: str represents a string, f is format, time is time, that is, the format is a string strptime: str represents a string, p is parse, time is time, that is, the string is parsed into time

Both time and datetime have these two functions, which will be introduced later. Let's first look at the meaning of formatting.

Format Meaning %y Two-digit year representation, 00-99%Y Four-digit year representation, 0000-9999%m Month, 01-12%d Date, 0-31%H24 Hours, 0-23%I12 Hours, 01-12%M Minutes, 00-59%S Seconds, 00-59%a Local Simplified Name of the Week %A Local Full Name of the Week %b Local Simplified Name of the Month %B Local Full Name of the Month %c Local Date and Time Representation %j Day of Day, 001-366%p Local A.M\P.M%U Week of Day, 00 -53, Sunday is the beginning of the week %W Week of Day, 00 - 53, Monday is the beginning of the week %w Day of Day, 0-6, Sunday is the beginning of the week %x local date %X local time %Z name of current time zone %%% matches because it is treated as an escape character

III. Time

The time module mainly deals with the formatting and parsing of time tuples (struct_time).

There are also functions for localization time, get timestamp, program hibernation, etc.

Note the distinction between datetime and datetime.time. datetime.time is the time part of date and time in the general sense, mainly hours, minutes and seconds.

import time#Greenwich astronomical time tuple print (time.gmtime())print (time.gmtime(1577851199))#local time tuple print (time.localtime())print (time.localtime(1577851199))#formatted time # time.asctime ([tupletime])print (time.asctime())# time.ctime([secs])print (time.ctime())#formatted as 2020-01-01 11:59:59 form print (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())#Convert format string to timestamp a = "2020-01-01 11:59:59"str2TT = time.strptime(a, "%Y-%m-%d %H:%M:%S")#Timetuples to timestamp ts = time.mktime(str2TT)print(ts)#Timestamp print(time.time())#Sleep seconds time.sleep(1)

datetime module mainly uses: datetime.date: date (2025-01-01) datetime.time: time (12:00:00) datetime.datetime: period (2025-01-01 12:00:00)

import datetimeimport timetoday = datetime.date.today ()print(today)print (type(today))#day of the week, isoweekday Sunday is the first day, weekday Monday is the first day print (today.isoweekday())print (today.weekday())#time tuple print (today.timetuple())#day number print (today.toordinal())datetime.date.fromordinal (today.toordinal())print(datetime.date(2020, 1, 1))datetime.date.fromtimestamp(time.time())print(datetime.time(12, 59, 59, 999))#nowlocaldateprint (datetime.datetime.now ())#nowutc dateprint (datetime.datetime.utcnow())print(datetime.datetime(2020, 1, 31, 12, 59, 59, 0))#string to date print(datetime.datetime.strptime ('2020 -1-1 12: 01','%Y-%m-%d %H:% M'))#date formatting print(datetime.datetime.now ().strftime ('% Y-%m-%d %H:% M'))

The most commonly used are:

#String get date datetime.datetime.strptime ('2020 -1-1 12: 01','%Y-%m-%d %H:% M')#Date format datetime.datetime.now().strftime ('% Y-%m-%d %H:%M:% S')

Timedelta is also under the datetime module, mainly used for date calculation.

This is useful when you need to use a time interval, such as the previous month, day, week, etc., which can be easily obtained using datetime and timedelta.

import datetimeimport timetoday = datetime.date.today ()#Last day of last month lastMonthLastDay = datetime.date(today.year, today.month, 1) -datedelta (1)print(lastMonthDay)#First day of last month lastMonthFirstDay = datetime.date (lastMonthLastDay.year, lastMonthLastDay.month, LastMonthDay 1)print(lastMonthFirstDay)#a week ago print(today - datetime.timedelta(7))#thisMonday = today - datetime.timedelta(today.isoweekday()-1)print(thisMonday)#thisSunday = thisMonday - datetime.timedelta(7)print(lastMonday)#lastSunday = today -datetime.timedelta(days=today.isoweekday())print(lastSunday)#Last Monday lastMonday = lastSunday - datetime.timedelta(days=6)print(lastMonday)# 8 hours later print(datetime.datetime.now () + datetime.timedelta(hours=8))start = datetime.datetime.now ()time.sleep(1)end = datetime.datetime.now()diff = end - start# print(type(diff))print(diff) VI. calendarimport calendar#whether it is a leap year print(calendar.isleap(2020))

calendar is calendar

"What do time tuples and time dates mean in Python" is introduced here. Thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report