Reprint Address: http://liyangliang.me/posts/2012/10/python-timestamp-to-timestr/
When writing python, often encounter time format problems, each time is on Google search and then find someone else's blog or website for reference. Now to summarize their own simple, convenient later query.
The first is the conversion between the recently used timestamp (timestamp) and the time string. The so-called timestamp is the number of seconds from 00:00:00 January 1, 1970 to the present. The article has a simple and concise introduction to why this is a special date for 1970:
Why does computer time start from January 1, 1970?
In Python, timestamps can be obtained through time
the methods in the module time()
, such as:
In [1]: import timeIn [2]: time.time()Out[2]: 1350816710.8050799
This value is unfriendly to people, so it is sometimes necessary to convert it to a certain format to make it easier for people to understand. We can invoke time.strftime()
The function to achieve this purpose. According to strftime()
the documentation for the function, I guess the name should be the shorthand for "string format Time", which is the length of the string. This method requires two parameters, one of which is the time format, one is a 9-tuple, the second parameter is optional, and the default is time.localtime()
the return value. And that 9-tuple is actually a tuple struct_time
(tuple) of 9 elements, which is also a form of time representation. Like
In [5]: import timeIn [6]: time.localtime()Out[6]: time.struct_time(tm_year=2012, tm_mon=10, tm_mday=21, tm_hour=19, tm_min=4, tm_sec=25, tm_wday=6, tm_yday=295, tm_isdst=0)
The exact meaning of the first 6 should be obvious, then the last three are: Weekday (0-6), the Day of the Year (1-366), whether it is daylight saving time (Default-1). Now take a look at how to convert the timestamp to a string in the specified format. Very simple, directly on the code
in [8]: import Timein [9]: st = time.localtime (1350816710.8050799) in [10]: Time.strftime ( '%y-%m-%d%h:%m:%s ', ST) out[ 10]: ' 2012-10-21 18:51:50 '
The timestamp is localtime()
converted to struct_time
, and then passed to strftime
the string converted to the specified format. So what's the reverse? The same needs to be converted to struct_time
, this work is done by the time.strptime()
function. strptime
p
should be in the meaning of parse, the prototype is:
strptime(string, format) -> struct_time
The time in the form of a string is parsed in the specified format and converted to struct_time
. Then pass the time.mktime()
final work to completion, the whole process is:
In [12]: import time In [13]: st = time.strptime(‘2012-10-21 18:51:50‘, ‘%Y-%m-%d %H:%M:%S‘) In [14]: time.mktime(st)Out[14]: 1350816710.0
Finally, there are two reference articles:
- Time stamp and time string conversion in Python: http://www.coder4.com/archives/2239
- The time module in Python is detailed (very good): http://qinxuye.me/article/details-about-time-module-in-python/
Python timestamps and dates convert each other