python笔记7:日期和时间
Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间。
时间间隔是以秒为单位的浮点小数。
每个时间戳都以自从1970年1月1日午夜(历元)经过了多长时间来表示。
时间戳单位最适于做日期运算。但是1970年之前的日期就无法以此表示了。太遥远的日期也不行,UNIX和Windows只支持到2038年。
例如函数time.time()用于获取当前时间戳
#!/usr/bin/python
# -*- coding: UTF- -*- import time; # 引入time模块 ticks = time.time()
print "当前时间戳为:", ticks
输出结果:
当前时间戳为: 1459994552.51
什么是时间元组?
很多Python函数用一个元组装起来的9组数字处理时间:
序号 | 字段 | 值 |
---|---|---|
0 | 4位数年 | 2008 |
1 | 月 | 1 到 12 |
2 | 日 | 1到31 |
3 | 小时 | 0到23 |
4 | 分钟 | 0到59 |
5 | 秒 | 0到61 (60或61 是闰秒) |
6 | 一周的第几日 | 0到6 (0是周一) |
7 | 一年的第几日 | 1到366 (儒略历) |
8 | 夏令时 | -1, 0, 1, -1是决定是否为夏令时的旗帜 |
上述也就是struct_time元组。这种结构具有如下属性:
序号 | 属性 | 值 |
---|---|---|
0 | tm_year | 2008 |
1 | tm_mon | 1 到 12 |
2 | tm_mday | 1 到 31 |
3 | tm_hour | 0 到 23 |
4 | tm_min | 0 到 59 |
5 | tm_sec | 0 到 61 (60或61 是闰秒) |
6 | tm_wday | 0到6 (0是周一) |
7 | tm_yday | 1 到 366(儒略历) |
8 | tm_isdst | -1, 0, 1, -1是决定是否为夏令时的旗帜 |
获取当前时间
从返回浮点数的时间辍方式向时间元组转换,只要将浮点数传递给如localtime之类的函数。
#!/usr/bin/python
# -*- coding: UTF- -*- import time localtime = time.localtime(time.time())
print "本地时间为 :", localtime
以上实例输出结果:
本地时间为 : time.struct_time(tm_year=, tm_mon=, tm_mday=, tm_hour=, tm_min=, tm_sec=, tm_wday=, tm_yday=, tm_isdst=)
获取格式化的时间
你可以根据需求选取各种格式,但是最简单的获取可读的时间模式的函数是asctime():
#!/usr/bin/python
# -*- coding: UTF- -*- import time localtime = time.asctime( time.localtime(time.time()) )
print "本地时间为 :", localtime
以上实例输出结果:
本地时间为 : Thu Apr ::
格式化日期
我们可以使用 time 模块的 strftime 方法来格式化日期:
time.strftime(format[, t])
#!/usr/bin/python
# -*- coding: UTF- -*- import time # 格式化成2016-- ::39形式
print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 格式化成Sat Mar :: 2016形式
print time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()) # 将格式字符串转换为时间戳
a = "Sat Mar 28 22:24:24 2016"
print time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y"))
输出结果:
-- ::
Mon Aug ::
1459203864.0
python中时间日期格式化符号:
- %y 两位数的年份表示(00-99)
- %Y 四位数的年份表示(000-9999)
- %m 月份(01-12)
- %d 月内中的一天(0-31)
- %H 24小时制小时数(0-23)
- %I 12小时制小时数(01-12)
- %M 分钟数(00=59)
- %S 秒(00-59)
- %a 本地简化星期名称
- %A 本地完整星期名称
- %b 本地简化的月份名称
- %B 本地完整的月份名称
- %c 本地相应的日期表示和时间表示
- %j 年内的一天(001-366)
- %p 本地A.M.或P.M.的等价符
- %U 一年中的星期数(00-53)星期天为星期的开始
- %w 星期(0-6),星期天为星期的开始
- %W 一年中的星期数(00-53)星期一为星期的开始
- %x 本地相应的日期表示
- %X 本地相应的时间表示
- %Z 当前时区的名称
- %% %号本身
获取某月日历
Calendar模块有很广泛的方法用来处理年历和月历,例如打印某月的月历:
#!/usr/bin/python
# -*- coding: UTF- -*- import calendar cal = calendar.month(, )
print "以下输出2016年1月份的日历:"
print cal;
以上实例输出结果:
以下输出2016年1月份的日历:
January
Mo Tu We Th Fr Sa Su
Time 模块
Time 模块包含了以下内置函数,既有时间处理的,也有转换时间格式的:
asctime() 函数接受时间元组并返回一个可读的形式为"Tue Dec 11 18:07:14 2008"(2008年12月11日 周二18时07分14秒)的24个字符的字符串。
time.asctime([t]))
t -- 9个元素的元组或者通过函数 gmtime() 或 localtime() 返回的时间值。
#!/usr/bin/python
import time t = time.localtime()
print "time.asctime(t): %s " % time.asctime(t)
输出结果:
time.asctime(t): Tue Feb ::
clock() 函数以浮点数计算的秒数返回当前的CPU时间。用来衡量不同程序的耗时,比time.time()更有用。
这个需要注意,在不同的系统上含义不同。在UNIX系统上,它返回的是"进程时间",它是用秒表示的浮点数(时间戳)。而在WINDOWS中,第一次调用,返回的是进程运行的实际时间。而第二次之后的调用是自第一次调用以后到现在的运行时间。(实际上是以WIN32上QueryPerformanceCounter()为基础,它比毫秒表示更为精确)
time.clock()
返回值:
该函数有两个功能,
在第一次调用的时候,返回的是程序运行的实际时间;
以第二次之后的调用,返回的是自第一次调用后,到这次调用的时间间隔
在win32系统下,这个函数返回的是真实时间(wall time),而在Unix/Linux下返回的是CPU时间。
#!/usr/bin/python
import time def procedure():
time.sleep(2.5) # measure process time
t0 = time.clock()
procedure()
print time.clock() - t0, "seconds process time" # measure wall time
t0 = time.time()
procedure()
print time.time() - t0, "seconds wall time"
输出结果:
0.0 seconds process time
2.50023603439 seconds wall time
ctime() 函数把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。 如果参数未给或者为None的时候,将会默认time.time()为参数。它的作用相当于 asctime(localtime(secs))。
time.ctime([ sec ])
#!/usr/bin/python
import time print "time.ctime() : %s" % time.ctime()
输出结果:
time.ctime() : Tue Feb ::
python笔记7:日期和时间的更多相关文章
- Python中的日期和时间
感觉C语言作为一门编程的入门语言还是很好的,相比较之下,Python为代表的一些语言,适合很多非计算机专业的编程入门学习. Python 日期和时间 Python 程序能用很多方式处理日期和时间,转换 ...
- python: 基本的日期与时间转换
需要执行简单的时间转换,比如天到秒,小时到分钟等的转换. 为了执行不同时间单位的转换和计算,请使用datetime 模块.比如,为了表示一个时间段,可以创建一个timedelta 实例,就像下面这样: ...
- PythonCookBook笔记——数字日期和时间
数字日期和时间 数字的四舍五入 用round函数,指定值和小数位数. >>> round(1.23, 1) 1.2 >>> round(1.27, 1) 1.3 & ...
- Python学习--15 日期和时间
获取当前时间 # coding: utf-8 from datetime import datetime now = datetime.now() print(now) print(now.strft ...
- 19、Python标准库: 日期和时间
一.time时间模块 import time 1 .时间戳 时间戳(timestamp):时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量. time_stamp = tim ...
- python时间戳、日期、时间转换
1.str转时间戳 # 字符类型的时间 tss1 = '2013-10-10 23:40:00' # 转为时间数组 timeArray = time.strptime(tss1, "%Y-% ...
- Python正则表达式匹配日期与时间
#!/usr/bin/env python # -*- coding: utf-8 -*- __author__ = 'Randy' import re from datetime import da ...
- $python日期和时间的处理
总结一下python中对日期和时间的常用处理方法. 准备 import time,datetime 常用操作 输出当前的日期时间 方式一: now = time.localtime() print ' ...
- Python3-Cookbook总结 - 第三章:数字日期和时间
第三章:数字日期和时间 在Python中执行整数和浮点数的数学运算时很简单的. 尽管如此,如果你需要执行分数.数组或者是日期和时间的运算的话,就得做更多的工作了. 本章集中讨论的就是这些主题. Con ...
随机推荐
- 基于Dubbo框架构建分布式服务(三)
我们将上面开发的服务提供方服务,部署到2个独立的节点上(192.168.14.1和10.10.4.125),然后可以通过Dubbo管理中心查看对应服务的状况,如图所示: 上图中可以看出,该服务有两个独 ...
- PHP 进程详解
.note-content { font-family: "Helvetica Neue", Arial, "Hiragino Sans GB", STHeit ...
- linux shell 用sed命令在文本的行尾或行首添加字符
转自 http://www.cnblogs.com/aaronwxb/archive/2011/08/19/2145364.html 昨天写一个脚本花了一天的2/3的时间,而且大部分时间都耗在了sed ...
- Java 随机抽奖
package Third; import java.util.Scanner; public class LotteryOdds { public static void main(String[] ...
- nginx服务器命令
nginx -s reload :修改配置后重新加载生效 nginx -s reopen :重新打开日志文件nginx -t -c /path/to/nginx.conf 测试nginx配置文件是 ...
- C语言创建及解析Json的使用法则
参考原文:http://blog.csdn.net/xukai871105/article/details/33013455 JSON(JavaScriptObject Notation)是一种轻量级 ...
- phpstorm 使用技巧
专题1 专题2 专题3 专题4 快捷键
- Red5 1.0.0RC1 集成到tomcat6.0.35中运行&部署新的red5项目到tomcat中
1.下载red5-war-1.0-RC1.zip 解压之得到 ROOT.war 文件. 2.处理tomcat. 下载apache-tomcat-6.0.35-windows-x86.zip包,解压到你 ...
- [Storm] Storm与asm的恩恩怨怨
asm的引用冲突 1. Jersey & Storm 0.9.3 jersey 1.8 (which depends on asm 3.0) Storm 0.93 (which depends ...
- Apache Torque的使用
这篇文章学习如何使用Torque,作为一个ORM(a tool that maps relational databases to java classes) 用Torque访问数据库,需要如下步骤 ...