Time template translation and description for python

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

Python embedded time template translation and description

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.
License Introduction: http://baike.baidu.com/view/100246.htm
UTC Introduction: http://wenda.tianya.cn/wenda/thread? Tid = 283921a9da7c5aef & CLK = wttpcts

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:

View plaincopy to clipboardprint?
Import time
If _ 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-006
Clock2: 1.00004944763
Clock3: 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 () Get the current Timestamp
Time. localtime () struct_time form 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 "))

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.