码迷,mamicode.com
首页 > 其他好文 > 详细

time模块

时间:2016-05-06 00:39:22      阅读:638      评论:0      收藏:0      [点我收藏+]

标签:

 

 

time.py
Functions:

time() -- return current time in seconds since the Epoch as a float # 1970-01-01 00:00:00

>>> time.time()
1462459975.1511018


clock() -- return CPU time since process start as a float

>>> time.clock()
9.777779019400511e-06
>>> time.clock()
0.9610759887080621
>>> time.clock()
1.856179835705376


sleep() -- delay for a number of seconds given as a float
gmtime() -- convert seconds since Epoch to UTC tuple

>>> time.gmtime()
time.struct_time(tm_year=2016, tm_mon=5, tm_mday=5, tm_hour=14, tm_min=56, tm_sec=23, tm_wday=3, tm_yday=126, tm_isdst=0)
>>> time.gmtime(0)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)
>>> time.gmtime(1)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=1, tm_wday=3, tm_yday=1, tm_isdst=0)

>>> time.gmtime(1).tm_mon
1


localtime() -- convert seconds since Epoch to local time tuple

>>> time.localtime()
time.struct_time(tm_year=2016, tm_mon=5, tm_mday=5, tm_hour=22, tm_min=57, tm_sec=51, tm_wday=3, tm_yday=126, tm_isdst=0)
>>> time.localtime(0)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=8, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)


asctime() -- convert time tuple to string # 把time tuple(手写)类型或struct_time(也是time tuple)类型转换为字符串,时间为本地时间

>>> time.asctime()
‘Thu May 5 23:03:19 2016‘
>>> time.asctime(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Tuple or struct_time argument required
>>> time.asctime((1,))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: function takes exactly 9 arguments (1 given)
>>> time.asctime((1970,1,1,1,1,1,1,1,1))
‘Tue Jan 1 01:01:01 1970‘
>>> time.asctime(time.localtime(121231234124))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 22] Invalid argument
>>> time.asctime(time.localtime(1212312341))
‘Sun Jun 1 17:25:41 2008‘

>>> type(time.asctime())
<class ‘str‘>


ctime() -- convert time in seconds to string # 把秒转换为字符串,时间为本地时间

>>> time.ctime()
‘Thu May 5 23:05:38 2016‘
>>> time.ctime(1)
‘Thu Jan 1 08:00:01 1970‘
>>> type(time.ctime(1))
<class ‘str‘>


mktime() -- convert local time tuple to seconds since Epoch

>>> time.mktime(time.localtime())
1462460940.0


strftime() -- convert time tuple to string according to format specification # 把time tuple转为为指定格式的字符串

>>> time.strftime("%Y%m")
‘201605‘
>>> time.strftime("%Y%m",time.localtime())
‘201605‘
>>> time.strftime("%Y%m",time.localtime(1))
‘197001‘


strptime() -- parse string to time tuple according to format specification # 把时间格式化的字符串转换为time tuple

>>> time.strptime("20111111","%Y%m%d")
time.struct_time(tm_year=2011, tm_mon=11, tm_mday=11, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=315, tm_isdst=-1)


