Speaking of time in computers, there are some more interesting things, such as Unix timestamps, UTC time, Greenwich time, etc. that we often hear. In terms of representation, they basically belong to the same thing, because they The time represents the number of seconds from January 1, 1970 to the present. Someone has a question at this point. Why did Mao start from this point in time? Because a big event happened today. The UNIX operating system was born. Does the birth of UNIX have an origin? It was done by a crook who used his wife and children to go on vacation. I assume that everyone understands the complexity of the operating system. So we estimate that his wife will have to go out for several years to complete the preparation of the UNIX system? 3 years? 5 years? Wrong, my wife went away for a month and then came back. This is just like Linus writing a Linux system in the dormitory on a rainy day. Well, maybe great gods are meant to be admired, and there are always things that people can't hope for. It feels like a nagging feeling. In fact, this world is like this. There are always some awesome people popping up from time to time to promote the progress of the entire human civilization. If you work hard enough, maybe one day you will stand on that stage.
After introducing the background of computer time, now comes the practical information. I have summarized common examples of using python to obtain system time, so that friends can quickly get started and query, and it supports common date formats. Let’s first talk about how to use the time module to time the time we want:
>>> import time >>> time.time() 1469101837.655935
time.time() function is the returned UTC time, which is the number of seconds from 1970.1.1 to now.
>>> time.ctime(1469101837.655935) 'Thu Jul 21 19:50:37 2016'
The time.ctime() function receives an actual value in seconds and converts it to a string representation of local time.
If we want to format the output in time format, we can use the strftime() function, which can change our time format into the format we want:
>>> from time import strftime,gmtime >>> strftime("%m/%d/%Y %H:%M") '07/21/2016 19:57' >>> time.strftime("%Y%m%d") '20160721' >>> strftime("%Y-%m-%d %H:%M:%S", gmtime()) '2016-07-21 11:47:51'
In python, in addition to the time module, there is also a datetime module, which can also be used to conveniently manipulate time. For example, use the datetime module to display the current time:
>>> from datetime import datetime >>> datetime.now().strftime('%Y-%m-%d %H:%M:%S') '2016-07-21 19:49:15' >>> datetime.now().isoformat() '2016-07-21T19:56:46.744893' >>> str(datetime.now()) '2016-07-21 19:48:37.436886'
In the script, these two Modules are commonly used, such as timestamp variables for file backup, time variables for old file deletion operations, etc. You can make your own modifications through the above examples to get the format you want. If you only need a certain time Parts can be split using the split() function, and the desired content can be obtained through slicing. This article ends here. If you have any questions, please leave a message.
For more articles related to python’s simple implementation of obtaining the current time, please pay attention to the PHP Chinese website!