Recently learning Python, I think this language is relatively easy to get started, but some of the foundation is to rely on a bit of accumulation, so want to write some blog record My Learning path.
The first thing you need to know about this module is to type Help () in idle, and then type time to get the information about the module. Returned a lot of English, for us these bad English slag, only the internet to help translate, translation too long will not be affixed.
Say a few words that I think are useful.
Time modules represent times in three main ways:
-
Timestamp: The timestamp represents the offset that is calculated in seconds from 00:00:00 on January 1, 1970
-
2. Formatted string str
-
3. Yuan zu Struct_time
Struct_time has the following nine properties
Index Properties Value
0Tm_year(year) For example: 2013
1Tm_mon(month) 1~12
2Tm_mday(Sun) 1~31
3Tm_hour(hours) 0~23
4Tm_min(minutes) 0~59
5tm_sec(sec) 0~59
6Tm_wday(week) 0~6 (0 is Sunday, and so on)
7Tm_yday(The day of the year, 1~366)
8TM_ISDST(Daylight saving System) (Default is-1)
1.clock () function
The first call returns the floating-point number of the current time, and the second call returns the time interval between the second and first call, which can be used to measure the program run time interval, or very precisely. 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0057.gif "alt=" J_0057.gif "/>
Usage format: Time.clock ()
Test it.
Import time
Print (Time.clock ())
Print (Time.clock ())
The returned results are as follows:
1.8111075224350944e-06
0.027731074681018687
>>>
2.sleep () function
This function is to let the program pause for a given time, the parameter is in seconds, (the parameter can be a floating-point sub-second precision, which is the translation on the Help document, that is, very accurate) easier said than done.
Put the code, use the above-mentioned clock () function, very practical.
Usage format: time.sleep (senconds)
Import time
Print (Time.clock ())
Time.sleep (3)
Print (Time.clock ())
The results of the operation are as follows:
1.8111075224350944e-06
3.019148839834706
See that the program is tentative for 3 seconds before you run the print function.
3.time () function
This function returns a timestamp that begins in the years of the 1970.1.1 with a floating-point number.
Usage format: time.time ()
The code is as follows:
Import time
Print (Time.time ())
return Result:
1470984536.4895656
>>>
4.ctime () function
Converts a timestamp to a string, which defaults to the current system time.
Usage format: time.ctime ()
The code is as follows:
Import time
Print (Time.ctime ())
The results of the operation are as follows:
Fri 12 15:20:44 2016
>>>
5.localtime () function
The time stamp is changed to a time structure of a meta-ancestor, and the system timestamp is returned by default without writing parameters.
Use format: Time.localtime ([sec])
The code is as follows:
Import time
Print (Time.localtime ())
The returned results are as follows:
Time.struct_time (tm_year=2016, tm_mon=8, tm_mday=12, tm_hour=15, tm_min=26, tm_sec=53, tm_wday=4, tm_yday=225, tm_ isdst=0)
>>>
Import time
Print (Time.localtime (Time.time ()))
The returned results are as follows:
Time.struct_time (tm_year=2016, tm_mon=8, tm_mday=12, tm_hour=15, tm_min=32, tm_sec=34, tm_wday=4, tm_yday=225, tm_ isdst=0)
>>>
6.strftime () function
outputs the specified struct_time (default current time), based on the specified formatted string
Usage format: time.strftime ()
Python format symbols in time Date:
%y Two-digit year representation (00-99)
%Y Four-digit year representation (000-9999)
%m Month (01-12)
One day in%d months (0-31)
%H 24-hour hours (0-23)
%I 12-hour hours (01-12)
%M minutes (00=59)
%s seconds (00-59)
%a Local Simplified Week name
%A Local Full week name
%b a locally simplified month name
%B Local Full month name
%c Local corresponding date representation and time representation
%j Day of the Year (001-366)
%p the equivalent of a local a.m. or p.m.
%u weeks of the year (00-53) Sunday is the beginning of the week
%w Week (0-6), Sunday for the beginning of the week
%W Week of the Year (00-53) Monday is the beginning of the week
%x Local corresponding date representation
%x Local corresponding time representation
%Z the name of the current time zone
Percent% of the number itself
The code is as follows:
Import time
Print (Time.strftime ('%y-%m-%d '))
The results are as follows:
16-08-12
>>>
Code:
Import time
Print (Time.strftime ('%y-%m-%d-%h-%m-%s ', Time.localtime ()))
Results:
2016-08-12-15-49-20
>>>
There are all kinds of conversion functions, do not introduce each.
This article has a lot of references on-line resources, I hope you do not spray, thank you.
In addition, there are small partners who want to learn Python together. Add Group: 113584067
I am here waiting for you to come. 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0057.gif "alt=" J_0057.gif "/>
-------Snow
This article is from the "10962685" blog, please be sure to keep this source http://10972685.blog.51cto.com/10962685/1837342
Python Learning path------Time Module