Python Learning note 23 (Time and Date (DateTime package))

Source: Internet
Author: User
Tags time and date timedelta

Python provides several built-in modules for manipulating date times, like Calendar,time,datetime.

Time Package

The time package is based on the C language library function (functions). Python's interpreter is usually written in C, and some Python functions directly invoke the C-language library function.

Import  Time Print (Time.time ())   # wall clock time, Unit:second Print (Time.clock ())  # processor clock time, Unit:second

Time.sleep () can put the program into hibernation until after a certain interval and then wake the program and let the program continue to run.

Import  Time Print ('start') time.sleep (     ) # sleep for ten seconds Print ('wakeup')
DateTime Package

The DateTime module defines the following classes:

    • Datetime.date: A class that represents a date. The commonly used attributes are year, month and day;
    • Datetime.time: A class that represents time. The commonly used properties are hour, minute, second, microsecond;
    • Datetime.datetime: Represents the DateTime.
    • Datetime.timedelta: Represents the time interval, which is the length between two points in time.
    • Datetime.tzinfo: Related information about the time zone. (This class is not discussed in detail here, and interested children's shoes can refer to the Python manual)

Note : These types of objects above are immutable (immutable).

Import datetime>>> t = datetime.datetime (2014,12,22,10,15,20)print(t) 2014-12-22 10:15:20      

Operation:

The DateTime package also defines the time interval object (Timedelta). A time-point (DateTime) plus a time interval (Timedelta) can be used to get a new point in time (DateTime).

>>> T1 = datetime.datetime (2014,12,12,12,12,12)>>> t2 = datetime.datetime ( 2014,12,13,12,12,12)>>> delta1 = datetime.timedelta (seconds =)>>> Delet2 = Datetime.timedelta (weeks = 1)print(t1 + delta1)print(t2 + delta2)  Print(t2-t1)print(T2 > t1)  # Two DateTime objects can also be compared to  True 

Python Learning note 23 (Time, DateTime package)

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.