程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Time module of Python

編輯:Python

Catalog

    • 1. introduction
    • 2. Timestamp and time tuple
      • 2.1 Time stamp
      • 2.2 time tuples
    • 3. Time format
      • 3.1 Get the formatted time
      • 3.2 Custom formatting
    • 4. appendix
      • 4.1 Important functions
      • 4.2 Reference resources

1. introduction

Python Provides a time Module to format the time , stay python Very useful in applications such as crawlers .

time The module contains information for obtaining the current time 、 Operation time and date 、 Read date from string 、 Format the date as a string function . The date can be expressed as since 1970 year 1 month 1 Midnight ( Epoch ) A real number after seconds , It can also be expressed as including 9 A tuple of integers .

2. Timestamp and time tuple

2.1 Time stamp

import time
ticks = time.time()
print (" The current timestamp is :", ticks)
 The current timestamp is : 1645263377.554756

The above case demonstrates the use of time() Get the current timestamp .

2.2 time tuples

quite a lot Python Functions are assembled in one element 9 Group digital processing time :

Indexes attribute Field value 0tm_year year Such as 2000,2022 etc. 1tm_mon month Range 1~122tm_mday Japan Range 1~313tm_hour Hours Range 0~234tm_min minute Range 0~595tm_sec second 0~61 (60~61 It's leap seconds )6tm_wday week Range 0~6 (0 It means Monday )7tm_yday Julian day 1 To 3668tm_isdst Daylight saving time 0、1 or -1N/Atm_zone The time zone Abbreviation for time zone name N/Atm_gmtoffUTC East deviation In seconds

The value range of seconds is 0~61, Consider the leap of one or two seconds . Daylight saving time numbers are Boolean , If you use -1, Indicates unknown , So use mktime() You may get the correct value .

import time
localtime = time.localtime()
print (" The local time is :", localtime)
 The local time is : time.struct_time(tm_year=2022, tm_mon=2, tm_mday=19, tm_hour=17, tm_min=49, tm_sec=21, tm_wday=5, tm_yday=50, tm_isdst=0)

The above example demonstrates how to get the time tuple of the current time .

localtime() The default value is time() The current timestamp returned .

3. Time format

3.1 Get the formatted time

You can choose various formats according to your needs , But the simplest function to get a readable time pattern is asctime():

import time
localtime = time.asctime(time.localtime())
print (" The local time is :", localtime)
 The local time is : Sat Feb 19 17:59:17 2022

It can also be used directly here asctime(), because asctime() The default value is localtime() Current time returned .

Can also be used ctime() , ctime() Equivalent to asctime(localtime()).

3.2 Custom formatting

We can use time Modular strftime Method to format the date :time.strftime(format[, t]), The first parameter is the format string , The second parameter is the time tuple .

import time
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
print(time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()))
2022-02-19 18:28:08
Sat Feb 19 18:28:08 2022

The above example demonstrates how to format the local time .

Format symbol meaning %a Localized abbreviation for the name of the day of the week .%A Full name of the day of the week in localization .%b Localized month abbreviation name .%B Localized month full name .%c The appropriate date and time representation for localization .%d Decimal number [01,31] Indicates the day of the month .%H Decimal number [00,23] Hours represented (24 hourly ).%I Decimal number [01,12] Hours represented (12 hourly ).%j Decimal number [001,366] It means mid year day .%m Decimal number [01,12] Indicates the month .%M Decimal number [00,59] Indicates the minutes of the .%p Localized AM or PM .%S Decimal number [00,61] Represents the seconds .%U Decimal number [00,53] The number of weeks in a year ( Sunday is the first day of the week ). All days of the new year before the first Sunday are considered to be on the 0 Zhou .%w Decimal number [0( Sunday ),6] The day of the week .%W Decimal number [00,53] The number of weeks in a year ( Monday as the first day of the week ). All days of the new year before the first Monday are considered to be on the 0 Zhou .%x The appropriate date for localization means .%X Appropriate time representation of localization .%y Decimal number [00,99] Indicates a year without a century .%Y A year with a century represented by a decimal number .%z Decimal signed number [-1200,+1200] Time zone .%Z Time zone name .%% Literally ‘%’ character .

4. appendix

4.1 Important functions

function describe asctime([t]) Convert time tuples to strings ctime([secs]) Convert seconds to strings localtime([secs]) Convert seconds to time tuples of local time gmtime([secs]) Convert seconds to UTC Time tuple of time mktime(t) Convert time tuples to local time sleep(secs) Sleep secs second strftime(format[, t]) Convert string to time tuple time() current time , Seconds since the era

4.2 Reference resources

Official documents :https://docs.python.org/zh-cn/3/library/time.html


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved