Python study notes -Python date and time

Python provides a time and calendar module can be used to format the date and time.

Interval is in seconds of floating point decimals.

Each time stamps are to midnight since January 1970 1 (epoch) after how long to represent.

There are many common functions can be converted to date format in Python's time module. The function of the time.time () to get the current time stamp, the following examples:

Example (Python 2.0+)

实例(Python 2.0+)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
import time;  # 引入time模块
 
ticks = time.time()
print "当前时间戳为:", ticks

Examples of the above output:

The current timestamp: 1,459,994,552.51

Stamp unit is best suited to do date arithmetic. But the date can not be before 1970, as an expression . The date does not work too far away, UNIX, and Windows support only to 2038.


What is the time tuple?

Many Python function with one yuan assembled set of digital processing time of 9 :

No. Field value
0 4-Digit Year 2008
1 month 1-12
2 day 1-31
3 hour 0-23
4 minute 0-59
5 second 0-61 (60 or 61 leap seconds)
6 The first few days of the week 0-6 (0 is Monday)
7 The first few days of the year 1-366 (Julian calendar)
8 summer time -1, 0, 1, -1 is determined whether the flag when daylight saving time

Above is struct_time tuple. This structure has the following properties:

No. Attributes value
0 tm_year 2008
1 tm_mon 1-12
2 tm_mday 1-31
3 tm_hour 0-23
4 tm_min 0-59
5 tm_sec 0-61 (60 or 61 leap seconds)
6 tm_wday 0-6 (0 is Monday)
7 tm_yday 1-366 (Julian calendar)
8 tm_isdst -1, 0, 1, -1 is determined whether the flag when daylight saving time

Get the current time

Tuple conversion from floating point to return to the time stamp mode, as long as the transfer function localtime to float or the like.

Example (Python 2.0+)

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
import time
 
localtime = time.localtime(time.time())
print "本地时间为 :", localtime

Examples of the above output:

Local time: time.struct_time (tm_year = 2016, tm_mon = 4, tm_mday = 7, tm_hour = 10, tm_min = 3, tm_sec = 27, tm_wday = 3, tm_yday = 98, tm_isdst = 0)

Get formatted time

The simplest mode of acquiring time-readable functions are asctime ():

Example (Python 2.0+)

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
import time
 
localtime = time.asctime( time.localtime(time.time()) )
print "本地时间为 :", localtime

Examples of the above output:

Local time: Thu Apr 7 10:05:21 2016

Date Format

We can use the time module strftime method to format dates :

time.strftime(format[, t])

Examples of presentations:

Example (Python 2.0+)

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
import time
 
# 格式化成2016-03-20 11:45:39形式
print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 
 
# 格式化成Sat Mar 28 22:24:24 2016形式
print time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()) 
  
# 将格式字符串转换为时间戳
a = "Sat Mar 28 22:24:24 2016"
print time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y"))

Examples of the above output:

10:25:09 2016-04-07 
Thu, Apr 07, 10:25:09 2016, the 
1459175064.0

python, date and time format symbols :

  • % Y represents two-digit year (00-99)
  • % Y represents a four-digit year (000-9999)
  • % M (01-12)
  • Within a% d day of the month (0-31)
  • % H 24 hours (0-23) manufactured by h
  • % I 12 hours hour (01-12)
  • % M number of minutes (00 = 59)
  • % S seconds (00-59)
  • % A week simplify local name
  • % A full weekday name local
  • % B local simplify month name
  • % B Full month name of the local
  • % C represents the corresponding date and time represent the local
  • One day (001-366)% j years
  • % P local AM or PM equivalent character
  • % U week number of the year (00-53) for the week beginning Sunday
  • % W week (0-6) Sunday for the start of the week
  • % W week number of the year (00-53) for the week beginning Monday
  • % X indicates the corresponding local date
  • % X indicates the corresponding local time
  • Name% Z current time zone
  • %%% Number itself

Gets a calendar month

Calendar module has a method for processing a wide range of almanac and calendar , such as printing of a calendar month:

Example (Python 2.0+)

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
import calendar
 
cal = calendar.month(2016, 1)
print "以下输出2016年1月份的日历:"
print cal

Examples of the above output:

The following output calendar January 2016: 
    January 2016 
of Mo Tu We Th Fr of Sa Su 
             . 1 2. 3 
 . 4. 5. 6. 7. 8. 9 10 
. 11. 17 16 15 14 12 is 13 is 
18 is 20 is 21 is 22 is 23 is 24. 19 
25 28 29 30 31 is 26 is 27

Time Module

