当前位置: 移动技术网 > IT编程>脚本编程>Python > #15 time&datetime&calendar模块

#15 time&datetime&calendar模块

2019年02月06日  | 移动技术网IT编程  | 我要评论

墨西哥毒贩电锯割头,vaagaa,163贵州网人事招考信息网

前言

从这一节开始,记录一些常用的内置模块,模块的学习可能比较无聊,但基础就在这无聊的模块中,话不多说,本节记录和时间相关的模块!

一、time模块

python中设计时间的模块有很多,但是最常用的就是time模块了,先来看看time模块的所有方法:

in [3]: dir(time)
out[3]:
['clock_monotonic', 'clock_monotonic_raw', 'clock_process_cputime_id', 'clock_realtime', 'clock_thread_cputime_id', '_struct_tm_items', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'altzone', 'asctime', 'clock', 'clock_getres', 'clock_gettime', 'clock_settime', 'ctime', 'daylight', 'get_clock_info', 'gmtime', 'localtime', 'mktime', 'monotonic', 'perf_counter', 'process_time', 'sleep', 'strftime', 'strptime', 'struct_time', 'time', 'timezone', 'tzname','tzset']

方法还不少呢,一点一点来剖析它

1.获取时间

in [19]: time.time()                      #获取当前时间的时间戳
out[19]: 1549343780.962011

in [20]: time.localtime()                 # 获取当地时间,返回当地时间下的时间元组,可是设置是否为夏令时,tm_isdst为夏令时
out[20]: time.struct_time(tm_year=2019, tm_mon=2, tm_mday=5, tm_hour=13, tm_min=16, tm_sec=27, tm_wday=1, tm_yday=36, tm_isdst=0)

in [21]: time.localtime(time.time())      # 参数接受为时间戳
out[21]: time.struct_time(tm_year=2019, tm_mon=2, tm_mday=5, tm_hour=13, tm_min=16, tm_sec=39, tm_wday=1, tm_yday=36, tm_isdst=0)

in [22]: time.asctime()                   # 获取格式化时间
out[22]: 'tue feb  5 13:17:23 2019'

in [23]: time.asctime(time.localtime())   # 参数为struct_time格式
out[23]: 'tue feb  5 13:17:40 2019'

in [24]: time.ctime()                     # 获取格式化时间,同上
out[24]: 'tue feb  5 13:18:11 2019'

in [25]: time.gmtime()                    # 同localtime,但返回格林威治天文时间下的时间元组
out[25]: time.struct_time(tm_year=2019, tm_mon=2, tm_mday=5, tm_hour=5, tm_min=18, tm_sec=18, tm_wday=1, tm_yday=36, tm_isdst=0)

2.格式化日期

in [28]: time.strftime("%y-%m-%d %h:%m:%s", time.localtime())    # 格式化成 年-月-日 时:分:秒 形式
out[28]: '2019-02-05 13:26:27'

in [29]: time.strftime("%a %b %d %h:%m:%s %y", time.localtime()) # 格式化成周 月 日 时:分:秒 年 形式
out[29]: 'tue feb 05 13:26:47 2019'
in [33]: time.gmtime(time.time())        # 将时间戳转换为struct_time格式
out[33]: time.struct_time(tm_year=2019, tm_mon=2, tm_mday=5, tm_hour=5, tm_min=40, tm_sec=15, tm_wday=1, tm_yday=36, tm_isdst=0)

in [34]: time.mktime(time.localtime())   # 将struct_time格式转换为时间戳 
out[34]: 1549345233.0
in [35]: time.strptime("2019/02/05","%y/%m/%d")   # 将格式化日期转换为struct_time格式
out[35]: time.struct_time(tm_year=2019, tm_mon=2, tm_mday=5, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=36, tm_isdst=-1)

补充:time模块日期格式化符号

%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      # 当前时区的名称
%%      # %号本身

3.睡眠

in [36]: time.sleep(3)   # 使用time.sleep(秒数)来达到睡眠

4.其他方法

in [37]: time.altzone      # 返回格林威治西部的夏令时地区的偏移秒数。如果该地区在格林威治东部会返回负值(如西欧,包括英国)。对夏令时启用地区才能使用
out[37]: -28800

