time模块

time.pyFunctions:

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 floatgmtime() -- 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 ‘‘
时间: 2024-11-03 21:48:39

time模块的相关文章

Day5 - 常用模块学习

本节大纲: 模块介绍(模块导入方法) time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 configparser hashlib subprocess logging模块 re正则表达式 模块,用一堆代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才能完成(函数又

c# 无法加载xxx.dll 找不到指定的模块(如何指定文件夹)

如果直接放在项目运行目录,例如bin/debug可以直接加载,但是这样比较乱. 如果在放debug里面的一个文件夹里面,有可能会报错“无法加载xxx.dll 找不到指定的模块”. 如果路径写成这样就会报错 framework/linphone 解决方法:将/ 改成"\" framework\linphone

LEDAPS1.3.0版本移植到windows平台----HuCsm云掩膜模块

这个是2012年左右放在百度空间的,谁知百度空间关闭...转移到博客园. 最近项目用到3.1.2版本的LEDAPS,新版本的使用情况会在后续文章中慢慢丰富. HuCsm是将LEDAPS项目中的TM/ETM+大气校正流程系列算法中的云掩膜模块由linux系统移植到windows下的产物,代码本身改动不大,使用接口不变. 包含文件: HuCsm.exe hd423m.dll hm423m.dll 编译程序需要包含的静态库有: gctp.lib hdfeos.lib hd423m.lib hm423m

Python学习系列----第五章 模块

5.1 如何引入模块 在Python中用关键字import来引入某个模块,比如要引用模块math,就可以在文件最开始的地方用import math来引入.在调用math模块中的函数时,必须这样引用: 模块名.函数名 有时候我们只需要用到模块中的某个函数,只需要引入该函数即可,此时可以通过语句 from 模块名 import 函数名1,函数名2.... 5.2 如何定义自己的模块 在Python中,每个Python文件都可以作为一个模块,模块的名字就是文件的名字. 比如有这样一个文件test.py

Python:hashlib加密模块,flask模块写登录接口

hashlib模块 主要用于加密相关的操作,(比如说加密字符串)在python3的版本里,代替了md5和sha模块,主要提供 sha1, sha224, sha256, sha384, sha512 ,md5 这些加密方式 import  hashlib m = hashlib.md5()   #用md5加密的方式(md5加密后无法解密),创建一个md5的对象 m.update(b"Hello")  #b代表二进制字节bytes,把字符串hello转成字节,然后加密:用b给一个变量转换

python如何使用pymysql模块

Python 3.x 操作MySQL的pymysql模块详解 前言pymysql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而MySQLdb不支持3.x版本. 一.安装 pip3 install pymysql 二.pymysql方法 pymysql.connect()参数说明 参数 类型 说明 host str mysql服务器地址 port int mysql服务器端口号 user str 用户名 passwd str

微控工具xp模块-开发版[微信(wechat)二次开发模块]

http://repo.xposed.info/module/com.easy.wtool 微控工具xp模块-开发版[微信(wechat)二次开发模块] 基于xposed框架的微信二次开发模块,方便开发者用微信做一些扩展功能(如微信群发.多群直播等...) 目前支持功能: 发文本消息 发图片消息 发语音消息 发视频消息 获取微信好友列表 群列表 支持群发消息 支持消息转发(目前支持文本.图片.语音.视频.图文消息转发) 群管理功能(建群.加人.踢人.设置公告.改群名.退群.解散群) [注:本模块

用ESP8266 WIFI模块连接服务器,并实现与服务器相互通讯

最近在做一个智能锁的项目,该项目要求实现在任何地方(当然是要有网络的)可以在手机上用APP开锁.而我负责的部分主要是实现底层与服务器连接,并且要能相互通讯.考虑了很多问题,最终选择了用ESP8266 WIFI模块实现了这个功能.下面向大家就简单分享一下. 工具:网络调试助手  ESP8266  STM32F1开发板 首先,用网络调试助手来虚拟一个服务器,如下: 有了服务器后,接下来我们就要用WIFI来连接这个服务器.ESP8266 有三种工作模式,由于项目要求,我选用了STA中的客户端模式.下面

Saltstack批量编译部署nginx(多模块)

最近一直在研究saltstack的同步文件和批量执行命令,随着架构的变大,批量部署的需求也变得明显起来了,我需要用一条命令就部署好nginx和tomcat,并且符合我所有的环境需求,可以直接投入生产环境使用,这就需要用到saltstack的批量安装部署功能了.这篇文章主要介绍nginx的批量部署,下篇讲解tomcat多实例的批量部署方法. 环境介绍: Centos 6.5 salt 2015.5.10 nginx 1.12.0 minion:test 1.修改master配置文件,修改后重启服务

python:模块导入之浅认识

(1)python有大量的模块: 1.内部提供的模块:不需要安装,可以直接调用 2.第三方库:包括业内开源的模块和自己开发的,需要安装 (2)什么是pyc文件: pyc文件的pycodeobject的一种持久化保存,而pycodeobject则是python真正编译的结果 明白什么时pyc文件,则我们需要从python的运行过程说起: 1.首先当python程序运行时,编译的结果则是保存在位于内存中的pycodeobject中,当python运行结束时,将pycodeobject写入到pyc文件