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. ...
随机推荐
- php创建带logo的二维码
<?php /** php使用二维码 **/ class MyQrcode{ const SIZE = 150; const LEVEL = "L"; const MARGI ...
- redis安装配置和使用;tomcat安装和使用
virtualbox主要有以下几种方式(不同版本号称法不一样,但实质是一样的): 1.Intelnal Network:利用主机上的全部的虚拟机构建一个虚拟网络 2.NAT:能訪问互联网,不能訪问主机 ...
- 在多线程环境中使用Jedis
Jedis是一个Java语言的Redis客户端,它为Java语言连接与操作Redis提供了简单易用的接口. Jedis不是线程安全的.故不应该在多线程环境中共用一个Jedis实例.可是.也应该避免直接 ...
- [Hapi.js] Extending the request with lifecycle events
Instead of using middlware, hapi provides a number of points during the lifecycle of a request that ...
- Unity 启动画面淡入淡出
前几天在玩我叫MT 2游戏的时候发现打开他们应用的时候发现他们Logo淡入淡出的效果做的挺好的,例如第一张是运营商腾讯的logo第二张是他们公司的游戏logo.我们也来模仿一下: 第一张图片:运营商的 ...
- 2. QT窗体间值的传递
一.主窗体与子窗体传参 方法有很多,这里介绍一种通过重载子窗体的构造函数实现主窗体参数传入到子窗体,并通过QT信号和槽的机制实现子窗口到主窗口值的传递. 主和子窗体的设置如下: 主要实现功能为: 1 ...
- 使用repo的本地开发流程
repo下的本地开发流程 单分支开发: 1 本地新建工作目录并初始化repo库: repo init; 2 下载代码(只取服务器当前分支): repo sync -c; 3 创建本地 ...
- MySQL主从同步、读写分离配置步骤
现在使用的两台服务器已经安装了MySQL,全是rpm包装的,能正常使用. 为了避免不必要的麻烦,主从服务器MySQL版本尽量保持一致; 环境:192.168.0.1 (Master) 192.168. ...
- No redirect found in host configuration file (C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet.config).
Configuration Error Description: An error occurred during the processing of a configuration file req ...
- Mysql操作个人收集
1.MySQL修改root密码 mysql> UPDATE user SET Password=PASSWORD('xxxx') where USER='root'; mysql> FLU ...