加入收藏 | 设为首页 | 会员中心 | 我要投稿 汽车网 (https://www.0577qiche.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

20 Python 3

发布时间:2023-04-14 12:32:41 所属栏目:教程 来源:
导读:A Python program can handle date and time in several ways. Converting between date formats is a common chore for computers. Python's time and calendar modules help track dates and times.

What i
A Python program can handle date and time in several ways. Converting between date formats is a common chore for computers. Python's time and calendar modules help track dates and times.

What is Tick?
Time intervals are floating-point numbers in units of seconds. Particular instants in time are expressed in seconds since 12:00am, January 1, 1970(epoch).

There is a popular time module available in Python which provides functions for working with times, and for converting between representations. The function time.time() returns the current system time in ticks since 12:00am, January 1, 1970(epoch).

Example
Live Demo
#!/usr/bin/python3
import time;      # This is required to include time module.
 
ticks = time.time()
print ("Number of ticks since 12:00am, January 1, 1970:", ticks)
This would produce a result something as follows −

Number of ticks since 12:00am, January 1, 1970: 1455508609.34375
Date arithmetic is easy to do with ticks. However, dates before the epoch cannot be represented in this form. Dates in the far future also cannot be represented this way - the cutoff point is sometime in 2038 for UNIX and Windows.

What is TimeTuple?
Many of the Python's time functions handle time as a tuple of 9 numbers, as shown below −

Index    Field    Values
0    4-digit year    2016
1    Month    1 to 12
2    Day    1 to 31
3    Hour    0 to 23
4    Minute    0 to 59
5    Second    0 to 61 (60 or 61 are leap-seconds)
6    Day of Week    0 to 6 (0 is Monday)
7    Day of year    1 to 366 (Julian day)
8    Daylight savings    -1, 0, 1, -1 means library determines DST
For Example −

Live Demo
import time
 
print (time.localtime());
This would produce a result as follows −

time.struct_time(tm_year = 2016, tm_mon = 2, tm_mday = 15, tm_hour = 9, 
   tm_min = 29, tm_sec = 2, tm_wday = 0, tm_yday = 46, tm_isdst = 0)
The above tuple is equivalent to struct_time structure. This structure has following attributes −

Index    Attributes    Values
0    tm_year    2016
1    tm_mon    1 to 12
2    tm_mday    1 to 31
3    tm_hour    0 to 23
4    tm_min    0 to 59
5    tm_sec    0 to 61 (60 or 61 are leap-seconds)
6    tm_wday    0 to 6 (0 is Monday)
7    tm_yday    1 to 366 (Julian day)
8    tm_isdst    -1, 0, 1, -1 means library determines DST
Getting current time
To translate a time instant from seconds since the epoch floating-point value into a timetuple, pass the floating-point value to a function (e.g., localtime) that returns a time-tuple with all valid nine items.

Live Demo
#!/usr/bin/python3
import time
 
localtime = time.localtime(time.time())
print ("Local current time :", localtime)
This would produce the following result, which Could be formatted in any other presentable form −

Local current time : time.struct_time(tm_year = 2016, tm_mon = 2, tm_mday = 15, 
   tm_hour = 9, tm_min = 29, tm_sec = 2, tm_wday = 0, tm_yday = 46, tm_isdst = 0)
Getting formatted time
You can format any time as per your requirement, but a simple method to get time in a readable format is asctime() −

Live Demo
#!/usr/bin/python3
import time
 
localtime = time.asctime( time.localtime(time.time()) )
print ("Local current time :", localtime)
This would produce the following result −

Local current time : Mon Feb 15 09:34:03 2016
Getting calendar for a month
The calendar module gives a wide range of methods to play with yearly and monthly calendars. Here, we print a calendar for a given month ( Jan 2008 ) −

Live Demo
#!/usr/bin/python3
import calendar
 
cal = calendar.month(2016, 2)
print ("Here is the calendar:")
print (cal)
This would produce the following result −

Here is the calendar:
   February 2016
Mo Tu We Th Fr Sa Su
 1  2  3  4  5  6  7
 8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29

(编辑:汽车网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章