Python module--time module
First, the Python Time introduction:
There are two ways to represent time in Python:
1, timestamp notation, that is, an integer or float type is a time interval in seconds. The base value for this time is calculated from 0 o'clock on January 1 in 1970.
2, tuple format notation, that is, a Python of the Data Structure representation. This tuple has 9 integral types of content. Each represents a different time meaning.
Second, the DateTime module
2.1 All functions of the DateTime module
In [2]: Dir (datetime) out[2]: [' maxyear ', ' minyear ', ' __doc__ ', ' __file__ ', ' __name__ ', ' __package__ ', ' Date ', ' DateTime ' , ' Datetime_capi ', ' time ', ' Timedelta ', ' Tzinfo ']in [3]:
2.2 DateTime module functions
Datetime.datetime.now () Gets the current system time
In [9]: Print Datetime.datetime.now () 2016-11-29 12:27:55.391272#datetime.datetime.now (). Strftime ('%y-%m-%d%h:%m:%s ') #一般用于数据库跟时间有关字段的赋值 #in []: Print Datetime.datetime.now (). Strftime ('%y-%m-%d%h:%m:%s ') 2016-11-29 13:26:22
Datetime.date.today () get today's date
in [+]: Print datetime.date.today () 2016-11-29
Datetime.timedelta (Days=x) Given two time difference values
In [16]: help (Datetime.timedelta) Class timedelta (__builtin__.object) | difference between two datetime values. | | methods Defined here: | | __abs__ (...) | x.__abs__ () <==> abs (x) | | __add__ (...) | x.__add__ (y) <==> x+y | | __div__ (...) | x.__div__ (y) <==> x/y | | __eq__ (...) | x.__eq__ (y) <==> x==y | | __floordiv__ (...) | x.__floordiv__ (y) <==> x//y | | __ge__ (...) | x.__ge__ (y) <==> x>=y | | __getattribute__ (...) | x.__getattribute__ (' name ') <==> x.name# #小李子In [14]: print datetime.timedelta (Days=1) 1 day, 0:00:00
Datetime.datetime.strptime () converts a string format into a time
# #注意这个函数字符串中连接符只能是 '-', others will error In [26]: print datetime.datetime.strptime ("2016-12-20", '%y-%m-%d ') 2016-12-20 00:00:00in [25]: print datetime.datetime.strptime ("2016 12 20", '%Y-% m-%d ')---------------------------------------------------------------------------valueerror Traceback (Most recent call last) <ipython-input-25-aac02a333bd5> in <module> ()----> 1 print Datetime.datetime.strptime ("2016 12 20", '%y-%m-%d ')/usr/local/python2.7/lib/python2.7/_strptime.pyc in _strptime (Data_string, format) 323 if not found: 324 raise valueerror ("time&Nbsp;data %r does not match format %r " %--> 325 (Data_string, format) 326 if len (data_string) != found.end (): 327 raise valueerror ("unconverted data remains: %s" % valueerror: time data ' 2016 12 20 ' does not match format '% y-%m-%d ' In [27]: import timein [28]: time.strftime ("%y-%m-%d %h:%m:%s", Time.localtime ()) out[28]: ' 2016-11-29 13:18:21 '
Three, Time module
3.1 All functions of the time module
In [3]: Dir (Time) out[3]: [' __doc__ ', ' __file__ ', ' __name__ ', ' __package__ ', ' accept2dyear ', ' altzone ', ' asctime ', ' clock ', ' ctime ', ' Daylight ', ' gmtime ', ' localtime ', ' mktime ', ' sleep ', ' strftime ', ' strptime ', ' struct_time ', ' time ', ' Timezon ' E ', ' tzname ', ' Tzset ']
3.2 Time Module specific function explanation
time.time () --Returns the current timestamp, floating-point number form. Parameter not accepted
in [+]: Time.time () out[30]: 1480398573.196829
Time. sleep ()--delays a period of time, accepting integer, floating-point type.
In [ten]: Time.sleep (3) #执行完成后睡眠3秒
Time.localtime () -Converts the timestamp to a local time-tuple format. Accepts a floating-point timestamp parameter whose default value is the current timestamp.
In [All]: Time.localtime () out[11]: Time.struct_time (tm_year=2016, tm_mon=12, tm_mday=10, tm_hour=11, tm_min=49, tm_sec= Tm_wday=5, tm_yday=345, tm_isdst=0) in []: Time.localtime (1481341446.412076) out[13]: Time.struct_time (tm_year= Tm_mon=12, tm_mday=10, tm_hour=11, tm_min=44, tm_sec=6, tm_wday=5, tm_yday=345, tm_isdst=0)
Time.asctime ()-Converts the format of a time tuple to a string form. Accepts a time tuple whose default value is LocalTime () return value
in [+]: Time.asctime () out[14]: ' Sat Dec 11:51:26 ' in [Max]: Time.asctime ((2016,12,10,11,52,47,5,345,0)) out[23]: ' Sat Dec 10 11:52:47 2016 '
Time.strftime ()-Converts a time tuple to a string in the specified format. Accepts string formatting strings and time tuples. The time tuple is optional and defaults to LocalTime ()
in [+]: Time.strftime ("%Y%h:%m:%s") out[25]: ' 2016 11:59:55 '
Strptime ()--resolves the time string in the specified format to a time-tuple, strftime () reverse process. Accepts a string, time format 2 parameters, are required.
in [+]: time.strptime (' Sat 12:00:59 ') out[28]: Time.struct_time (tm_year=2016, tm_mon=12, tm_mday=10, Tm_hour =12, Tm_min=0, tm_sec=59, tm_wday=5, tm_yday=345, Tm_isdst=-1)
This article from the "11931192" blog, reproduced please contact the author!
Python module--time module