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)