tzset() -- change the local timezone
"""




time.py源码
  1 # encoding: utf-8
  2 # module time
  3 # from (built-in)
  4 # by generator 1.138
  5 """
  6 This module provides various functions to manipulate time values.
  7 
  8 There are two standard representations of time.  One is the number
  9 of seconds since the Epoch, in UTC (a.k.a. GMT).  It may be an integer
 10 or a floating point number (to represent fractions of seconds).
 11 The Epoch is system-defined; on Unix, it is generally January 1st, 1970.
 12 The actual value can be retrieved by calling gmtime(0).
 13 
 14 The other representation is a tuple of 9 integers giving local time.
 15 The tuple items are:
 16   year (including century, e.g. 1998)
 17   month (1-12)
 18   day (1-31)
 19   hours (0-23)
 20   minutes (0-59)
 21   seconds (0-59)
 22   weekday (0-6, Monday is 0)
 23   Julian day (day in the year, 1-366)
 24   DST (Daylight Savings Time) flag (-1, 0 or 1)
 25 If the DST flag is 0, the time is given in the regular time zone;
 26 if it is 1, the time is given in the DST time zone;
 27 if it is -1, mktime() should guess based on the date and time.
 28 
 29 Variables:
 30 
 31 timezone -- difference in seconds between UTC and local standard time
 32 altzone -- difference in  seconds between UTC and local DST time
 33 daylight -- whether local time should reflect DST
 34 tzname -- tuple of (standard time zone name, DST time zone name)
 35 
 36 Functions:
 37 
 38 time() -- return current time in seconds since the Epoch as a float
 39 clock() -- return CPU time since process start as a float
 40 sleep() -- delay for a number of seconds given as a float
 41 gmtime() -- convert seconds since Epoch to UTC tuple
 42 localtime() -- convert seconds since Epoch to local time tuple
 43 asctime() -- convert time tuple to string
 44 ctime() -- convert time in seconds to string
 45 mktime() -- convert local time tuple to seconds since Epoch
 46 strftime() -- convert time tuple to string according to format specification
 47 strptime() -- parse string to time tuple according to format specification
 48 tzset() -- change the local timezone
 49 """
 50 # no imports
 51 
 52 # Variables with simple values
 53 
 54 altzone = -32400
 55 
 56 daylight = 0
 57 
 58 timezone = -28800
 59 
 60 _STRUCT_TM_ITEMS = 9
 61 
 62 # functions
 63 
 64 def asctime(p_tuple=None): # real signature unknown; restored from __doc__
 65     """
 66     asctime([tuple]) -> string
 67 
 68     Convert a time tuple to a string, e.g. ‘Sat Jun 06 16:26:11 1998‘.
 69     When the time tuple is not present, current time as returned by localtime()
 70     is used.
 71     """
 72     return ""
 73 
 74 def clock(): # real signature unknown; restored from __doc__
 75     """
 76     clock() -> floating point number
 77 
 78     Return the CPU time or real time since the start of the process or since
 79     the first call to clock().  This has as much precision as the system
 80     records.
 81     """
 82     return 0.0
 83 
 84 def ctime(seconds=None): # known case of time.ctime
 85     """
 86     ctime(seconds) -> string
 87 
 88     Convert a time in seconds since the Epoch to a string in local time.
 89     This is equivalent to asctime(localtime(seconds)). When the time tuple is
 90     not present, current time as returned by localtime() is used.
 91     """
 92     return ""
 93 
 94 def get_clock_info(name): # real signature unknown; restored from __doc__
 95     """
 96     get_clock_info(name: str) -> dict
 97 
 98     Get information of the specified clock.
 99     """
100     return {}
101 
102 def gmtime(seconds=None): # real signature unknown; restored from __doc__
103     """
104     gmtime([seconds]) -> (tm_year, tm_mon, tm_mday, tm_hour, tm_min,
105                            tm_sec, tm_wday, tm_yday, tm_isdst)
106 
107     Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.
108     GMT).  When ‘seconds‘ is not passed in, convert the current time instead.
109 
110     If the platform supports the tm_gmtoff and tm_zone, they are available as
111     attributes only.
112     """
113     pass
114 
115 def localtime(seconds=None): # real signature unknown; restored from __doc__
116     """
117     localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,
118                               tm_sec,tm_wday,tm_yday,tm_isdst)
119 
120     Convert seconds since the Epoch to a time tuple expressing local time.
121     When ‘seconds‘ is not passed in, convert the current time instead.
122     """
123     pass
124 
125 def mktime(p_tuple): # real signature unknown; restored from __doc__
126     """
127     mktime(tuple) -> floating point number
128 
129     Convert a time tuple in local time to seconds since the Epoch.
130     Note that mktime(gmtime(0)) will not generally return zero for most
131     time zones; instead the returned value will either be equal to that
132     of the timezone or altzone attributes on the time module.
133     """
134     return 0.0
135 
136 def monotonic(): # real signature unknown; restored from __doc__
137     """
138     monotonic() -> float
139 
140     Monotonic clock, cannot go backward.
141     """
142     return 0.0
143 
144 def perf_counter(): # real signature unknown; restored from __doc__
145     """
146     perf_counter() -> float
147 
148     Performance counter for benchmarking.
149     """
150     return 0.0
151 
152 def process_time(): # real signature unknown; restored from __doc__
153     """
154     process_time() -> float
155 
156     Process time for profiling: sum of the kernel and user-space CPU time.
157     """
158     return 0.0
159 
160 def sleep(seconds): # real signature unknown; restored from __doc__
161     """
162     sleep(seconds)
163 
164     Delay execution for a given number of seconds.  The argument may be
165     a floating point number for subsecond precision.
166     """
167     pass
168 
169 def strftime(format, p_tuple=None): # real signature unknown; restored from __doc__
170     """
171     strftime(format[, tuple]) -> string
172 
173     Convert a time tuple to a string according to a format specification.
174     See the library reference manual for formatting codes. When the time tuple
175     is not present, current time as returned by localtime() is used.
176 
177     Commonly used format codes:
178 
179     %Y  Year with century as a decimal number.
180     %m  Month as a decimal number [01,12].
181     %d  Day of the month as a decimal number [01,31].
182     %H  Hour (24-hour clock) as a decimal number [00,23].
183     %M  Minute as a decimal number [00,59].
184     %S  Second as a decimal number [00,61].
185     %z  Time zone offset from UTC.
186     %a  Locale‘s abbreviated weekday name.
187     %A  Locale‘s full weekday name.
188     %b  Locale‘s abbreviated month name.
189     %B  Locale‘s full month name.
190     %c  Locale‘s appropriate date and time representation.
191     %I  Hour (12-hour clock) as a decimal number [01,12].
192     %p  Locale‘s equivalent of either AM or PM.
193 
194     Other codes may be available on your platform.  See documentation for
195     the C library strftime function.
196     """
197     return ""
198 
199 def strptime(string, format): # real signature unknown; restored from __doc__
200     """
201     strptime(string, format) -> struct_time
202 
203     Parse a string to a time tuple according to a format specification.
204     See the library reference manual for formatting codes (same as
205     strftime()).
206 
207     Commonly used format codes:
208 
209     %Y  Year with century as a decimal number.
210     %m  Month as a decimal number [01,12].
211     %d  Day of the month as a decimal number [01,31].
212     %H  Hour (24-hour clock) as a decimal number [00,23].
213     %M  Minute as a decimal number [00,59].
214     %S  Second as a decimal number [00,61].
215     %z  Time zone offset from UTC.
216     %a  Locale‘s abbreviated weekday name.
217     %A  Locale‘s full weekday name.
218     %b  Locale‘s abbreviated month name.
219     %B  Locale‘s full month name.
220     %c  Locale‘s appropriate date and time representation.
221     %I  Hour (12-hour clock) as a decimal number [01,12].
222     %p  Locale‘s equivalent of either AM or PM.
223 
224     Other codes may be available on your platform.  See documentation for
225     the C library strftime function.
226     """
227     return struct_time
228 
229 def time(): # real signature unknown; restored from __doc__
230     """
231     time() -> floating point number
232 
233     Return the current time in seconds since the Epoch.
234     Fractions of a second may be present if the system clock provides them.
235     """
236     return 0.0
237 
238 # classes
239 
240 class struct_time(tuple):
241     """
242     The time value as returned by gmtime(), localtime(), and strptime(), and
243      accepted by asctime(), mktime() and strftime().  May be considered as a
244      sequence of 9 integers.
245 
246      Note that several fields‘ values are not the same as those defined by
247      the C language standard for struct tm.  For example, the value of the
248      field tm_year is the actual year, not year - 1900.  See individual
249      fields‘ descriptions for details.
250     """
251     def __init__(self, *args, **kwargs): # real signature unknown
252         pass
253 
254     @staticmethod # known case of __new__
255     def __new__(*args, **kwargs): # real signature unknown
256         """ Create and return a new object.  See help(type) for accurate signature. """
257         pass
258 
259     def __reduce__(self, *args, **kwargs): # real signature unknown
260         pass
261 
262     def __repr__(self, *args, **kwargs): # real signature unknown
263         """ Return repr(self). """
264         pass
265 
266     tm_hour = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
267     """hours, range [0, 23]"""
268 
269     tm_isdst = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
270     """1 if summer time is in effect, 0 if not, and -1 if unknown"""
271 
272     tm_mday = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
273     """day of month, range [1, 31]"""
274 
275     tm_min = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
276     """minutes, range [0, 59]"""
277 
278     tm_mon = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
279     """month of year, range [1, 12]"""
280 
281     tm_sec = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
282     """seconds, range [0, 61])"""
283 
284     tm_wday = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
285     """day of week, range [0, 6], Monday is 0"""
286 
287     tm_yday = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
288     """day of year, range [1, 366]"""
289 
290     tm_year = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
291     """year, for example, 1993"""
292 
293 
294     n_fields = 9
295     n_sequence_fields = 9
296     n_unnamed_fields = 0
297 
298 
299 class __loader__(object):
300     """
301     Meta path import for built-in modules.
302 
303         All methods are either class or static methods to avoid the need to
304         instantiate the class.
305     """
306     @classmethod
307     def create_module(cls, *args, **kwargs): # real signature unknown
308         """ Create a built-in module """
309         pass
310 
311     @classmethod
312     def exec_module(cls, *args, **kwargs): # real signature unknown
313         """ Exec a built-in module """
314         pass
315 
316     @classmethod
317     def find_module(cls, *args, **kwargs): # real signature unknown
318         """
319         Find the built-in module.
320 
321                 If ‘path‘ is ever specified then the search is considered a failure.
322 
323                 This method is deprecated.  Use find_spec() instead.
324         """
325         pass
326 
327     @classmethod
328     def find_spec(cls, *args, **kwargs): # real signature unknown
329         pass
330 
331     @classmethod
332     def get_code(cls, *args, **kwargs): # real signature unknown
333         """ Return None as built-in modules do not have code objects. """
334         pass
335 
336     @classmethod
337     def get_source(cls, *args, **kwargs): # real signature unknown
338         """ Return None as built-in modules do not have source code. """
339         pass
340 
341     @classmethod
342     def is_package(cls, *args, **kwargs): # real signature unknown
343         """ Return False as built-in modules are never packages. """
344         pass
345 
346     @classmethod
347     def load_module(cls, *args, **kwargs): # real signature unknown
348         """
349         Load the specified module into sys.modules and return it.
350 
351             This method is deprecated.  Use loader.exec_module instead.
352         """
353         pass
354 
355     def module_repr(module): # reliably restored by inspect
356         """
357         Return repr for the module.
358 
359                 The method is deprecated.  The import machinery does the job itself.
360         """
361         pass
362 
363     def __init__(self, *args, **kwargs): # real signature unknown
364         pass
365 
366     __weakref__ = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
367     """list of weak references to the object (if defined)"""
368 
369 
370     __dict__ = None # (!) real value is ‘‘
371 
372 
373 # variables with complex values
374 
375 tzname = (
376     Öйú±ê׼ʱ¼ä,
377     ÖйúÏÄÁîʱ,
378 )
379 
380 __spec__ = None # (!) real value is ‘‘

 

time模块

标签:

原文地址:http://www.cnblogs.com/owasp/p/5463808.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!