Summary of common time operations in Python: Obtain the current time, time function, and application.

Source: Internet
Author: User
Tags julian day month name time and date

Summary of common time operations in Python: Obtain the current time, time function, and application.

This example describes common time operations in Python. We will share this with you for your reference. The details are as follows:

We pilot a required module

>>> import time

Set a time format, which will be used below

>>>ISOTIMEFORMAT='%Y-%m-%d %X'

Let's take a look at the current time. Similar to many other languages, this is from epoch (January 1, 1970 00:00:00) to the current number of seconds.

>>> time.time()1180759620.859

The above cannot be understood. Please change the format to see it.

>>> time.localtime()(2007, 6, 2, 12, 47, 7, 5, 153, 0)

Localtime returns the time in tuple format. There is a function similar to it called gmtime (). The difference between the two functions is the time zone, and gmtime () returns the value of the 0 time zone, localtime returns the value of the current time zone.

>>> time.strftime( ISOTIMEFORMAT, time.localtime() )'2007-06-02 12:54:29′

We have defined the time format and used strftime to convert the time. If the current time is used, time. localtime () does not need to be used.

>>> time.strftime( ISOTIMEFORMAT, time.localtime( time.time() ) )'2007-06-02 12:54:31′>>> time.strftime( ISOTIMEFORMAT, time.gmtime( time.time() ) )'2007-06-02 04:55:02′

The difference between gmtime and localtime is shown above.

View the time zone

>>> time.timezone-28800

The value above is a second value, which is the description of the difference between the current time zone and the 0 time zone.-28800 =-8*3600, that is, the UTC + 8 zone.

Post a few simple functions

