@nalan90
2017-07-31T11:52:42.000000Z
字数 3477
阅读 626
Python高效编程技巧实战
将字符串的时间转换为时间戳
In [1]: d = '2017-07-31 11:00:34'
In [2]: import time
In [7]: time.strptime?
Docstring:
strptime(string, format) -> struct_time
Parse a string to a time tuple according to a format specification.
See the library reference manual for formatting codes (same as strftime()).
Type: builtin_function_or_method
In [3]: timeArray = time.strptime(d,'%Y-%m-%d %H:%M:%S')
In [4]: print timeArray
time.struct_time(tm_year=2017, tm_mon=7, tm_mday=31, tm_hour=11, tm_min=0, tm_sec=34, tm_wday=0, tm_yday=212, tm_isdst=-1)
In [44]: time.mktime?
Docstring:
mktime(tuple) -> floating point number
Convert a time tuple in local time to seconds since the Epoch.
Type: builtin_function_or_method
In [5]: timestamp = int(time.mktime(timeArray))
In [6]: print timestamp
1501470034
字符串格式更改
## 如d = "2013-10-10 23:40:00",想改为 d = "2013/10/10 23:40:00"
In [8]: d
Out[8]: '2017-07-31 11:00:34'
In [9]: timeArray = time.strptime(d,'%Y-%m-%d %H:%M:%S')
In [10]: print timeArray
time.struct_time(tm_year=2017, tm_mon=7, tm_mday=31, tm_hour=11, tm_min=0, tm_sec=34, tm_wday=0, tm_yday=212, tm_isdst=-1)
In [11]: print time.strftime('%Y/%m/%d %H:%M:%S',timeArray)
2017/07/31 11:00:34
In [12]: time.strftime?
Docstring:
strftime(format[, tuple]) -> string
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.
Type: builtin_function_or_method
时间戳转换为指定格式日期
## 方案一
In [13]: time.time()
Out[13]: 1501470399.986831
In [14]: timestamp = int(time.time())
In [15]: time.time?
Docstring:
time() -> floating point number
Return the current time in seconds since the Epoch.
Fractions of a second may be present if the system clock provides them.
Type: builtin_function_or_method
In [16]: print timestamp
1501470419
In [17]: time.localtime?
Docstring:
localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,
tm_sec,tm_wday,tm_yday,tm_isdst)
Convert seconds since the Epoch to a time tuple expressing local time.
When 'seconds' is not passed in, convert the current time instead.
Type: builtin_function_or_method
In [18]: timeArray = time.localtime(timestamp)
In [19]: print timeArray
time.struct_time(tm_year=2017, tm_mon=7, tm_mday=31, tm_hour=11, tm_min=6, tm_sec=59, tm_wday=0, tm_yday=212, tm_isdst=0)
In [20]: print time.strftime('%Y-%m-%d %H:%M:%S',timeArray)
2017-07-31 11:06:59
## 方案二
In [23]: print timestamp
1501470419
In [24]: import datetime
In [25]: dateArray = datetime.datetime.utcfromtimestamp(timestamp)
In [26]: print dateArray
2017-07-31 03:06:59
In [27]: print type(dateArray)
<type 'datetime.datetime'>
In [28]: datetime.datetime.strftime?
Docstring: format -> strftime() style string.
Type: method_descriptor
In [30]: print dateArray.strftime('%Y-%m-%d %H:%M:%S')
2017-07-31 03:06:59
获取当前时间并转换为指定日期格式
## 方案一
In [61]: import time
In [62]: now = int(time.time())
In [63]: timeArray = time.localtime(now)
In [64]: print time.strftime('%Y-%m-%d %H:%M:%S',timeArray)
2017-07-31 11:28:14
## 方案二
In [65]: import datetime
In [66]: now = datetime.datetime.now()
In [67]: print now.strftime('%Y-%m-%d %H:%M:%S')
2017-07-31 11:29:11
获得三天前的时间
In [38]: import time,datetime
In [39]: threeDayAgo = (datetime.datetime.now() - datetime.timedelta(days = 3))
In [46]: datetime.datetime.timetuple?
Docstring: Return time tuple, compatible with time.localtime().
Type: method_descriptor
In [40]: timestamp = int(time.mktime(threeDayAgo.timetuple()))
## 生成时间戳
In [41]: print timestamp
1501211728
## 生成字符串的日期
In [43]: print threeDayAgo.strftime('%Y-%m-%d %H:%M:%S')
2017-07-28 11:15:28
给定时间戳,计算该时间的几天前时间:
In [53]: timestamp = 1381419600
In [54]: import time,datetime
In [55]: dateArray = datetime.datetime.utcfromtimestamp(timestamp)
In [56]: threeDayAgo = dateArray - datetime.timedelta(days = 3)
In [57]: print threeDayAgo.strftime('%Y-%m-%d %H:%M:%S')
2013-10-07 15:40:00