Python standard library 02 time and date (times, datetime package)

Source: Internet
Author: User
Tags local time time and date timedelta

Vamei Source: Http://www.cnblogs.com/vamei Welcome reprint, Please also keep this statement. Thank you!

Python has good time and date management capabilities. In fact, the computer only maintains a wall clock (wall clock time), which is the time interval from the beginning of a fixed time to the present. The choice of time starting point is related to the computer, but the starting point of a computer is fixed. Other date information is calculated from this time. In addition, the computer can measure the actual time the CPU is running, that is, the processor time (processor clock times) to measure the performance of the computing machine. When the CPU is idle, the processor time is paused.

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 Timeprint (Time.time ())   # Wall clock time, Unit:secondprint (Time.clock ())  # Processor clock time, Unit:se Cond

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 timeprint (' start ') time.sleep     # Sleep for ten Secondsprint (' Wake Up ')

This method is available when we need to see the program running state regularly.

The time package also defines the Struct_time object. The object is actually converting the wall clock time to year, month, day, hour, minute, second ... Date information, stored in the properties of the object (Tm_year, Tm_mon, Tm_mday ...). The following method converts the wall clock time to a Struct_time object:

st = Time.gmtime ()      # returns the UTC time in struct_time format st = Time.localtime ()   # Returns the local time in struct_time format, which is determined by the system environment.
s = Time.mktime (ST) # Convert struct_time format to wall clock time

DateTime Package

1) Introduction

The datetime package is a premium package based on the time package, giving us a layer of convenience.

DateTime can be understood as both a date and a time component. Date is the day (equivalent to the calendar) of the month and date, which is the exact time (equivalent to a watch) in 24 hours of the day, minutes and seconds. You can manage the two separately (the Datetime.date class, the Datetime.time Class), or you can combine them (the Datetime.datetime class). Because the structure is similar, we will only introduce the Datetime.datetime class.

For example, the time I see now is September 3, 2012 21:30, we can express it in the following way:

Import Datetimet = Datetime.datetime (2012,9,3,21,30) print (t)

The returned T has the following properties:

Hour, minute, second, microsecond

Year, month, Day, weekday # weekday = Week

2) 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). Like today's 3 o'clock in the morning plus 5 hours to get today's 8 o'clock in the morning. Similarly, two point-in-time subtraction will get a time interval.

Import Datetimet = Datetime.datetime (2012,9,3,21,30)
T_next = Datetime.datetime (2012,9,5,23,30) delta1 = Datetime.timedelta (seconds = +) Delta2 = Datetime.timedelta (weeks = 3) Print (t + delta1) print (t + delta2)
Print (T_NEXT-T)

It can also be days, hours, milliseconds, microseconds when passing parameters (such as seconds and weeks above) to Datetime.timedelta.

Two DateTime objects can also be compared. For example, using the above T and T_next:

Print (T > T_next)

3) DateTime object and string conversion

If we have a string, how do we convert it to a DateTime object?

One method is to use the previous regular expression to search for a string. But the time information actually has the very obvious characteristic, we can read the time information in the format reading way.

From datetime import datetime

Str
T = datetime.strptime (str, format)

Strptime, p = parsing


We tell python the format of the date contained in our STR string through format. In format,%y represents the location of the year, and%m indicates where the month appears ....

In turn, we can also invoke the Strftime () method of a DateTime object to convert a DateTime object to a string of a specific format. Like the T_next defined above,

Print (t_next.strftime (format))

Strftime, F = formatting

The exact format can be found in the official documentation. In the case of a Linux system, you can also consult the Manual of the date command ($man date), both of which are connected.

Summarize

Time, sleep

datetime, Timedelta

Format time

Python standard library 02 time and date (times, 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.