Python Time Module Learning

Source: Internet
Author: User
Tags time zones

The Python time module provides some C library functions for managing times and dates , because it is bound to the underlying C implementation, so some details are based on the specific platform.

I. Wall clock time 1.time ()

the core function of the time module is Time (), which returns the number of seconds from the beginning of the era, the return value is a floating point, and the specific precision depends on the platform.

>>>import time

>>>time.time ()

1460599046.85416

2.ctime ()

Floating-point numbers are typically used to store and compare dates, but not friendly to humans, and to record and print time, you can use CTime ().

>>>import time

>>>time.ctime ()

' Thu Apr 14 10:03:58 2016 '

>>> later = Time.time () +5

>>> Time.ctime (later)

' Thu Apr 14 10:05:57 2016 '

two. Processor clock time

Clock () returns the processor clock time, and its return value is typically used for performance testing and benchmarking. Therefore, they reflect the actual running time of the program.

>>>import time

>>>time.clock ()

0.07

three. Time Composition

the time module defines struct_time to maintain times and dates, where each component is stored separately for access.

Import time

def show_struct (s):

print ' Tm_year: ', s.tm_year

print ' Tm_mon: ', S.tm_mon

Print "Tm_mday:", S.tm_mday

Print "Tm_hour:",s.tm_hour

Print "Tm_min:", s.tm_min

Print "Tm_sec:", s.tm_sec

Print "Tm_wday:", S.tm_wday

Print "Tm_yday:", S.tm_yday

Show_struct (Time.gmtime ())

Show_struct (Time.localtime ())

Gmtime () is used to get the UTC time, LocalTime () is used to get the current time zone, UTC time is actually Greenwich mean time, and it has a eight hour lag with China.

Locatime () = Gmtime () + 8hour

four. Working with time zones 1. Get The time difference

>>>import time

>>>time.timezone/3600

-8

2. Set the time zone

ZONES = ["GMT", "Europe/amsterdam"]

For zone in ZONES:

os.environ["TZ"] = Zone

Time.tzset ()

Five. Parsing and formatting time

The time module provides two functions strptime() and strftime(), which can be struct_time and a time-value string conversion.

1.strptime ()

Used to convert the string time into struct_time format:

>>> Now=time.ctime ()

>>> Time.strptime (now)

Time.struct_time (tm_year=2016, tm_mon=4, tm_mday=14, tm_hour=10, tm_min=48, tm_sec=40, tm_wday=3, tm_yday=105, tm_ ISDST=-1)

2.strftime ()

Formatted output for TIME

>>> from time import gmtime, strftime

>>> strftime ("%a,%d%b%Y%h:%m:%s +0000", Gmtime ())

' Thu, June 2001 14:17:15 +0000 '

3.mktime ()

a floating-point number that is used to convert struct_time into time

>>>from time Import Mktime, gmtime

>>>mktime (Gmtime ())

1460573789.0

six. Sleep ()

The Sleep function is used to hand over the current thread, asking it to wait for the system to wake it up again, and if the write program has only one thread, it will actually block the process and do nothing.

Import time

Def FUCN ():

Time.sleep (5)

Print "Hello, World"

Execute the above code and wait 5 seconds before outputting the information.

To learn more about the basic Python Tutorials , go to the Wheat Academy.

Python Time Module Learning

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.