Skip to content

Commit 5d83043

Browse files
committed
add datetime
1 parent 2c96bee commit 5d83043

1 file changed

Lines changed: 153 additions & 0 deletions

File tree

Standard-Modules/datetime.md

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
# datetime
2+
3+
Python 提供了两个标准库用于处理跟时间相关的问题,一个是 `time`,另一个是 `datetime``datetime``time` 进行了封装,提供了更多实用的函数。本文介绍 `datetime` 库的简单使用。
4+
5+
# 当前时间
6+
7+
获取当前时间可以使用 `now()``utcnow()` 方法,其中,`now()` 用于获取当地时间,而 `utcnow()` 用于获取 UTC 时间。
8+
9+
```python
10+
>>> from datetime import datetime
11+
12+
>>> datetime.now() # 返回一个 datetime 对象,这里是当地时间
13+
datetime.datetime(2016, 12, 10, 11, 32, 43, 806970)
14+
15+
>>> datetime.utcnow() # 返回一个 datetime 对象,这里是 UTC 时间
16+
datetime.datetime(2016, 12, 10, 3, 32, 49, 999423)
17+
18+
>>> datetime.now().year, datetime.now().month, datetime.now().day # 年月日
19+
(2016, 12, 10)
20+
21+
>>> datetime.now().hour, datetime.now().minute, datetime.now().second # 时分秒
22+
(11, 35, 37)
23+
```
24+
25+
# 时间格式化
26+
27+
有时,我们需要对时间做格式化处理,可以使用 `strftime()``strptime()` 方法,其中,`strftime` 用于对 `datetime` 对象进行格式化,`strptime` 用于对字符串对象进行格式化。
28+
29+
```python
30+
>>> from datetime import datetime
31+
32+
# 获取当前当地时间
33+
>>> now = datetime.now()
34+
>>> now
35+
datetime.datetime(2016, 12, 10, 11, 46, 24, 432168)
36+
37+
# 对 datetime 对象进行格式化,转为字符串格式
38+
>>> now_str = now.strftime('%Y-%m-%d %H:%M:%S.%f')
39+
>>> now_str
40+
'2016-12-10 11:46:24.432168'
41+
42+
# 对字符串对象进行格式化,转为 datetime 对象
43+
>>> datetime.strptime(now_str, '%Y-%m-%d %H:%M:%S.%f')
44+
datetime.datetime(2016, 12, 10, 11, 46, 24, 432168)
45+
```
46+
47+
# 时间戳
48+
49+
[Unix 时间戳](http://funhacks.net/2015/04/29/Unix-timestamp/)根据精度的不同,有 10 位(秒级),13 位(毫秒级),16 位(微妙级)和 19 位(纳秒级)。
50+
51+
要注意的是,由于每个时区都有自己的本地时间(北京在东八区),因此也产生了世界标准时间(UTC, Universal Time Coordinated)。所以,在将一个时间戳转换为普通时间(比如 2016-01-01 12:00:00)时,要注意是要本地时区的时间还是世界时间等。
52+
53+
- 获取当前当地时间戳
54+
55+
```python
56+
>>> import time
57+
>>> from datetime import datetime
58+
59+
# 获取当前当地时间,返回一个 datetime 对象
60+
>>> now = datetime.now()
61+
>>> now
62+
datetime.datetime(2016, 12, 9, 11, 56, 47, 632778)
63+
64+
# 13 位的毫秒时间戳
65+
>>> long(time.mktime(now.timetuple()) * 1000.0 + now.microsecond / 1000.0)
66+
1481255807632L
67+
68+
# 10 位的时间戳
69+
>>> int(time.mktime(now.timetuple()))
70+
1481255807
71+
```
72+
73+
- 获取当前 UTC 时间戳
74+
75+
```python
76+
>>> import calendar
77+
>>> from datetime import datetime
78+
79+
# 获取当前的 UTC 时间,返回 datetime 对象
80+
>>> utc_now = datetime.utcnow()
81+
>>> utc_now
82+
datetime.datetime(2016, 12, 9, 4, 0, 53, 356641)
83+
84+
# 13 位的时间戳
85+
>>> long(calendar.timegm(utc_now.timetuple()) * 1000.0 + utc_now.microsecond / 1000.0)
86+
1481256053356L
87+
88+
# 10 位的时间戳
89+
>>> calendar.timegm(utc_now.timetuple())
90+
1481256053
91+
```
92+
93+
- 将时间戳转为字符串形式
94+
95+
```python
96+
>>> from datetime import datetime
97+
98+
# 13 位的毫秒时间戳
99+
>>> timestamp = 1456402864242
100+
101+
# 根据时间戳构建当地时间
102+
>>> datetime.fromtimestamp(timestamp / 1000.0).strftime('%Y-%m-%d %H:%M:%S.%f')
103+
'2016-02-25 20:21:04.242000'
104+
105+
# 根据时间戳构建 UTC 时间
106+
>>> datetime.utcfromtimestamp(timestamp / 1000.0).strftime('%Y-%m-%d %H:%M:%S.%f')
107+
'2016-02-25 12:21:04.242000'
108+
109+
# 10 位的时间戳
110+
>>> timestamp = 1456402864
111+
112+
# 根据时间戳构建当地时间
113+
>>> datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')
114+
'2016-02-25 20:21:04'
115+
116+
# 根据时间戳构建 UTC 时间
117+
>>> datetime.utcfromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')
118+
'2016-02-25 12:21:04'
119+
```
120+
121+
- 将时间戳转为 datetime 形式
122+
123+
```python
124+
>>> from datetime import datetime
125+
126+
# 13 位的毫秒时间戳
127+
>>> timestamp = 1456402864242
128+
129+
# 根据时间戳构建当地时间
130+
>>> datetime.fromtimestamp(timestamp / 1000.0)
131+
datetime.datetime(2016, 2, 25, 20, 21, 4, 242000)
132+
133+
# 根据时间戳构建 UTC 时间
134+
>>> datetime.utcfromtimestamp(timestamp / 1000.0)
135+
datetime.datetime(2016, 2, 25, 12, 21, 4, 242000)
136+
137+
# 10 位的时间戳
138+
>>> timestamp = 1456402864
139+
140+
# 根据时间戳构建当地时间
141+
>>> datetime.fromtimestamp(timestamp)
142+
datetime.datetime(2016, 2, 25, 20, 21, 4)
143+
144+
# 根据时间戳构建 UTC 时间
145+
>>> datetime.utcfromtimestamp(timestamp)
146+
datetime.datetime(2016, 2, 25, 12, 21, 4)
147+
```
148+
149+
# 参考资料
150+
151+
- [python-datetime-time-conversions](http://www.saltycrane.com/blog/2008/11/python-datetime-time-conversions/)
152+
153+

0 commit comments

Comments
 (0)