Learn the Python time module simply

Source: Internet
Author: User
Tags time zones sleep function
In this paper, the Python Time module for the classification of learning, hope that everyone's learning is helpful.

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 10:03:58 ' >>> 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 Timedef 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_ydayshow_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 converted between the struct_time and the value strings.

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, 2 June 001 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 Timedef FUCN ():   time.sleep (5)   print "Hello, World"

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

The above is the entire content of this article, I hope you can have a Python time module to have a general understanding.

  • 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.