Python provides time, datetime, and calendar modules to process date and time.
I. Common functions of the time module
1. time. time
The number of seconds since January 1, January 1, 1970. This is a floating point number.
2. time. sleep
You can call time. sleep to suspend the current process. Time. sleep receives a floating point parameter, indicating the time when the process is suspended.
3. time. clock
In windows, time. clock () returns the number of seconds since the method was called for the first time. The accuracy is higher than 1 microsecond. You can use this function to record the execution time of the program. The following is a simple example:
1 >>> print time.clock()2 1.26999365003e-0063 >>> time.sleep(2)4 >>> print time.clock()5 21.42214196436 >>> time.sleep(3)7 >>> print time.clock()8 44.0470900595
4. time. gmtime
This function is prototype: time. gmtime ([sec]). The optional parameter sec indicates the number of seconds since January 1. The default value is time. time (). The function returns an object of the time. struct_time type. (Struct_time is the time object defined in the time module.) The following is a simple example:
1 >>> print time. gmtime () # Get the struct_time object of the current time 2 (2013, 4, 8, 4, 28, 30, 0, 98, 0) 3 >>> print time. gmtime (time. time () 4 (2013, 4, 8, 4, 28, 50, 0, 98, 0) 5 >>> print time. gmtime (time. time ()-24*60*60) # obtain the struct_time object 6 (2013, 4, 7, 4, 29, 10, 6, 97, 0) of the time of yesterday) 7 >>>
5. time. localtime
Similar to time. gmtime, struct_time is also returned, which can be viewed as a localized version of gmtime.
6. time. mktime
Time. mktime: opposite to gmtime () and localtime (), it receives the struct_time object as a parameter and returns a floating point number that represents the time in seconds. For example:
1 >>> print time.mktime(time.localtime())2 1365395531.0
7. time. strftime
Time. strftime converts a date to a string representation. Its function prototype is time. strftime (format [, t]). The format parameter is a format string (for details about the format string, refer to time. strftime). The optional parameter t is a struct_time object. The following example converts the struct_time object to a string representation:
1 >>> print time.strftime('%Y-%m-%d %H:%M:%S',time.gmtime())2 2013-04-08 04:35:34
Time. strftime parameter:Strftime (format [, tuple])-> string outputs the time and date formatting symbols in python Based on the specified formatting string for the specified struct_time (the default value is the current time:
% Y two-digit year (00-99) % Y four-digit year (000-9999) % m month (01-12) one day (0-31) % H 24-hour (0-23) % I 12-hour (01-12) in % d month) % M minutes (00 = 59) % S seconds (00-59) % a local simplified week name % A local full week name % B local simplified month name % B local full month name % c local corresponding date representation and Time Representation % j Years one day (001-366) % p local. m. or P. m. % U the number of weeks in a year (00-53) Sunday is the beginning of the week % w Week (0-6 ), sunday is the beginning of a week % W the number of weeks in a year (00-53) monday is the start of the week % x local corresponding date represents % X local corresponding time represents % Z Current Time Zone name % itself
8. time. strptime
Parses a time string in the specified format and returns the struct_time object. The prototype of this function is time. strptime (string, format). The two parameters are strings. The following is a simple example to parse a string into a struct_time object:
1 >>> print time.strptime('2013-04-09 12:30:25','%Y-%m-%d %H:%M:%S')2 (2013, 4, 9, 12, 30, 25, 1, 99, -1)
For more information, see the Python manual.
Ii. datetime Module
The interface provided by the time module is basically the same as that provided by the C standard library time. h. Compared with the time module, the datetime Module Interface is more intuitive and easier to call.
1. Two constants:
Datetime. MINYEAR and datetime. MAXYEAR indicate the minimum and maximum years that datetime can represent. MINYEAR = 1, MAXYEAR = 9999.
2. Several important classes:
2.1. datetime. date: indicates the date class. Common attributes include year, month, and day;
Year is returned between two constants;
The range of month is [1, 12]. (The month starts from 1, not from 0 );
Day is determined by month.
The date class provides common class methods and class attributes:
Date. max, date. min: the maximum and minimum date that the date object can represent;
Date. today (): returns a date object that represents the current local date.
1 >>> from datetime import *2 >>> import time3 >>> print date.today()4 2013-04-085 >>> print date.max6 9999-12-317 >>> print date.min8 0001-01-01
Instance methods and Attributes provided by date
- Date. year, date. month, date. day: year, month, and day;
- Date. replace (year, month, day): generates a new date object, replacing the attributes of the original object with the year, month, and day specified by the parameter. (The original object remains unchanged)
- Date. timetuple (): returns the time. struct_time object corresponding to the date;
- Date. weekday (): Return weekday. If it is Monday, return 0; if it is day 2, return 1, and so on;
- Data. isoweekday (): returns weekday. If it is Monday, 1 is returned. If it is day 2, 2 is returned, and so on;
- Date. isocalendar (): returns the tuples in the format of (year, month, day;
- Date. isoformat (): returns a string in the format of 'yyyy-MM-DD;
- Date. strftime (fmt): Custom formatted string. The following is a detailed description.
1 >>> now=date(2013,04,8) 2 >>> tomorrow=now.replace(day=9) 3 >>> print 'now:',now 4 now: 2013-04-08 5 >>> print 'tomorrow:',tomorrow 6 tomorrow: 2013-04-09 7 >>> print 'timetuple():',now.timetuple() 8 timetuple(): (2013, 4, 8, 0, 0, 0, 0, 98, -1) 9 >>> print 'weekday():',now.weekday()10 weekday(): 011 >>> print 'isoweekday():',now.isoweekday()12 isoweekday(): 113 >>> print 'isocalendar():',now.isocalendar()14 isocalendar(): (2013, 15, 1)15 >>> print 'isoformat():',now.isoformat()16 isoformat(): 2013-04-08
Date also reloads some operations, which allow us to perform the following operations on the date:
- Date2 = date1 + timedelta # date plus an interval, a new date object is returned (timedelta will be described below, indicating the interval)
- Date2 = date1-timedelta # Return a new date object after the date separation interval.
- Timedelta = date1-date2 # subtract two dates and return a time interval object
- Date1 <date2 # compare two dates
1 now = date. today () 2 tomorrow = now. replace (day = 7) 3 delta = tomorrow-now 4 print 'now: ', now, 'Tomorrow:', tomorrow 5 print 'timedelta :', delta 6 print now + delta 7 print tomorrow> now 8 9 ##---- result ---- 10 # now: 2010-04-06 tomorrow: 2010-04-07 11 # timedelta: 1 day, 0:00:00 #2010-04-07 13 # True
Reference: http://blog.csdn.net/JGood/article/details/5457284