• Day5模块-time和datetime模块


    模块是封装一段代码来实现某种功能。

    分为三类:

    1.自定义模块

    2.标准库,内置模块

    3.开源模块

    -----------------------------------------------------------------------------------------------------

    1.time模块

     1 >>> import time
     2 #返回处理器时间,3.3后变成了time.process_time()测量处理器运算时间,不包括sleep时间,不稳定
     3 >>> print(time.clock())
     4 2.9933526190766363e-06
     5 >>> print(time.process_time())
     6 0.09360059999999999
     7 
     8 #返回与utc时间的时间差,以秒计算
     9 >>> print(time.altzone)
    10 -32400
    11 #本地时间
    12 >>> print(time.localtime())
    13 time.struct_time(tm_year=2017, tm_mon=5, tm_mday=21, tm_hour=20, tm_min=54, tm_s
    14 ec=47, tm_wday=6, tm_yday=141, tm_isdst=0)
    15 #返回utc时间的struc时间对象格式
    16 >>> print(time.gmtime(time.time()-800000))
    17 time.struct_time(tm_year=2017, tm_mon=5, tm_mday=12, tm_hour=6, tm_min=41, tm_se
    18 c=56, tm_wday=4, tm_yday=132, tm_isdst=0)
    19 #返回时间格式
    20 >>> print(time.asctime())
    21 Sun May 21 20:56:26 2017
    22 >>> print(time.asctime(time.localtime()))
    23 Sun May 21 20:55:55 2017
    View Code

    日期字符串转换为时间戳

     1 #将字符串转换成struct对象
     2 >>> string_2_struct = time.strptime("2016/05/22","%Y/%m/%d")
     3 >>> print(string_2_struct)
     4 time.struct_time(tm_year=2016, tm_mon=5, tm_mday=22, tm_hour=0, tm_min=0, tm_sec
     5 =0, tm_wday=6, tm_yday=143, tm_isdst=-1)
     6 >>>
     7 #将struct对象转换为时间戳对象
     8 >>> string_2_stamp = time.mktime(string_2_struct)
     9 >>> print(string_2_stamp)
    10 1463846400.0
    View Code

    时间戳转换为字符串

     1 >>> print(time.time()) #当前utc时间,时间是秒
     2 1495372102.3229373
     3 >>> print(time.gmtime()) #struct对象的时间格式
     4 time.struct_time(tm_year=2017, tm_mon=5, tm_mday=21, tm_hour=13, tm_min=8, tm_se
     5 c=29, tm_wday=6, tm_yday=141, tm_isdst=0)
     6 
     7 #将utc时间戳转换成struct_time格式
     8 >>> print(time.gmtime(time.time()-86640)) 
     9 time.struct_time(tm_year=2017, tm_mon=5, tm_mday=20, tm_hour=13, tm_min=1, tm_se
    10 c=42, tm_wday=5, tm_yday=140, tm_isdst=0)
    11 #将utc struct_time格式转成指定的字符串格式
    12 >>> print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()))
    13 2017-05-21 13:07:45

    时间加减

     1 >>> import datetime
     2 >>> print("datetime".center(50,'*'))
     3 *********************datetime*********************
     4 >>> print(datetime.datetime.now()) #返回当前时间
     5 2017-05-21 21:12:10.507988
     6 # 时间戳直接转成日期格式 2016-08-19
     7 >>> print(datetime.date.fromtimestamp(time.time())) 
     8 2017-05-21
     9 
    10 #返回当前时间
    11 >>> print(datetime.datetime.now())
    12 2017-05-21 21:17:05.163842
    13 #时间戳直接转成日期格式 2016-08-19
    14 >>> print(datetime.date.fromtimestamp(time.time()))
    15 2017-05-21
    16 #当前时间加3天
    17 >>> print(datetime.datetime.now()+datetime.timedelta(3))
    18 2017-05-24 21:17:39.092782
    19 #当前时间减3天
    20 >>> print(datetime.datetime.now()+datetime.timedelta(-3))
    21 2017-05-18 21:17:46.039180
    22 #当前时间加3小时
    23 >>> print(datetime.datetime.now()+datetime.timedelta(hours=3))
    24 2017-05-22 00:18:01.337055
    25 #当前时间加30分钟
    26 >>> print(datetime.datetime.now()+datetime.timedelta(minutes=30))
    27 2017-05-21 21:49:16.833373
    View Code

    时间替换

    1 >>> c_time = datetime.datetime.now()
    2 >>> print(c_time.replace(minute=3,hour=2))
    3 2017-05-21 02:03:05.940477
    4 >>> print(datetime.datetime.now)
    5 <built-in method now of type object at 0x0000000067D262C0>
    6 >>> print(datetime.datetime.now())
    7 2017-05-21 21:23:49.137948
    View Code
    DirectiveMeaningNotes
    %a Locale’s abbreviated weekday name.  
    %A Locale’s full weekday name.  
    %b Locale’s abbreviated month name.  
    %B Locale’s full month name.  
    %c Locale’s appropriate date and time representation.  
    %d Day of the month as a decimal number [01,31].  
    %H Hour (24-hour clock) as a decimal number [00,23].  
    %I Hour (12-hour clock) as a decimal number [01,12].  
    %j Day of the year as a decimal number [001,366].  
    %m Month as a decimal number [01,12].  
    %M Minute as a decimal number [00,59].  
    %p Locale’s equivalent of either AM or PM. (1)
    %S Second as a decimal number [00,61]. (2)
    %U Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. (3)
    %w Weekday as a decimal number [0(Sunday),6].  
    %W Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. (3)
    %x Locale’s appropriate date representation.  
    %X Locale’s appropriate time representation.  
    %y Year without century as a decimal number [00,99].  
    %Y Year with century as a decimal number.  
    %z Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59].  
    %Z Time zone name (no characters if no time zone exists).  
    %% A literal '%' character.

    字符串、时间格式、utc时间的转换关系

    a.字符串转换为utc时间

    >>> time.strptime("2016/05/22","%Y/%m/%d")
    time.struct_time(tm_year=2016, tm_mon=5, tm_mday=22, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=143, tm_isdst=-1)

    >>> struct_time = time.strptime("2016/05/22","%Y/%m/%d")
    >>> time.mktime(struct_time)
    1463846400.0

    b.utc时间转换为字符串

    >>> time.gmtime(1463846400.0)
    time.struct_time(tm_year=2016, tm_mon=5, tm_mday=21, tm_hour=16, tm_min=0, tm_se
    c=0, tm_wday=5, tm_yday=142, tm_isdst=0)

    >>> time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime())
    '2017-05-21 13:30:47'

  • 相关阅读:
    ABP框架理论学习之Debugging
    探索博客园的“打赏”模式
    P3382 【模板】三分法
    1020.数字识别
    洛谷 P1162 填涂颜色
    在windows命令行批量ping局域网内IP
    1154:LETTERS
    百炼 2790:迷宫
    洛谷 P1605 迷宫
    2012年NOIP普及组 摆花
  • 原文地址:https://www.cnblogs.com/wolfs685/p/6886157.html
Copyright © 2020-2023  润新知