forked from zhanghe06/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate.py
More file actions
127 lines (103 loc) · 5.06 KB
/
date.py
File metadata and controls
127 lines (103 loc) · 5.06 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# coding=utf-8
__author__ = 'zhanghe'
import time
import datetime
def add_time(time_str, second):
"""
添加时间
:param time_str:
:param second:
:return:
"""
if time_str is None:
return '0000-00-00 00:00:00'
new_time_stamp = time.localtime(time.mktime(time.strptime(time_str, '%Y-%m-%d %H:%M:%S')) + second)
return time.strftime('%Y-%m-%d %H:%M:%S', new_time_stamp)
def test():
"""
测试代码
"""
# 时间戳(timestamp)
# 时间戳表示的是从 1970-01-01 00:00:00 开始按秒计算的偏移量。
# 返回时间戳方式的函数主要有time(),clock()等
print time.time() # 1441632561.1
print type(time.time()) # <type 'float'>
# 元组(struct_time)
# struct_time元组共有9个元素,返回struct_time的函数主要有gmtime(),localtime(),strptime()
print time.localtime() # time.struct_time(tm_year=2015, tm_mon=9, tm_mday=7, tm_hour=21, tm_min=29, tm_sec=21, tm_wday=0, tm_yday=250, tm_isdst=0)
print type(time.localtime()) # <type 'time.struct_time'>
# 将一个元祖(struct_time)转化为时间戳
print time.mktime(time.localtime()) # 1441633092.0
print type(time.mktime(time.localtime())) # <type 'float'>
print time.ctime() # Mon Sep 7 21:31:02 2015
print type(time.ctime()) # <type 'str'>
# 格式化字符串 time.strftime(format[, t])
# 把一个代表时间的元组或者struct_time(如由time.localtime()和time.gmtime()返回)转化为格式化的时间字符串。
# 如果t未指定,将传入time.localtime()。
# 如果元组中任何一个元素越界,ValueError的错误将会被抛出。
print time.strftime("%Y-%m-%d %H:%M:%S") # 2015-09-07 21:44:07
print type(time.strftime("%Y-%m-%d %H:%M:%S")) # <type 'str'>
print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 2015-09-07 21:44:07
print time.strftime("%Y-%m-%d", time.localtime()) + ' 09:00:00' # 2015-09-07 09:00:00
print ' '.join((time.strftime("%Y-%m-%d", time.localtime()), '09:00:00')) # 2015-09-07 09:00:00
# time.strptime(string[, format])
# 把一个格式化时间字符串转化为struct_time。实际上它和strftime()是逆操作。
# format默认为:"%a %b %d %H:%M:%S %Y"。
print time.strptime('2015-09-07 21:44:07', '%Y-%m-%d %X') # time.struct_time(tm_year=2015, tm_mon=9, tm_mday=7, tm_hour=21, tm_min=44, tm_sec=7, tm_wday=0, tm_yday=250, tm_isdst=-1)
print type(time.strptime('2015-09-07 21:44:07', '%Y-%m-%d %X')) # <type 'time.struct_time'>
# 格式化时间字符串转时间戳
# 实际分解成两步 第一步:字符串转为元祖;第二步:元祖(struct_time)转为时间戳
print time.mktime(time.strptime('2015-09-07 21:44:07', '%Y-%m-%d %X')) # 1441633447.0
print type(time.mktime(time.strptime('2015-09-07 21:44:07', '%Y-%m-%d %X'))) # <type 'float'>
# 日期时间元祖 datetime tuple(datetime obj)
# MySql中DATETIME类型 对应的就是 python里的这种类型
# 注意与时间元祖的区别
print datetime.datetime.now() # 2015-09-07 22:15:03.419781
print type(datetime.datetime.now()) # <type 'datetime.datetime'>
# 日期时间元祖 转为 时间元祖
# date.timetuple():返回日期对应的time.struct_time对象
print datetime.datetime.now().timetuple() # time.struct_time(tm_year=2015, tm_mon=9, tm_mday=7, tm_hour=22, tm_min=18, tm_sec=22, tm_wday=0, tm_yday=250, tm_isdst=-1)
print type(datetime.datetime.now().timetuple()) # <type 'time.struct_time'>
# 字符串日期转星期(星期(0-6),星期天为星期的开始)
print time.strftime('%w', time.strptime('2016-01-17', '%Y-%m-%d'))
if __name__ == "__main__":
test()
"""
1.python获取当前时间
time.time() 获取当前时间戳
time.localtime() 当前时间的struct_time形式
time.ctime() 当前时间的字符串形式
2.python格式化字符串
格式化成2009-03-20 11:45:39形式
time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
3.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 当前时区的名称
%% %号本身
4.py中涉及的time有四种类型
1. time string
2. datetime tuple(datetime obj)
3. time tuple(time obj)
4. timestamp
time模块的官方文档
https://docs.python.org/2/library/time.html
"""