python3 时间和日期
时间间隔以秒为单位的浮点数。特别是在时间瞬间在自1970年1月1日(纪元)12点的表示。
在Python提供一个流行时间 time 模块,提供时间的的 函数功能,并可转换表示。函数 time.time()返回当前系统时间,从1970年1月1日12点起来的秒数。
示例
#!/usr/bin/python3
import time; # This is required to include time module. ticks = time.time()
print ("Number of ticks since 12:00am, January 1, 1970:", ticks)
Number of ticks since 12:00am, January 1, 1970: 1455508609.34375
日期计算使用刻度线是很容易做到的。 然而,新纪元日期之前不能以这种形式表示。在未来的一个日期也不能表示这种方式 - 分界点是在某个时候,在 UNIX和Windows上为 2038年。
| 索引 | 字段 | 值 |
|---|---|---|
| 0 | 4-digit year | 2016 |
| 1 | Month | 1 to 12 |
| 2 | Day | 1 to 31 |
| 3 | Hour | 0 to 23 |
| 4 | Minute | 0 to 59 |
| 5 | Second | 0 to 61 (60 or 61 are leap-seconds) |
| 6 | Day of Week | 0 to 6 (0 is Monday) |
| 7 | Day of year | 1 to 366 (Julian day) |
| 8 | Daylight savings | -1, 0, 1, -1 means library determines DST |
示例
>>> print (time.localtime())
time.struct_time(tm_year=2016, tm_mon=3, tm_mday=15, tm_hour=9, tm_min=29, tm_sec=2, tm_wday=0, tm_yday=46, tm_isdst=0)
| 索引 | 属性 | 值 |
|---|---|---|
| 0 | tm_year | 2016 |
| 1 | tm_mon | 1 to 12 |
| 2 | tm_mday | 1 to 31 |
| 3 | tm_hour | 0 to 23 |
| 4 | tm_min | 0 to 59 |
| 5 | tm_sec | 0 to 61 (60 or 61 are leap-seconds) |
| 6 | tm_wday | 0 to 6 (0 is Monday) |
| 7 | tm_yday | 1 to 366 (Julian day) |
| 8 | tm_isdst | -1, 0, 1, -1 means library determines DST |
划时代浮点值从秒时刻转换生成为时间元组,浮点值传递给一个函数返回时间的元组并带上所有九个项目无效(例如,本地时间)。
#!/usr/bin/python3
import time localtime = time.localtime(time.time())
print ("Local current time :", localtime)
Local current time : time.struct_time(tm_year=2016, tm_mon=2, tm_mday=15, tm_hour=9, tm_min=29, tm_sec=2, tm_wday=0, tm_yday=46, tm_isdst=0)
#!/usr/bin/python3
import time localtime = time.asctime( time.localtime(time.time()) )
print ("Local current time :", localtime)
Local current time : Mon Feb 15 09:34:03 2016
日历模块提供各种各样年度和月度的日历方法。在这里,我们将打印给定月份(2008年1月)的日历 -
#!/usr/bin/python3
import calendar cal = calendar.month(2016, 2)
print ("Here is the calendar:")
print (cal)
Here is the calendar:
February 2016
Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29
time模块
Python提供一个流行时间模块,为时间和表示之间转换提供了相应函数。这里是所有可用方法的列表 -
| SN | 函数及描述 |
|---|---|
| 1 |
如果定义则在当地的DST时区偏移,在UTC西部秒数。这是负值,如果当地的MST时区是UTC以东 (在西欧,包括英国).只使用这个,如果白天不为零。 |
| 2 |
接受时间元组,并返回一个可读24个字符的字符串,如 'Tue Dec 11 18:07:14 2008'. |
| 3 |
返回当前CPU时间为几秒钟的浮点数。要测量计算成本的不同的方法,time.clock()的值比time.time更有用。 |
| 4 |
类似 asctime(localtime(secs))和不带参数的 ctime()函数
|
| 5 |
接受从新纪元用秒表示瞬间和返回用UTC表示时间元组t。注: t.tm_isdst 总是为 0 |
| 6 |
接受从新纪元用秒表示瞬间和返回本地时间的时间元组t(t.tm_isdst 为 0 或 1, 根据DST是否通过局部规则适用于时刻秒) |
| 7 |
接受表示为本地时间的时间元组并返回从新纪元以秒表示瞬间的一个浮点值
|
| 8 |
线程暂停secs秒调用
|
| 9 |
time.strftime(fmt[,tupletime]) 接受表示为本地时间的时间元组瞬间,并返回表示由字符串指定 fmt 瞬间的字符串 |
| 10 |
time.strptime(str,fmt='%a %b %d %H:%M:%S %Y') 解析 str 按照格式字符串格式化并返回时间元组格式的时间
|
| 11 |
返回当前时刻,从新纪元以来的秒的浮点数
|
| 12 |
通过重置库例程使用时间转换规则。 环境变量TZ指定如何做到这一点 |
| SN |
属性及说明
|
|---|---|
| 1 | time.timezone
属性time.timezone是在本地时区以UTC的秒偏移量(不包括DST) |
| 2 | time.tzname
属性time.tzname是一对区域设置相关的字符串
|
calendar模块
默认情况下,日历采用星期一作为一周的第一天,周日是最后一个。如要改变这种情况,调用calendar.setfirstweekday()函数。
| SN | 方法及描述 |
|---|---|
| 1 | calendar.calendar(year,w=2,l=1,c=6)
返回一个多行字符串使用格式化成用c空格分隔的三列逐年的日历 |
| 2 | calendar.firstweekday( )
返回每周工作日开始的当前设置。默认情况下,当日历被首次导入是0,这意味着星期一 |
| 3 | calendar.isleap(year)
如果 year 是闰年返回True;否则为false |
| 4 | calendar.leapdays(y1,y2)
返回跨越润年在范围内(y1,y2)的总数
|
| 5 | calendar.month(year,month,w=2,l=1)
返回一个多行字符串以及逐年月月份的日历,每周一行加上两个标题行。 |
| 6 | calendar.monthcalendar(year,month)
返回列表的列表整数。
|
| 7 | calendar.monthrange(year,month)
返回两个整数。 |
| 8 | calendar.prcal(year,w=2,l=1,c=6)
类似打印 calendar.calendar(year,w,l,c). |
| 9 | calendar.prmonth(year,month,w=2,l=1)
类似打印 calendar.month(year,month,w,l). |
| 10 | calendar.setfirstweekday(weekday)
设定每个星期的第一天工作日代码。工作日代码是0(星期一)至6(星期日)
|
| 11 | calendar.timegm(tupletime)
time.gmtime反转:接受时间元组形式的时刻,并返回同一时刻作为自新纪元以来秒的浮点数 |
| 12 | calendar.weekday(year,month,day)
返回给定日期是星期几代码。 工作日代码是0(星期一)至60(星期日);月数是1(1月)到12(12月) |
python3 时间和日期的更多相关文章
- 【转】Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历))
Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历)) 本文由 Luzhuo 编写,转发请保留该信息. 原文: http://blog. ...
- Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历))
Python3 日期时间 相关模块(time(时间) / datatime(日期时间) / calendar(日历)) 本文由 Luzhuo 编写,转发请保留该信息. 原文: http://blog. ...
- python3数字、日期和时间
1.对数值进行取整 #使用内建的round(value,ndigits)函数来取整,ndigits指定保留的位数,在取整时会取值在偶数上,如1.25取一位会取整1.2,1.26会取整1.3 In [1 ...
- 5.Python3标准库-日期和时间
''' 不同于int,str,float,Python没有包含对应日期和时间的原生类型,不过提供了3个相应的模块,可以采用多种表示来管理日期和时间值 time模块由底层C库提供与时间相关的函数.它包含 ...
- Python3.x:日期库dateutil简介
Python3.x:日期库dateutil简介 安装 pip install python-dateutil 关于parser #字符串可以很随意,可以用时间日期的英文单词,可以用横线.逗号.空格等做 ...
- Android随笔之——Android时间、日期相关类和方法
今天要讲的是Android里关于时间.日期相关类和方法.在Android中,跟时间.日期有关的类主要有Time.Calendar.Date三个类.而与日期格式化输出有关的DateFormat和Simp ...
- Lua库之时间和日期操作
Lua库之时间和日期操作 (2010-02-07 18:41:20) 转载▼ os.time() <== 返回当前系统的日历时间os.date() <== 返回本地化的时间字符串,这里是& ...
- date 显示或设置系统时间和日期
显示或设置系统时间和日期 date [options] [+format] date [options] [new date] date用来显示系统的时间和日期,超级用户可以使用date来更改系统时钟 ...
- java时间和日期类型
在java中,代表时间和日期的类型包括:java.util.Date和java.util.Calendar,此外,在JDBC API中还提供了3个扩展类,java.UtilDate类的子类:java. ...
随机推荐
- GridView视图
本文实现如下效果 Test_Grid.java public class Test_Grid extends Activity { private GridView gridview; private ...
- hdu 5344 MZL's xor(数学之异或)
Problem Description MZL loves xor very much.Now he gets an array A.The length of A ≤i,j≤n) The xor ...
- Direct3D 索引缓存
小学的时候我们知道3个顶点组成一个三角形,那么四个顶点我们会说有4个三角形.这就是一个顶点同时参与了四次绘制三角形的结果. 在程序中也一样,比如我们绘制的两个三角形是挨着一起的,总有几个顶点是重合的. ...
- qt绘制设备
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' from PyQt4.QtGui import * from Py ...
- 格而知之16:我所理解的Block(3)
23.在前文中的例子中,Block结构体里的isa指针还没有详细讲解,这个指针都被置向了_NSConcreteStackBlock,它标识了Block的类型. 其实除了_NSConcreteStack ...
- [每日一题] OCP1z0-047 :2013-07-27 外部表――不能被DML和建索引
首先看官方文档上的解释: Managing External Tables Oracle Database allows you read-only access to data in externa ...
- LR选择哪种方式录制
LR选择哪种方式录制,有以下考虑原则: 1.基于浏览器的应用程序推荐使用HTML-basic script方式录制 2.不是基于浏览器的应用程序推荐使用URL-basic script方式录制 3.如 ...
- Javascript - IE8下parseInt()方法的取值异常
公司的测试小妹妹跑来对我说,下拉框第9项始终无法正确提交的时候,我还以为见鬼了. parseInt()会把'0'开头的数字以8进制来解析,当有大于7的数字时候就按10进制来解析. // p ...
- Silverlight Application 无法调用js的方法
今天下午做项目的时候遇到了这个错误 找了很多原因没找出,在蛋疼之际找出了问题, Silverlight调js方法的时候 js方法里面不能出现一点问题,如果有一点问题就会报这个错误.
- 0112.1——iOS开发之理解iOS中的MVC设计模式
模型-视图-控制器(Model-View-Controller,MVC)是Xerox PARC在20世纪80年代为编程语言Smalltalk-80发明的一种软件设计模式,至今已广泛应用于用户交互应用程 ...