Time Processing Module of Python

Source: Internet
Author: User

Time:

Location: Base

---------------------------------------------------------------------------------

I. Brief Introduction

The time processing module is used in several projects of the Institute of materials, especially when drawing the time-stress curve. Summary of the usage of the time module today, so that you can use it more skillfully later.

There are two time Representation Methods in the time module. One is the timestamp method, which is based on the offset of January 1, 1970 00:00:00, and the other is the struct_time () type representation method.

---------------------------------------------------------------------------------

Ii. Details

First of all, you need to import the time module:

import time
Next we will introduce several common functions in this module.

1. time. time () returns the number of seconds since January 1, January 1, 1970, that is, the so-called timestamp, which is a floating point number. I doubt this, so the type is actually a floating point:

type(time.time())
For high-end siege lions, remember that January 1, 1970 should be the same as you should know October 1, 1949.

2. time. sleep () is used to suspend the current thread. It accepts a floating point number, indicating the suspension time, in seconds. For example:

Time. sleep (2.5) # Hold the current thread for 2.5 seconds and rest for 2.5 seconds. Let the bullet fly for a while.


3. time. clock () is used to return the number of seconds that have elapsed since the first call of this method. The legend can be accurate to a subtle level. I doubt this. To test it, at least it is not so reliable on windows, test:

import timetime.clock()time.sleep(1.2)print time.clock()


4. time. gmtime ([sec]) optional parameter [sec] indicates the number of seconds since January 1, January 1, 1970. The default value is time. time (), that is, the current cumulative number of seconds. This function returns a time. struct_time type object, which includes year, day, month, week, day, hour, minute, second, and other time description information.

Import timetime. gmtime () # returns the time_struct type object time. gmtime (time. time ()-24*60*60) corresponding to the current time. # returns the time_struct object corresponding to the current time yesterday.
 

5. time. localtime () and time. like gmtime (), I printed time at the beginning. gmtime () was found to be time incorrect, and a time was found. localtime (). If you try this operation, the previous localtime is the time_struct object corresponding to the local time, while the gmtime () returns a struct_time object corresponding to the International Standard Time!

6. The operation of time. mktime () is opposite to the operation of the above two functions, which is used to convert structt_time objects to accumulate seconds. You can test this and their results should be roughly equivalent.

import timeprint time.time()print time.mktime(time.localtime())print time.mktime(time.gmtime())

7. time. strftime () Interprets time as a time string in a given format. The function prototype is time. strftime (format [, t]) where t is a struct_time object, you can freely combine the desired format according to your requirements, but just like the printf function in C, control with % Controller

import timeprint time.strftime('%Y-%m-%d %H:%M:%S')print time.strftime('%w,%j',time.gmtime())

You can summarize the usage of these percent control operators as follows:

% Y corresponds to four digits of the Year. % y corresponds to two digits Of The Year. % m corresponds to the month. % d corresponds to the date. % H corresponds to the time in the 24-hour format. % I corresponds to the time in the 12-hour format. % M corresponds to the minute. % S corresponds to second % j corresponds to day % w in a year corresponds to week % W in a year

8. time. strptime () interprets the time string as a struct_time object, which receives two string parameters. The usage is as follows:

import timeprint time.strptime('2009-06-23 15:30:53', '%Y-%m-%d %H:%M:%S')

9. time. ctime () converts a timestamp into a string. The default value is the current timestamp.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.