Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d829c91

Browse files
authored
Create unixtimeformat.py
Unix时间戳转换
1 parent 98c4dab commit d829c91

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

unixtimeformat.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
import time
5+
6+
def timestamp_datetime(value):
7+
format = '%Y-%m-%d %H:%M:%S'
8+
# value为传入的值为时间戳(整形),如:1332888820
9+
value = time.localtime(value)
10+
## 经过localtime转换后变成
11+
## time.struct_time(tm_year=2012, tm_mon=3, tm_mday=28, tm_hour=6, tm_min=53, tm_sec=40, tm_wday=2, tm_yday=88, tm_isdst=0)
12+
# 最后再经过strftime函数转换为正常日期格式。
13+
dt = time.strftime(format, value)
14+
return dt
15+
16+
def datetime_timestamp(dt):
17+
#dt为字符串
18+
#中间过程,一般都需要将字符串转化为时间数组
19+
time.strptime(dt, '%Y-%m-%d %H:%M:%S')
20+
## time.struct_time(tm_year=2012, tm_mon=3, tm_mday=28, tm_hour=6, tm_min=53, tm_sec=40, tm_wday=2, tm_yday=88, tm_isdst=-1)
21+
#将"2012-03-28 06:53:40"转化为时间戳
22+
s = time.mktime(time.strptime(dt, '%Y-%m-%d %H:%M:%S'))
23+
return int(s)
24+
25+
if __name__ == '__main__':
26+
d = datetime_timestamp('2015-07-28 21:53:40')
27+
print d
28+
s = timestamp_datetime(1352889820)
29+
print s

0 commit comments

Comments
 (0)