04时间处理模块.py

'''
我们写程序时对时间的处理可以归为以下3种:

时间的显示,在屏幕显示、记录日志等

时间的转换,比如把字符串格式的日期转成Python中的日期类型

时间的运算,计算两个日期间的差值等
'''
# 时间戳 表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。例子:1554864776.161901
# import time
# print(time.time())
# s1 = time.time()
# s2 = time.time()
# print(s1)
# print(s2)
# print(s2 - s1)

# import time
# print(time.time()) #当前时间的时间戳
# print(time.strftime('%Y-%m-%d',time.localtime())) #将当前时间转换为2019-10-13格式
#如何将2019-10-13格式转化为时间戳
# s = time.strptime('2019-10-13','%Y-%m-%d') #将2019-10-13格式转换为struct_time格式
# print(time.mktime(s)) #将struct_time格式转化为时间戳 <class 'float'>



'''
元组(struct_time)共九个元素。由于Python的time模块实现主要调用C库,所以各个平台可能有所不同,mac上:time.struct_time(tm_year=2020, tm_mon=4, tm_mday=10, tm_hour=2, tm_min=53, tm_sec=15, tm_wday=2, tm_yday=100, tm_isdst=0)
索引(Index) 属性(Attribute) 值(Values)
0 tm_year(年) 比如2011
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
6 tm_wday(weekday) 0 - 6(0表示周一)
7 tm_yday(一年中的第几天) 1 - 366
8 tm_isdst(是否是夏令时) 默认为-1

UTC时间
UTC(Coordinated Universal Time,世界协调时)亦即格林威治天文时间,世界标准时间。在中国为UTC+8,又称东8区。DST(Daylight Saving Time)即夏令时
'''

# time模块的方法
# time.localtime([secs]):将一个时间戳转换为当前时区的struct_time。若secs参数未提供,则以当前时间为准。
# from time import localtime
# print(localtime()) #没有参数默认是当前时间
# print(localtime(133))

# time.gmtime([secs]):和localtime()方法类似,gmtime()方法是将一个时间戳转换为UTC时区(0时区)的struct_time。
#
# time.time():返回当前时间的时间戳。

# time.mktime(t):将一个struct_time转化为时间戳。
# import time
# s1 = time.localtime()
# print(time.mktime(s1))

# time.sleep(secs):线程推迟指定的时间运行,单位为秒。
# time.sleep(3)
# print('-------')
#

# time.asctime([t]):把一个表示时间的元组或者struct_time表示为这种形式:’Sun Oct 1 12:04:38 2019’。如果没有参数,将会将time.localtime()作为参数传入。
# print(time.asctime(time.localtime(13333))) #天数不会显示出来


# time.ctime([secs]):把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。如果参数未给或者为None的时候,将会默认time.time()为参数。它的作用相当于time.asctime(time.localtime(secs))。
# print(time.ctime(133333))


# time.strftime(format[, t]):把一个代表时间的元组或者struct_time(如由time.localtime()和time.gmtime()返回)转化为格式化的时间字符串。如果t未指定,将传入time.localtime()。
# 举例:time.strftime(“%Y-%m-%d %X”, time.localtime()) #输出’2017-10-01 12:14:23’
# import time
# print(time.strftime('%Y-%m-%d',time.localtime()))
# print(time.strftime('%Y.%m.%d %H:%M',time.localtime()))

# import datetime
# print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))#strftime("%Y-%m-%d %H:%M:%S")格式设计



# time.strptime(string[, format]):把一个格式化时间字符串转化为struct_time。实际上它和strftime()是逆操作。
# 举例:time.strptime(‘2017-10-3 17:54’,”%Y-%m-%d %H:%M”) #输出 time.struct_time(tm_year=2017, tm_mon=10, tm_mday=3, tm_hour=17, tm_min=54, tm_sec=0, tm_wday=1, tm_yday=276, tm_isdst=-1)time模块的方法
# print(time.strptime('2019.10.5','%Y.%m.%d'))
'''
Meaning Notes
%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.
'''



# datetime模块定义了下面这几个类:
#
# datetime.date:表示日期的类。常用的属性有year, month, day;
# import datetime
# print(datetime.date.today())
# print(datetime.date)

#datetime.date.timetuple 将日期格式转化为元组形式
# print(datetime.date.timetuple(datetime.date.today()))

# datetime.time:表示时间的类。常用的属性有hour, minute, second, microsecond;


# datetime.datetime:表示日期时间。
# print(datetime.datetime.now()) #2019-10-06 09:57:51.327767
# print(datetime.datetime.fromtimestamp(1232321)) #1970-01-15 14:18:41

# datetime.timedelta:表示时间间隔,即两个时间点之间的长度。
# help(datetime.timedelta) 了解函数相关用处
# t1 = datetime.datetime.now()
# print(t1 - datetime.timedelta(days=5,hours=6)) #2019-10-01 04:11:15.796780
# print(t1 + datetime.timedelta(days=5,hours=6)) #2019-10-11 16:14:08.875679


#replace 替换
# t1 = datetime.datetime.now()
# print(t1.replace(year=2016,month=7)) #t1还是没有改变还是原来的值




# datetime.tzinfo:与时区有关的相关信息。(这里不详细充分讨论该类,感兴趣的童鞋可以参考python手册)


#时区模块
# import pytz
# print(pytz.all_timezones) #打印所有时区地址
# print(pytz.timezone('Africa/Accra'))
# print(datetime.datetime.now(pytz.timezone('Africa/Accra'))) #2019-10-06 03:06:34.414594+00:00
# print(datetime.datetime.now(pytz.timezone('America/Toronto'))) #2019-10-05 23:06:34.416594-04:00



posted @ 2019-11-07 11:31  *!Walter!*  阅读(59)  评论(0)    收藏  举报