标签:style 数字 cal nta 浮点数 size 利用 对象 space
time模块在time模块中,time.time()和time.sleep()函数是最有用的模块。
time.time()函数返回自那一刻以来的秒数,是一个浮点值。
可以通过两次调用time.time(),相减后得到这两次调用之间经过的时间。也可以采用cProfile.run()函数剖析代码。
>>> import time >>> dir(time) ['CLOCK_MONOTONIC', 'CLOCK_MONOTONIC_RAW', 'CLOCK_PROCESS_CPUTIME_ID', 'CLOCK_REALTIME', 'CLOCK_THREAD_CPUTIME_ID', '_STRUCT_TM_ITEMS', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'altzone', 'asctime', 'clock', 'clock_getres', 'clock_gettime', 'clock_settime', 'ctime', 'daylight', 'get_clock_info', 'gmtime', 'localtime', 'mktime', 'monotonic', 'perf_counter', 'process_time', 'sleep', 'strftime', 'strptime', 'struct_time', 'time', 'timezone', 'tzname', 'tzset'] >>> time.time() 1518951868.44606 >>> t1=time.time() >>> t=time.time()-t1 >>> print(t) 20.188032150268555
如果需要让函数暂停一下,就调用time.sleep()函数,并传入希望程序暂停的秒数。
在IDLE中按Ctrl-C不会中断time.sleep()调用。IDLE会等待到暂停结束,再抛出KeyboardInterrupt异常。
利用一个while循环,配合time.sleep()函数,可以让程序暂停,直到一个特定的日期。
>>> import time >>> for i in range(3): ... print(time.time()) ... time.sleep(1) ... print(time.time()) ... time.sleep(10) ... 1518952469.0750868 1518952470.076255 1518952480.08646 1518952481.0876613 1518952491.097851 1518952492.0990806 >>>
使用内置的round()函数可以进行数字的四舍五入,可以精确控制时间的精度。
>>> import time >>> now=time.time() >>> now 1518952700.4760277 >>> round(now,2) 1518952700.48 >>> round(now,4) 1518952700.476 >>> round(now) 1518952700
datetime模块
从time模块中,可以看到时间可读性较差。datetime模块能以更方便的格式显示日期,或对日期进行算数运算。
datetime模块有自己的datetime数据类型。datetime值表示一个特定的时刻。
datetime对象可以用比较操作符进行比较,得到True/False值。
unix纪元时间戳可以通过datetime.datetime.fromtimestamp(),转换为datetime对象。datetime对象的日期和时间将根据本地时区转换。
>>> import datetime >>> datetime.datetime.now() datetime.datetime(2018, 2, 18, 19, 22, 32, 105656) >>> dt=datetime.datetime(2088,8,8,18,28,38,0) >>> dt.year,dt.month,dt.day (2088, 8, 8) >>> dt.hour,dt.minute,dt.second (18, 28, 38) >>> datetime.datetime.now() > dt False
datetime模块还提供了timedelta数据类型,它表示一段时间,而不是一个时刻。
>>> import datetime >>> delta=datetime.timedelta(days=11,hours=10,minutes=9,seconds=8) >>> delta.days,delta.seconds,delta.microseconds (11, 36548, 0) >>> delta.total_seconds() 986948.0 >>> str(delta) '11 days, 10:09:08'
算数运算符可以对datetime值进行日期运算。
利用+和-运算符,timedelta对象与datetime对象或其他timedelta对象相加或相减。
利用*和/运算符,timedelta对象可以乘以或除以整数或浮点数。
>>> import datetime >>> dt=datetime.datetime.now() >>> dt datetime.datetime(2018, 2, 18, 19, 33, 22, 97390) >>> thousandDays=datetime.timedelta(days=1000) >>> dt+thousandDays datetime.datetime(2020, 11, 14, 19, 33, 22, 97390) >>> dt+(2*thousandDays) datetime.datetime(2023, 8, 11, 19, 33, 22, 97390)
利用strftime()方法,可以将datetime对象显示为字符串。
strptime()函数与strftime()方法相反。
Directive | Meaning | Example |
---|---|---|
%a | Weekday as locale’s abbreviated name. | Sun, Mon, …, Sat (en_US); So, Mo, …, Sa (de_DE) |
%A | Weekday as locale’s full name. | Sunday, Monday, …, Saturday (en_US); Sonntag, Montag, …, Samstag (de_DE) |
%w | Weekday as a decimal number, where 0 is Sunday and 6 is Saturday. | 0, 1, …, 6 |
%d | Day of the month as a zero-padded decimal number. | 01, 02, …, 31 |
%b | Month as locale’s abbreviated name. | Jan, Feb, …, Dec (en_US); Jan, Feb, …, Dez (de_DE) |
%B | Month as locale’s full name. | January, February, …, December (en_US); Januar, Februar, …, Dezember (de_DE) |
%m | Month as a zero-padded decimal number. | 01, 02, …, 12 |
%y | Year without century as a zero-padded decimal number. | 00, 01, …, 99 |
%Y | Year with century as a decimal number. | 0001, 0002, …, 2013, 2014, …, 9998, 9999 |
%H | Hour (24-hour clock) as a zero-padded decimal number. | 00, 01, …, 23 |
%I | Hour (12-hour clock) as a zero-padded decimal number. | 01, 02, …, 12 |
%p | Locale’s equivalent of either AM or PM. | AM, PM (en_US); am, pm (de_DE) |
%M | Minute as a zero-padded decimal number. | 00, 01, …, 59 |
%S | Second as a zero-padded decimal number. | 00, 01, …, 59 |
%f | Microsecond as a decimal number, zero-padded on the left. | 000000, 000001, …, 999999 |
%z | UTC offset in the form +HHMM or -HHMM (empty string if the object is naive). | (empty), +0000, -0400, +1030 |
%Z | Time zone name (empty string if the object is naive). | (empty), UTC, EST, CST |
%j | Day of the year as a zero-padded decimal number. | 001, 002, …, 366 |
%U | Week number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0. | 00, 01, …, 53 |
%W | Week number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0. | 00, 01, …, 53 |
%c | Locale’s appropriate date and time representation. | Tue Aug 16 21:30:00 1988 (en_US); Di 16 Aug 21:30:00 1988 (de_DE) |
%x | Locale’s appropriate date representation. | 08/16/88 (None); 08/16/1988 (en_US); 16.08.1988 (de_DE) |
%X | Locale’s appropriate time representation. | 21:30:00 (en_US); 21:30:00 (de_DE) |
%% | A literal '%' character. | % |
>>> import datetime >>> dt20180218=datetime.datetime.now() >>> dt20180218.strftime('%Y/%m/%d %H:%M:%S') '2018/02/18 19:45:13' >>> dt20180218.strftime('%I:%M %p') '07:45 PM' >>> dt20180218.strftime('%B of %y') 'February of 18' >>> datetime.datetime.strptime('February 18,2018','%B %d,%Y') datetime.datetime(2018, 2, 18, 0, 0)
标签:style 数字 cal nta 浮点数 size 利用 对象 space
原文地址:http://blog.51cto.com/juispan/2071838