in [38]: time.clock()      # 以浮点数计算的秒数返回当前的cpu时间,用来衡量不同程序的耗时
out[38]: 10.241939
 
in [39]: time.timezone     # 当地时区(未启动夏令时)距离格林威治的偏移秒数(>0,美洲;<=0大部分欧洲,亚洲,非洲)
out[39]: -28800

in [40]: time.tzname       # 包含一对根据情况的不同而不同的字符串,分别是带夏令时的本地时区名称,和不带的
out[40]: ('cst', 'cst')

二、datetime模块

datetime模块相比于time模块并不常用,但还是要了解它

in [42]: datetime.datetime.now()   # 获取当前时间,直接返回格式化后的时间格式,比较人性化
out[42]: datetime.datetime(2019, 2, 5, 13, 56, 28, 929415)

in [43]: datetime.datetime.now() + datetime.timedelta(5)         # 当前时间加5天,默认单位是天
out[43]: datetime.datetime(2019, 2, 10, 14, 1, 14, 324972)

in [44]: datetime.datetime.now() + datetime.timedelta(-5)        # 当前时间减5天
out[44]: datetime.datetime(2019, 1, 31, 14, 1, 21, 15534)

in [45]: datetime.datetime.now() + datetime.timedelta(hours=5)   # 当前时间加5小时
out[45]: datetime.datetime(2019, 2, 5, 19, 1, 33, 334723)

in [46]: datetime.datetime.now() + datetime.timedelta(minutes=5) # 当前时间加5分钟
out[46]: datetime.datetime(2019, 2, 5, 14, 6, 46, 123465)

可以看到datetime模块常用来作日期的加减计算,这方面很有优势

三、calendar模块

in [9]: import calendar

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

      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                   1  2  3                   1  2  3
 7  8  9 10 11 12 13       4  5  6  7  8  9 10       4  5  6  7  8  9 10
14 15 16 17 18 19 20      11 12 13 14 15 16 17      11 12 13 14 15 16 17
21 22 23 24 25 26 27      18 19 20 21 22 23 24      18 19 20 21 22 23 24
28 29 30 31               25 26 27 28               25 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  2  3  4  5  6  7             1  2  3  4  5                      1  2
 8  9 10 11 12 13 14       6  7  8  9 10 11 12       3  4  5  6  7  8  9
15 16 17 18 19 20 21      13 14 15 16 17 18 19      10 11 12 13 14 15 16
22 23 24 25 26 27 28      20 21 22 23 24 25 26      17 18 19 20 21 22 23
29 30                     27 28 29 30 31            24 25 26 27 28 29 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  2  3  4  5  6  7                1  2  3  4                         1
 8  9 10 11 12 13 14       5  6  7  8  9 10 11       2  3  4  5  6  7  8
15 16 17 18 19 20 21      12 13 14 15 16 17 18       9 10 11 12 13 14 15
22 23 24 25 26 27 28      19 20 21 22 23 24 25      16 17 18 19 20 21 22
29 30 31                  26 27 28 29 30 31         23 24 25 26 27 28 29
                                                    30

      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                   1  2  3                         1
 7  8  9 10 11 12 13       4  5  6  7  8  9 10       2  3  4  5  6  7  8
14 15 16 17 18 19 20      11 12 13 14 15 16 17       9 10 11 12 13 14 15
21 22 23 24 25 26 27      18 19 20 21 22 23 24      16 17 18 19 20 21 22
28 29 30 31               25 26 27 28 29 30         23 24 25 26 27 28 29
                                                    30 31
in [12]: calendar.firstweekday()   # 返回每周的第一天,0代表周一
out[12]: 0

in [13]: calendar.isleap(2004)   # 测试是否为闰月年
out[13]: true

in [14]: calendar.isleap(2019)
out[14]: false

in [15]: print(calendar.month(2019,1))   # calendar.month(year,month,w=2,l=1),返回一个多行字符串格式的year年month月日历,两行标题,一周一行,每日宽度间隔为w=2字符每行的长度为7* w+6,l=1是每星期的行数。
    january 2019
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 31

calendar掌握这么多就差不多了,如果需要查看其他方法,查看其帮助信息即可。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网