Time module in Python

Source: Internet
Author: User
Tags locale month name

The Strftime function was encountered during the process of Django's learning process, so we summed up and summed up some operations and functions commonly used in the time module with Python cookbook and Python docs.

    • Time.time ()

Represents a specific point in time, also known as the epoch: [English] [? i:p?k] [US] [?? P?k,? I?pɑk]) begins with the number of seconds that appear to be an intuitive floating point, which varies depending on the platform, usually at midnight on January 1, 1970. Cases:

>>> Import Time
>>> time.time ()1299080804.953
>>> Print time.asctime (time.gmtime (0))
Thu Jan : xx 1970
    • Time.gmtime ([secs]), Time.localtime([secs])

Time.gmtime converts any timestamp (the number of seconds that the parameter secs represents) to a tuple that is GMT (Greenwich Mean Time), or UTC (World standard times). It is also possible to pass a timestamp (the number of seconds from the beginning of the era) to Time.localtime, which is converted according to the current time zone.

>>>Time.gmtime (0)
Time.struct_time (Tm_year=1970, Tm_mon=1, Tm_mday=1, Tm_hour=0, Tm_min=0, Tm_sec=0, Tm_wday=3, Tm_yday=1, Tm_isdst=0)
>>>time.localtime (0)
Time.struct_time (Tm_year=1970, Tm_mon=1, Tm_mday=1, Tm_hour=8, Tm_min=0, Tm_sec=0, Tm_wday=3, Tm_yday=1, Tm_isdst=0)
    • Time.asctime ([t])

Converts struct_time to a 24-character string (24-character string) in the form of:

Thu Mar :from:
    • Time.strftime(format[, t])

Converts a tuple (tuple) or struct_time class, or parameter T, to a string of the specified format

    • Time.striptime (string[, format])

The opposite of Time.strftime, parses the given string and returns a Struct_time class. Cases:

>>> from time import strftime, gmtime
>>>strftime ("%a%d%b%Y%h:%m:%s")
'Thu 00:29:41'
>>>strftime ("%a%d%b%Y%h:%m:%s", Gmtime ())
'Wed 16:30:09'
>>>Time.strptime (" one by one", "%d%b%y")
...
Valueerror:day isOut of range forMonth
>>>Time.strptime ("---- Feb", "%d%b%y")
Time.struct_time (Tm_year= ., Tm_mon=2, Tm_mday= -, Tm_hour=0, Tm_min=0, Tm_sec=0, Tm_wday=0, Tm_yday= -, Tm_isdst=-1)
    • Time.sleep (secs)

You can implement the delay in a python program in seconds, and support the delay of non-integer seconds for floating-point numbers. Cases:

>>> for I inch Range (3):
Time.sleep (2.5)
print"Hello world! "


Hello world!
Hello world!
Hello world!
>>>


Attached one: struct_time

Index Attribute Values
0 Tm_year (for example, 1993)
1 Tm_mon range [1, 12]
2 Tm_mday range [1, 31]
3 Tm_hour range [0, 23]
4 Tm_min Range [0, 59]
5 Tm_sec Range [0, 61]; See (1) in strftime ()description
6 Tm_wday Range [0, 6], Monday is 0
7 Tm_yday range [1, 366]
8 Tm_isdst 0, 1 or-1; See below

Annex II: Directives can embedded in the format string

Directive meaning Notes
%a Locale ' s abbreviated weekday name.
%A Locale ' s full weekday name.
%b Locale ' s abbreviated month name.
%B Locale ' s full month name.
%c Locale ' s appropriate date and time representation.
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%I Hour (12-hour clock) as a decimal number [01,12].
%j Day of the year as a decimal number [001,366].
%m Month as a decimal number [01,12].
%M Minute as a decimal number [00,59].
%p Locale ' s equivalent of either AM or PM. (1)
%s Second as a decimal number [00,61]. (2)
%u Week number of the year (Sunday as the first day of the Week) as a decimal number [00,53]. A New year preceding the first Sunday is considered to is in week 0. (3)
%w Weekday as a decimal number [0 (Sunday), 6].
%W Week number of the year (Monday as the first day of the Week) as a decimal number [00,53]. A New year preceding the first Monday is considered to is in week 0. (3)
%x Locale ' s appropriate date representation.
%x Locale ' s appropriate time representation.
%y Year without century as a decimal number [00,99].
%Y Year with century as a decimal number.
%Z Time zone name (no characters if no time zone exists).
%% A literal '% ' character.

Reference: 1. Python Cookbook

2. Docs:http://docs.python.org/library/time.html#time.struct_time

Time module in Python

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.