Time Module contains the following built-in function, both the processing time, but also the format conversion time:

No. Functions and Description
1 time.altzone
offset the number of seconds of daylight saving time returns west of Greenwich area. If the area is returned in the east of Greenwich negative (such as Western Europe, including the UK). Enable the region to use daylight saving time right.
2 time.asctime ([tupletime])
receiving time of tuples and returns a readable form for the "Tue Dec 11 18:07:14 2008" (December 11, 2008 Tuesday 18:07:14) of 24 characters String.
3 time.clock ()
to return the current floating-point computations seconds of CPU time. Different procedures used to measure time-consuming than time.time () is more useful.
4 time.ctime ([secs])
acts as asctime (localtime (secs)), is not equivalent to the asctime parameter ()
5 time.gmtime ([secs])
receiving time stamp (elapsed float era 1970 seconds) and returns the time in GMT time astronomical tuple t. Note: t.tm_isdst always 0
6 time.localtime ([secs])
receiving time stamp (elapsed float era 1970 seconds) and returns the tuple's local time (local daylight saving time is not t.tm_isdst preferably 0 or 1, depending on) t .
7 time.mktime (tupletime)
receiving time stamp tuple and returns (through the 1970 era float seconds).
8 time.sleep (secs)
to postpone the calling thread is running, secs refers to the number of seconds.
9 time.strftime (fmt [, tupletime])
received in a time tuple, and return the local time format readable string representation is determined by the fmt.
10 time.strptime (str, fmt = '%
a% b% d% H:% M:% S% Y') in accordance with the format fmt time string parsing a tuple of time.
11 time.time ()
Returns the timestamp of the current time (post-1970 era through the floating-point number of seconds).
12 time.tzset ()
according to TZ environment variable to re-initialize the time-related settings.

Time Module includes the following two very important properties:

No. Properties and description
1 time.timezone
property time.timezone local time zone (daylight saving time does not start) offset from Greenwich number of seconds (> 0, the Americas; <= 0 most of Europe, Asia, Africa).
2 time.tzname
属性time.tzname包含一对根据情况的不同而不同的字符串,分别是带夏令时的本地时区名称,和不带的。

 


日历(Calendar)模块

此模块的函数都是日历相关的,例如打印某月的字符月历。

星期一是默认的每周第一天,星期天是默认的最后一天。更改设置需调用calendar.setfirstweekday()函数。模块包含了以下内置函数:

序号 函数及描述
1 calendar.calendar(year,w=2,l=1,c=6)
返回一个多行字符串格式的year年年历,3个月一行,间隔距离为c。 每日宽度间隔为w字符。每行长度为21* W+18+2* C。l是每星期行数。
2 calendar.firstweekday( )
返回当前每周起始日期的设置。默认情况下,首次载入 calendar 模块时返回 0,即星期一。
3 calendar.isleap(year)

是闰年返回 True,否则为 False。

>>> import calendar
>>> print(calendar.isleap(2000))
True
>>> print(calendar.isleap(1900))
False
4 calendar.leapdays(y1,y2)
返回在Y1,Y2两年之间的闰年总数。
5 calendar.month(year,month,w=2,l=1)
返回一个多行字符串格式的year年month月日历,两行标题,一周一行。每日宽度间隔为w字符。每行的长度为7* w+6。l是每星期的行数。
6 calendar.monthcalendar(year,month)
返回一个整数的单层嵌套列表。每个子列表装载代表一个星期的整数。Year年month月外的日期都设为0;范围内的日子都由该月第几日表示,从1开始。
7 calendar.monthrange(year,month)
返回两个整数。第一个是该月的星期几的日期码,第二个是该月的日期码。日从0(星期一)到6(星期日);月从1到12。
8 calendar.prcal(year,w=2,l=1,c=6)
相当于 print calendar.calendar(year,w=2,l=1,c=6)
9 calendar.prmonth(year,month,w=2,l=1)
相当于 print calendar.month(year,month,w=2,l=1) 。
10 calendar.setfirstweekday(weekday)
设置每周的起始日期码。0(星期一)到6(星期日)。
11 calendar.timegm(tupletime)
和time.gmtime相反:接受一个时间元组形式,返回该时刻的时间戳(1970纪元后经过的浮点秒数)。
12 calendar.weekday(year,month,day)
返回给定日期的日期码。0(星期一)到6(星期日)。月份为 1(一月) 到 12(12月)。
  •  
发布了19 篇原创文章 · 获赞 0 · 访问量 812

Guess you like

Origin blog.csdn.net/weixin_44151772/article/details/104032893