Def ISOString2Time (s): ''' convert a ISO format time to second from: 2006-04-12 16:46:40 to: 23123123 converts a time to ''' return time. strptime (s, ISOTIMEFORMAT) def Time2ISOString (s): ''' convert second to a ISO format time from: 23123123: 16:46:40 converts the given second to the defined format '''return time. strftime (ISOTIMEFORMAT, time. localtime (float (s) def dateplustime (d, t): ''' d = 16:46:40 t = 2 hours Return 18:46:40 calculates the number of seconds of the difference between a date. time2sec is another function that can be used to process strings such as 3 days, 13 minutes, and 10 hours. Then, you can write this code back, A regular expression is required. '''Return Time2ISOString (time. mktime (ISOString2Time (d) + time2sec (t) def dateMinDate (d1, d2): '''minus to iso format date, return seconds calculates the second of the two time differences ''' d1 = ISOString2Time (d1) d2 = ISOString2Time (d2) return time. mktime (d1)-time. mktime (d2)

I. Introduction

The time module provides functions for various operation times.

Note: There are two methods to indicate time:

The first method is the timestamp (offset calculated in seconds relative to 00:00:00). The timestamp is unique.

The second type is represented as an array (struct_time). There are nine elements, respectively, indicating that the struct_time of the same timestamp varies with the time zone.

Year (four digits, e.g. 1998)
Month (1-12)
Day (1-31)
Hours (0-23)
Minutes (0-59)
Seconds (0-59)
Weekday (0-6, Monday is 0)
Julian day (day in the year, 1-366)
Whether DST (Daylight Savings Time) flag (-1, 0 or 1) Is Daylight Saving Time
If the DST flag is 0, the time is given in the regular time zone;
If it is 1, the time is given in the DST time zone;
If it is-1, mktime () shoshould guess based on the date and time.

Introduction of Baidu on Ghost: http://baike.baidu.com/view/100246.htm

Introduction to UTC refer to the introduction to UTC below: http://www.bkjia.com/article/40758.htm

Ii. Function Introduction

1.asctime()

Asctime ([tuple])-> string

Converts a struct_time (default time) to a string.

Convert a time tuple to a string, e.g. 'sat Jun 06 16:26:11 123 '.
When the time tuple is not present, current time as returned by localtime ()
Is used.

2.clock()

Clock ()-> floating point number

This function has two functions,

During the first call, the actual running time is returned;

After the second call, the returned time interval from the first call to the current call is

Example:

import timeif __name__ == '__main__':  time.sleep(1)  print "clock1:%s" % time.clock()  time.sleep(1)  print "clock2:%s" % time.clock()  time.sleep(1)  print "clock3:%s" % time.clock()

Output:

clock1:3.35238137808e-006clock2:1.00004944763clock3:2.00012040636

The first clock outputs the program running time.

The second and third clock outputs are the time interval from the first clock.

3.sleep(...)

Sleep (seconds)

The thread delays running at a specified time. After testing, the unit is seconds. However, there is one of the following statements in the help document, which cannot be understood.

"The argument may be a floating point number for subsecond precision ."

4.ctime(...)

Ctime (seconds)-> string

Converts a timestamp (current time by default) into a time string.

For example:

time.ctime()

Output: 'sat Mar 28 22:24:24 123'

5.gmtime(...)

Gmtime ([seconds])-> (tm_year, tm_mon, tm_day, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst)

Converts a timestamp to struct_time of UTC time zone (0 Time Zone). If the seconds parameter is not input, the current time is used as the conversion standard.

6.localtime(...)

Localtime ([seconds])-> (tm_year, tm_mon, tm_day, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst)

Converts a timestamp to struct_time of the current time zone. If the seconds parameter is not input, the current time is used as the conversion standard.

7.mktime(...)

Mktime (tuple)-> floating point number

Converts a struct_time to a timestamp.

8.strftime(...)

Strftime (format [, tuple])-> string

Returns the specified struct_time (current time by default) according to the specified formatted string.

Time and date formatting symbols in python:

% Y two-digit year (00-99)
% Y indicates the four-digit year (000-9999)
% M month (01-12)
One day in % d month (0-31)
% H hour in 24-hour format (0-23)
% I 12-hour (01-12)
% M minutes (00 = 59)
% S seconds (00-59)

% A local simplified week name
% A local full week name
% B local simplified month name
% B local full month name
% C local Date and Time
% J one day in the year (001-366)
% P local equivalent of A. M. or P. M.
% U number of weeks in a year (00-53) Sunday is the start of the week
% W Week (0-6), Sunday is the beginning of the week
% W number of weeks in a year (00-53) Monday is the start of the week
% X local date Representation
% X Local Time Representation
% Z Current Time Zone name
% Itself

9.strptime(...)

Strptime (string, format)-> struct_time

Converts a time string to a time string in the array format based on the specified formatting character.

For example:

The format string for 11:45:39 is: % Y-% m-% d % H: % M: % S

Sat Mar 28 22:24:24 2009 formatted string: % a % B % d % H: % M: % S % Y

10.time(...)

Time ()-> floating point number

Returns the timestamp of the current time.

Iii. Doubts

1. Renewal

In struct_time, it seems useless to parse, for example

a = (2009, 6, 28, 23, 8, 34, 5, 87, 1)b = (2009, 6, 28, 23, 8, 34, 5, 87, 0)

A and B indicate the time stamp and the standard time respectively. The conversion between them is related to the timestamp 3600, but the output is 646585714.0 after the conversion.

4. Small applications

1. Obtain the current time using python

time.time()Obtain the current Timestamp
time.localtime()Struct_time format of the current time
time.ctime()String format of the current time

2. format strings in python

Formatted as 11:45:39

time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

Formatted as Sat Mar 28 22:24:24 2009

time.strftime("%a %b %d %H:%M:%S %Y", time.localtime())

3. Convert the format string to the timestamp

a = "Sat Mar 28 22:24:24 2009"b = time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y"))

PS: This site also provides several Unix timestamp conversion and date online tools, which are very useful for your reference:

Unix timestamp Conversion Tool:
Http://tools.jb51.net/code/unixtime

Online date/day calculator:
Http://tools.jb51.net/jisuanqi/date_jisuanqi

Online calendar:
Http://tools.jb51.net/bianmin/wannianli

Online calendar/Calendar Conversion Tool:
Http://tools.jb51.net/bianmin/yinli2yangli

Related Article

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.