python3 time模块相关


哒哒哒社区中博文地址
学习time 模块和日历模块相关内容


引入 time 和calendar模块
```python
import time
import calendar
print(time.__doc__)






    This module provides various functions to manipulate time values.
    
    There are two standard representations of time.  One is the number
    of seconds since the Epoch, in UTC (a.k.a. GMT).  It may be an integer
    or a floating point number (to represent fractions of seconds).
    The Epoch is system-defined; on Unix, it is generally January 1st, 1970.
    The actual value can be retrieved by calling gmtime(0).
    
    The other representation is a tuple of 9 integers giving local time.
    The tuple items are:
      year (including century, e.g. 1998)
      month (1-12)
      day (1-31)
      hours (0-23)
      minutes (0-59)
      seconds (0-59)
      weekday (0-6, Monday is 0)
      Julian day (day in the year, 1-366)
      DST (Daylight Savings Time) flag (-1, 0 or 1)
    If the DST flag is 0, the time is given in the regular time zone;
    if it is 1, the time is given in the DST time zone;
    if it is -1, mktime() should guess based on the date and time.
    
  ``` 


常用的内置函数:
```python


# 时间戳
print(time.time())


# 本地时间
print(time.localtime())


# 格式化的本地时间
# 接受时间元组并返回一个可读的形式为"Tue Dec 11 18:07:14 2008"(2008年12月11日 周二18时07分14秒)的24个字符的字符串。
print(time.asctime(time.localtime(time.time())))


# 推迟线程进行
time.sleep(2)


# 格式化日期
# 接收以时间元组,并返回以可读字符串表示的当地时间,格式由fmt决定。
print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()))


#将格式字符串转换为时间戳
# 根据fmt的格式把一个时间字符串解析为时间元组。
print(time.mktime(time.strptime("2018-06-26 08:00:00","%Y-%m-%d %H:%M:%S")))


# 打印某年某月的日历
print(calendar.month(2018,9))


# 返回一个多行字符串格式的year年年历,3个月一行,间隔距离为c。 每日宽度间隔为w字符。每行长度为21* W+18+2* C。l是每星期行数。
print(calendar.calendar(2018,w=2,l=1,c=6))


# 返回当前每周起始日期的设置。默认情况下,首次载入caendar模块时返回0,即星期一。
print(calendar.firstweekday())


# 是闰年返回True,否则为false。
print(calendar.isleap(2018))


# 2000-2019年之间的闰年
year = 2000
while year < 2019:
    if calendar.isleap(year):
        print(year)
    year = year + 1


# 返回在Y1,Y2两年之间的闰年总数。
print(calendar.leapdays(2000,2018))


# 返回一个多行字符串格式的year年month月日历,两行标题,一周一行。每日宽度间隔为w字符。每行的长度为7* w+6。l是每星期的行数。
print(calendar.month(2018,6,w=2,l=1))
```


    1529983755.2326024
    time.struct_time(tm_year=2018, tm_mon=6, tm_mday=26, tm_hour=11, tm_min=29, tm_sec=15, tm_wday=1, tm_yday=177, tm_isdst=0)
    Tue Jun 26 11:29:15 2018
    2018-06-26 11:29:17
    1529971200.0
       September 2018
    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 30
    
                                      2018
    
          January                   February                   March
    Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
     1  2  3  4  5  6  7                1  2  3  4                1  2  3  4
     8  9 10 11 12 13 14       5  6  7  8  9 10 11       5  6  7  8  9 10 11
    15 16 17 18 19 20 21      12 13 14 15 16 17 18      12 13 14 15 16 17 18
    22 23 24 25 26 27 28      19 20 21 22 23 24 25      19 20 21 22 23 24 25
    29 30 31                  26 27 28                  26 27 28 29 30 31
    
           April                      May                       June
    Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
                       1          1  2  3  4  5  6                   1  2  3
     2  3  4  5  6  7  8       7  8  9 10 11 12 13       4  5  6  7  8  9 10
     9 10 11 12 13 14 15      14 15 16 17 18 19 20      11 12 13 14 15 16 17
    16 17 18 19 20 21 22      21 22 23 24 25 26 27      18 19 20 21 22 23 24
    23 24 25 26 27 28 29      28 29 30 31               25 26 27 28 29 30
    30
    
            July                     August                  September
    Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
                       1             1  2  3  4  5                      1  2
     2  3  4  5  6  7  8       6  7  8  9 10 11 12       3  4  5  6  7  8  9
     9 10 11 12 13 14 15      13 14 15 16 17 18 19      10 11 12 13 14 15 16
    16 17 18 19 20 21 22      20 21 22 23 24 25 26      17 18 19 20 21 22 23
    23 24 25 26 27 28 29      27 28 29 30 31            24 25 26 27 28 29 30
    30 31
    
          October                   November                  December
    Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
     1  2  3  4  5  6  7                1  2  3  4                      1  2
     8  9 10 11 12 13 14       5  6  7  8  9 10 11       3  4  5  6  7  8  9
    15 16 17 18 19 20 21      12 13 14 15 16 17 18      10 11 12 13 14 15 16
    22 23 24 25 26 27 28      19 20 21 22 23 24 25      17 18 19 20 21 22 23
    29 30 31                  26 27 28 29 30            24 25 26 27 28 29 30
                                                        31
    
    0
    False
    2000
    2004
    2008
    2012
    2016
    5
         June 2018
    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 30
    
    




```python
# python中时间日期格式化符号:
"""
%y 两位数的年份表示(00-99)
%Y 四位数的年份表示(000-9999)
%m 月份(01-12)
%d 月内中的一天(0-31)
%H 24小时制小时数(0-23)
%I 12小时制小时数(01-12)
%M 分钟数(00=59)
%S 秒(00-59)
%a 本地简化星期名称
%A 本地完整星期名称
%b 本地简化的月份名称
%B 本地完整的月份名称
%c 本地相应的日期表示和时间表示
%j 年内的一天(001-366)
%p 本地A.M.或P.M.的等价符
%U 一年中的星期数(00-53)星期天为星期的开始
%w 星期(0-6),星期天为星期的开始
%W 一年中的星期数(00-53)星期一为星期的开始
%x 本地相应的日期表示
%X 本地相应的时间表示
%Z 当前时区的名称
%% %号本身
"""
```


猜你喜欢

转载自blog.csdn.net/xlelou/article/details/80813070
今日推荐