python 时间模块

time 模块 

time 模块下三种时间格式:

stamptime 时间戳 ,常用的time.time()  返回的就是一个时间戳  1659513403.53

format time  eg    2984-01-15 16:31:20

time tuple  eg   time.struct_time(tm_year=2022, tm_mon=8, tm_mday=3, tm_hour=7, tm_min=56, tm_sec=43, tm_wday=2, tm_yday=215, tm_isdst=0)   是time.struct_time  下的对象。

sting time   eg ‘Thu Jan  1 10:40:54 1970’

 

import time
from datetime import datetime


print time.time()
# Return the current time in seconds since the Epoch  返回一个秒级时间戳

print(time.localtime())
#     Convert seconds since the Epoch to a time tuple expressing local time.
#     When 'seconds' is not passed in, convert the current time instead.  转置给定的秒级时间戳,time tuple 如果 没有参数,默认当前时间戳

print(time.gmtime())
#    Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.
#     GMT).  When 'seconds' is not passed in, convert the current time instead. 同 localtime 返回的是 utc 时间

print time.mktime(time.localtime())
# Convert a time tuple in local time to seconds since the Epoch.  转置元组 返回时间戳。和localtime 相反

print(time.asctime())
#    Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.
#    When the time tuple is not present, current time as returned by localtime() is used.  接收的是time tuple 返回一个string  

print(time.ctime(9654))
# Convert a time in seconds since the Epoch to a string in local time.       等同于 asctime(localtime(seconds)) 接收的是时间戳
# This is equivalent to asctime(localtime(seconds)).

print time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(31999998680))         
#    Convert a time tuple to a string according to a format specification.    接收元组,返回格式化后的时间
#     See the library reference manual for formatting codes. When the time tuple is not present, current time as returned by localtime() is used.
print time.strptime('2984-01-15 16:31:20', '%Y-%m-%d %H:%M:%S')
#     Parse a string to a time tuple according to a format specification.
#     See the library reference manual for formatting codes (same as strftime()).  解析格式化后的时间,返回一个元组。

summarize :

获取时间戳    time.time()     mktime(timetuple)

获取 结构体时间   localtime(secconds),   gmtimt(seconds),     strptime(formattime,format) 

格式化时间  strftime(timetuple )  

字符串时间 asctime(touple )  ctime(seconds)

datetime 模块 

 

  1. datetime.date:表示日期的类,主要用于处理年、月、日;datetime.time:表示时间的类,主要用于处理时、分、秒;

  2. datetime.datetime:表示日期时间的类,date类和time类的综合使用,可以处理年、月、日、时、分、秒;
  3. datetime.timedelta:表示时间间隔,即两个时间点的间隔,主要用于做时间加减的
  4. datetime.tzinfo:时区的相关信息。

datetime 类

from datetime import datetime
@staticmethod # known case of __new__ # 至少 年月日 三个参数
def __new__(cls, year=None, month=None, day=None, hour=None, minute=None, second=None, microsecond=None, tzinfo=None): # known case of datetime.datetime.__new__
""" T.__new__(S, ...) -> a new object with type S, a subtype of T """
pass




 

date 类

from  datetime  import date
@staticmethod # known case of __new__
def __new__(cls, year=None, month=None, day=None): # known case of datetime.date.__new__
""" T.__new__(S, ...) -> a new object with type S, a subtype of T """
pass
@classmethod
def fromordinal(cls, ordinal): # known case of datetime.date.fromordinal
""" int -> 从顺序年开始,非1970,单位是天, """
return date(1,1,1)
@classmethod
def fromtimestamp(cls, timestamp): # known case of datetime.date.fromtimestamp
""" timestamp -> local date from a POSIX timestamp (like time.time()). """
return date(1,1,1)

 

时间日期格式化符号:

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

 

C strftime格式化说明


 
说明符替换为实例
%a 缩写的星期几名称 Sun
%A 完整的星期几名称 Sunday
%b 缩写的月份名称 Mar
%B 完整的月份名称 March
%c 日期和时间表示法 Sun Aug 19 02:56:02 2012
%d 一月中的第几天(01-31) 19
%H 24 小时格式的小时(00-23) 14
%I 12 小时格式的小时(01-12) 05
%j 一年中的第几天(001-366) 231
%m 十进制数表示的月份(01-12) 08
%M 分(00-59) 55
%p AM 或 PM 名称 PM
%S 秒(00-61) 02
%U 一年中的第几周,以第一个星期日作为第一周的第一天(00-53) 33
%w 十进制数表示的星期几,星期日表示为 0(0-6) 4
%W 一年中的第几周,以第一个星期一作为第一周的第一天(00-53) 34
%x 日期表示法 08/19/12
%X 时间表示法 02:50:06
%y 年份,最后两个数字(00-99) 01
%Y 年份 2012
%Z 时区的名称或缩写 CDT
%% 一个 % 符号 %
 

 

posted @ 2022-08-03 16:41  Orientation  阅读(53)  评论(0编辑  收藏  举报