Time Module Method:
Time.time (): Gets the timestamp of the current time
Time.localtime (): Accept a timestamp and convert it to a tuple of the current time. If you do not give arguments, Time.time () is passed in as a parameter by default
time.localtime (): |
index |
properties |
meaning |
0 |
Tm_ye AR |
year |
1 |
tm_mon |
month |
2 |
tm_mday |
Day |
3 |
tm_hour |
when |
4 |
tm_min |
min |
5 |
tm_sec |
sec |
6 |
tm_wday |
one week The day of the |
7 |
tm_yday |
the day of the year |
8 |
tm_isdst |
Daylight saving |
Time.mktime (): and Time.localtime () instead, it converts a time tuple into a timestamp (which must be given to a parameter)
Time.asctime (): A time tuple is represented as: "Sun Jul 28 03:35:26 2013" In this format, the default is to pass Time.localtime () as a parameter.
Time.ctime (): Converts a timestamp to the expression format of the Time.asctime (), and by default the Time.time () is passed as a parameter.
Time.gmtime (): Converts a timestamp to utc+0 time zone (China should be + 8 time zone, 8 hours apart), and will default to Time.time () as a parameter.
Time.strftime (Format,time.localtime ()): Converts a time tuple to a formatted time character, without giving the time tuple argument the default to pass Time.localtime () as a parameter
For example, the time format inside the Web log is time.strftime ('%d/%b/%y:%x ')
return Result:
Sun Jul 28 04:37:38 2013
Format
Property |
Format |
Meaning |
Range of values (format) |
Year |
%y |
Remove the years of the century |
00-99 |
%Y |
The full year |
|
%j |
The day ordinal of a year |
001-366 |
Month |
%m |
Month |
January 12 |
%b |
Name of the local simplified month |
Abbreviated English Month |
%B |
Name of the local full month |
Full English month |
Date |
%d |
The day of the one month |
January 31 |
Hours |
%H |
The first hours of the day (24-hour system) |
00-23 |
%l |
Number of hours (12-hour system) |
"01-12" |
Minutes |
%M |
Number of minutes |
00-59 |
Seconds |
%s |
Seconds |
00-59 |
Week |
%u |
Number of weeks in the year (starting from Sunday) |
00-53 |
%W |
Number of weeks in the year (starting from Monday) |
|
%w |
Day of the week one |
0-6 |
Time |
%Z |
China: It should be gmt+8 (China standard Time) |
Seeking Greater God's literacy |
Other |
%x |
Local corresponding date |
Day/month/year |
%x |
Local Phase printing time |
Hours: minutes: seconds |
%c |
Detailed date and time |
Day/month/year: minutes: Seconds |
%% |
'% ' character |
'% ' character |
%p |
Local AM or PM's corresponding character |
AM or PM |
Time.strptime (Stringtime,format): Converts the time string to an array of times according to the specified formatter.
For example:
Time.strptime (' 28/jul/2013:04:33:29 ', '%d/%b/%y:%x ')
return Result:
Copy the Code code as follows:
Time.struct_time (tm_year=2013, tm_mon=7, tm_mday=28, tm_hour=4, tm_min=33, tm_sec=29, tm_wday=6, tm_yday=209, TM_ISDST =-1)
Time.clock (): Returns the processor clock time, typically used for performance testing and benchmarking, as they reflect the actual time used by the program, which is not normally used.
Time.sleep (): Delay the specified time run, in seconds
Import Timeprint time.time () #打印时间戳print time.localtime () #打印本地时间元组print time.gmtime () #答应UTC + 0 timezone time tuple print Time.ctime () #打印asctime格式化时间print Time.mktime (Time.localtime ()) #将时间元组转换为时间戳print time.asctime () #打印格式化时间print time.strftime (' %d/%b/%y:%x ') #打印指定格式的时间格式 # Translate the time string and its format into the time-tuple print time.strptime (' 28/jul/2013:04:33:29 ', '%d/%b/%y:%x ') print '%0. 5f '%time.clock () #打印处理器时间for I in range (100000): passprint '%0.5f '%time.clock () #打印处理器时间
Look at the results:
[Root@localhost ~]# python time1.py
1364028568.55time.struct_time (tm_year=2013, tm_mon=3, tm_mday=23, tm_hour=4, tm_min=49, tm_sec=28, tm_wday=5, Tm_yday =82, Tm_isdst=1) time.struct_time (tm_year=2013, tm_mon=3, tm_mday=23, Tm_hour=8, tm_min=49, tm_sec=28, tm_wday=5, tm_ yday=82, tm_isdst=0) Sat Mar 04:49:28 20131364028568.0Sat Mar 04:49:28 201323/mar/2013:04:49:28time.struct_time (tm _year=2013, Tm_mon=7, tm_mday=28, tm_hour=4, tm_min=33, tm_sec=29, tm_wday=6, tm_yday=209, Tm_isdst=-1) 0.020000.03000
DateTime module
Datetime.time (): Generates a Time object. This time can be set by us, the default is 0 (this class is only for the time)
#coding: Utf-8import datetimeprint datetime.time () t = datetime.time (1, 3, 5,) print Tprint t.hour #时print t.minute #分prin T t.second #秒print t.microsecond #毫秒print datetime.time.max #一天的结束时间print datetime.time.min #一天的开始时间
Execute:
00:00:0001:03:05.00002523:59:59.99999900:00:00
Datetime.date (): Generates a Date object. This date will be set by us (this class is only for dates)
#coding: Utf-8import datetime# Set Date t = Datetime.date (2, 3) #打印设置日期的和元组print t.timetuple () #日期元组print tprint t.year # Year print t.month #月print t.day #日 # gets today's Day date = Datetime.date.today () print Todayprint datetime.datetime.now () #这个打印到毫秒级别 #获取今天日期的元组t1 = Today.timetuple () print t1# printed in ctime format (time.ctime () format) # '%a%b%d%h:%m:%s%Y ' Print t.ctime () print Today.ctime ()
Run results
Time.struct_time (tm_year=2013, tm_mon=2, tm_mday=3, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=34, Tm_isdst=-1) 2013-02-032013232013-07-282013-07-28 20:13:25.942000time.struct_time (tm_year=2013, tm_mon=7, tm_mday=28, tm_hour=0 , Tm_min=0, Tm_sec=0, tm_wday=6, tm_yday=209, Tm_isdst=-1) Sun Feb 3 00:00:00 2013Sun Jul 28 00:00:00 2013
Datetime.timedelta (): This class is used to do the arithmetic of time
Datetime.datetime.combine (Date,time): This is used to combine dates and times
#coding: utf-8import datetime# Printing: Representation format from milliseconds to weeks = converted to seconds (Total_seconds ()) for i in [Datetime.timedelta (Milliseconds=1), #1毫秒 D Atetime.timedelta (Seconds=1), #1秒 Datetime.timedelta (Minutes=1), #1分钟 Datetime.timedelta (Hours=1), #1小时 Datetime.timedelta (Days=1), #1天 Datetime.timedelta (Weeks=1)]: #11周 #print i + ': ' + i.total_seconds () print '%s =%s Secon DS '% (I,i.total_seconds ()) Printprint ' ~ ' * 20 + ' I am dividing line ' + ' ~ ' * 20print ' calculation time plus minus .... ' A = Datetime.datetime.now () print ' Now time is: ' Print Aprint ' plus 5 hours later: ' B = A + Datetime.timedelta (hours=5) print bprint ' plus one week later changes into: ' C = a + Datetime.timedelta (weeks=1) print Cprint ' minus one week later becomes: ' d = A-datetime.timedelta (weeks=1) print dprint ' Calculate 2 time difference how long ' p Rint '%s minus%s '% (b, a) print ' equals:%s '% (b-a) print '%s minus%s '% (A, D) print ' equals:%s '% (a-d) printprint ' ~ ' * 20 + ' I am split line ' + ' ~ ' * 2 0print ' Comparison 2 time: ' print ' compare the same day and a week ago ' Print a > dprint ' if compare D > A then return false ' Printprint ' ~ ' * 20 + ' I am split line ' + ' ~ ' * 20prin T ' s above are separate dates and times, now let's put them free to combine ' print ' assuming we want the time: 2014-01-05 13:14:25 ' t =Datetime.time (+, +) d = datetime.date (in.) Print datetime.datetime.combine (d, T)
Print as:
0:00:00.001000 = 0.001 seconds0:00:01 = 1.0 seconds0:01:00 = 60.0 seconds1:00:00 = 3600.0 seconds1 day, 0:00:00 = 86400.0 Seconds7 days, 0:00:00 = 604800.0 seconds
Calculate the time of the addition and subtraction ...
The time is now: 2013-07-28 21:34:33.531000 plus 5 hours later became: 2013-07-29 02:34:33.531000 plus a week later became: 2013-08-04 21:34:33.531000 minus a week later becomes: 2013-07-21 21:34:33.531000 calculate 2 time difference how long 2013-07-29 02:34:33.531000 minus 2013-07-28 21:34:33.531000 equals: 5:00:002013-07-28 21:34:33.531000 minus 2013-07-21 21:34:33.531000 equals: 7 days, 0:00:00 Compare 2 times: True if comparison of the same day and a week ago if you compare D > A then return false the above list is separated by the date and time, and now we're going to set them free. Suppose the time we want is: 2014-01-05 13:14:252014-01-05 13:14:25