Simple learning of the Pythontime Module

Source: Internet
Author: User
Tags sleep function
This article will focus on the Pythontime module. The Pythontime module provides some C-library functions for time and date management, if you are interested in the time module, you can refer to this article to perform classification learning for the Python time module, hoping to help you learn it.

I. Wall Clock Time

1. time ()

The core function time () of the time module. It returns the number of seconds since the epoch. the return value is a floating point. The specific precision depends on the platform.

>>>import time>>>time.time()1460599046.85416

2. ctime ()

Floating Point Numbers are generally used to store and compare dates, but are unfriendly to humans. 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'

Ii. CPU clock time

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

>>>import time>>>time.clock()0.07

Iii. Time Composition

The time module defines struct_time to maintain the time and date. 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 obtain the UTC time. localtime () is used to obtain the current time in the current time zone. The UTC time is actually the Greenwich Mean Time. The time difference between gmtime and China time is eight hours.

Locatime () = gmtime () + 8 hour

4. process the time zone

1. Get 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()

5. parsing and formatting time

The time module provides two functions: strptime () and strftime (), which can be converted between struct_time and time Value strings.

1. strptime ()

Converts string time to 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, 28 Jun 2001 14:17:15 +0000'

3. mktime ()

Converts struct_time to a floating point of time.

>>>from time import mktime, gmtime>>>mktime(gmtime())1460573789.0

6. sleep ()

The sleep function is used to hand over the current thread and ask it to wait for the system to wake it up again. 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 all the content of this article. I hope you can have a rough understanding of the Python time module.

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.