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 are the modules of the Python time data type

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "what are the modules of Python time data type". In daily operation, I believe that many people have doubts about the module of Python time data type. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what are the modules of Python time data type?" Next, please follow the editor to study!

1. What is the time data type

The data structure representing the time type in Python is the time data type.

2.time module import time# gets the timestamp of the current time print (time.time ()) # output: 1548742426.169880 tuple t = time.localtime () print (t) # output: time.struct_time (tm_year=2019, tm_mon=1, tm_mday=29, tm_hour=14, tm_min=14, tm_sec=17, tm_wday=1, tm_yday=29 Tm_isdst=0) # converts the current time tuple to a string print (time.asctime (time.localtime () # output: Tue Jan 29 14:15:51 20pm # format string print (time.strftime ("% Y-%m-%d% H:%M:%S") Time.localtime ()) # output: 2019-01-29 14 time.strptime 16V 0V # converts the string to a time tuple print (time.strptime ('2018-11-27 08V 08RV 08V,'% Y-%m-%d% HV% MHD% S') # output: time.struct_time (tm_year=2018, tm_mon=11, tm_mday=27, tm_hour=8, tm_min=8, tm_sec=8, tm_wday=1, tm_yday=331, tm_isdst=-1) # sleep method Will take up cpu time slice print (time.sleep (5)) # 5 seconds later output: None# print calendar import calendarprint (calendar.month (2019, 1)) # output: January 2019 calendar

Timestamp: the number of seconds from the zero time zone 00:00:00 on January 1, 1970 to a given date and time (floating point)

Timestamp acquisition: import the time module using import, and call the time method of the time module. If you don't need the second after the decimal point, the next three decimal places will be milliseconds, and the next three digits will be microseconds. Generally, milliseconds will be fine.

Localtime method: returns the tuple of the current time (including year, month, day, hour, second, etc.)

Asctime method: converts the current time tuple to a string (European and American time format)

Strftime method: formatting strings

Strptime method: contrary to the strftime method, it is used to convert a string into a time tuple

Sleep method: takes up cpu time slices (that is, pauses the entire thread for some time)

Print Calendar: import imports the calendar module, then calls the month method

3.datetime module

The datetime module in python provides operation date and time functions. The five core objects provided by this module are: datetime (time date type), date (date type), time (time type), tzinfo (time zone type), and timedelta (time difference type).

(1) datetime type

From datetime import datetime# 1: build a datetime object with a specified date and time today = datetime (year=2019,month=1,day=29,hour=14,minute=22,second=54) print (today) # output: 2019-01-29 14-22-15-15 get the current date and time Output type: datetimenow = datetime.now () print (now,type (now)) # output: 2019-01-29 14 print 2335.408583 d_now = datetime.now () # datetime type conversion string d_str = d_now.strftime ('% Y-%m-%d% HRV% MVA% S') print (d_str) Type (d_str)) # output: 2019-01-29 14:26:12 # string is converted to datetime type d_type = datetime.strptime (dudes stronometry% Ymuri% Maffe% d% HGV% M15% S') print (d_type) Type (d_type)) # output: 2019-01-29 14:26:12 # calculation timestamp timestamp = d_type.timestamp () print (timestamp) # output: 1548743501.calculation timestamp timestamp = d_type.timestamp () print (timestamp) # output: 1548743935.getting the datetime object d_type = datetime.fromtimestamp (1543408827) print (d_type, type (d_type)) # output: 2018-11-28 20:40:27

Strftime method is used to convert datetime type to character string, and strptime method is used to convert string to datetime type.

Timestamp method: calculate the timestamp

Fromtimestamp method: get the datetime object by timestamp

(2) date type

From datetime import datedata_today = date (year=2018, month=11, day=29) print (data_today) # output: 2018-11-29

Import date module, instantiate date

(3) time type

From datetime import timenow_time = time (hour=8, minute=30, second=10) print (now_time, type (now_time)) # output: 20:30:10

Import time type, instantiate time

(4) timedelta type

The from datetime import datetime and timedelta# intervals can be subtracted to now = datetime.now () before_datatime = datetime (year=2018, month=11, day=20, hour=8, minute=20, second=20) delta = now-before_datatimeprint (delta, type (delta)) # output: 70 days, 6 purposed 22now 37.340041 # can initialize the interval delta_days = timedelta (days=7) print (delta_days, type (delta_days)) # output: 7 days 0:00:00 # you can get the future time through the time interval future_datetime = now + delta_daysprint (future_datetime) # output: 2019-02-05 14-14-43-15-54.582315

The timedelta object represents a time period, and the timedelta object can be instantiated manually or subtracted

(5) tzinfo type

From datetimeimport datetimeimport pytzutc_tz = pytz.timezone ('UTC') print (pytz.country_timezones (' cn')) # shows cities in Chinese time zone # output: ['Asia/Shanghai',' Asia/Urumqi'] print (pytz.country_timezones ('us')) # shows cities in US time zone # # output: [' America/New_York', 'America/Detroit',' America/Kentucky/Louisville', 'America/Kentucky/Monticello' 'America/Indiana/Indianapolis', 'America/Indiana/Vincennes',' America/Indiana/Winamac', 'America/Indiana/Marengo',' America/Indiana/Petersburg', 'America/Indiana/Vevay',' America/Chicago', 'America/Indiana/Tell_City',' America/Indiana/Knox', 'America/Menominee',' America/North_Dakota/Center', 'America/North_Dakota/New_Salem',' America/North_Dakota/Beulah', 'America/Denver' 'America/Boise', 'America/Phoenix',' America/Los_Angeles', 'America/Anchorage',' America/Juneau', 'America/Sitka',' America/Metlakatla', 'America/Yakutat',' America/Nome', 'America/Adak' 'Pacific/Honolulu'] # # get time zone china_tz = pytz.timezone (' Asia/Shanghai') america_tz = pytz.timezone ('America/New_York') # # get city local time china_local_time = datetime.now (china_tz) # East Zone 8 america_local_time = datetime.now (america_tz) # West Zone 5 print (china_local_time) # output: 2019-01-29 14:51: 51.252579+08:00print (america_local_time) # output: 2019-01-29 14 51V 51.252579 08R 00

Install the pytz package: enter the project and execute the pip install pytz command

Get time zone: pytz.timezone (region name)

Get the city local time: datetime.now (time zone name).

At this point, the study of "what are the modules of Python time data types" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Development

Wechat

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

12
Report