源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Python常用时间操作总结【取得当前时间、时间函数、应用等】

  • 时间:2020-01-21 22:07 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Python常用时间操作总结【取得当前时间、时间函数、应用等】
本文实例讲述了Python常用时间操作。分享给大家供大家参考,具体如下: 我们先导入必须用到的一个module
>>> import time

设置一个时间的格式,下面会用到
>>>ISOTIMEFORMAT='%Y-%m-%d %X'

看一下当前的时间,和其他很多语言相似这是从epoch(1970 年 1 月 1 日 00:00:00)开始到当前的秒数。
>>> time.time()
1180759620.859

上面的看不懂,换个格式来看看
>>> time.localtime()
(2007, 6, 2, 12, 47, 7, 5, 153, 0)

localtime返回tuple格式的时间,有一个和它类似的函数叫gmtime(),2个函数的差别是时区,gmtime()返回的是0时区的值,localtime返回的是当前时区的值。
>>> time.strftime( ISOTIMEFORMAT, time.localtime() )
'2007-06-02 12:54:29′

用上我们的时间格式定义了,使用strftime对时间做一个转换,如果取现在的时间,time.localtime() 可以不用。
>>> time.strftime( ISOTIMEFORMAT, time.localtime( time.time() ) )
'2007-06-02 12:54:31′
>>> time.strftime( ISOTIMEFORMAT, time.gmtime( time.time() ) )
'2007-06-02 04:55:02′

上面展示了gmtime和localtime的区别。 查看时区用
>>> time.timezone
-28800

上面的值是一个秒值,是当前时区和0时区相差的描述,-28800=-8*3600,即为东八区。 帖几个简单的函数
def ISOString2Time( s ):
  '''
  convert a ISO format time to second
  from:2006-04-12 16:46:40 to:23123123
  把一个时间转化为秒
  '''
  return time.strptime( s, ISOTIMEFORMAT )
def Time2ISOString( s ):
  '''
  convert second to a ISO format time
  from: 23123123 to: 2006-04-12 16:46:40
  把给定的秒转化为定义的格式
  '''
  return time.strftime( ISOTIMEFORMAT, time.localtime( float( s) ) )
def dateplustime( d, t ):
  '''
  d=2006-04-12 16:46:40
  t=2小时
  return 2006-04-12 18:46:40
  计算一个日期相差多少秒的日期,time2sec是另外一个函数,可以处理,3天,13分钟,10小时等字符串,回头再来写这个,需要结合正则表达式。
  '''
  return Time2ISOString( time.mktime( ISOString2Time( d ))+time2sec( t ) )
def dateMinDate( d1, d2 ):
  '''
  minus to iso format date,return seconds
  计算2个时间相差多少秒
  '''
  d1=ISOString2Time( d1 )
  d2=ISOString2Time( d2 )
  return time.mktime( d1 )-time.mktime( d2 )

[b]一、简介[/b] time模块提供各种操作时间的函数 说明:一般有两种表示时间的方式: 第一种是时间戳的方式(相对于1970.1.1 00:00:00以秒计算的偏移量),时间戳是惟一的 第二种以数组的形式表示即(struct_time),共有九个元素,分别表示,同一个时间戳的struct_time会因为时区不同而不同 year (four digits, e.g. 1998) month (1-12) day (1-31) hours (0-23) minutes (0-59) seconds (0-59) weekday (0-6, Monday is 0) Julian day (day in the year, 1-366) DST (Daylight Savings Time) flag (-1, 0 or 1) 是否是夏令时 If the DST flag is 0, the time is given in the regular time zone; if it is 1, the time is given in the DST time zone; if it is -1, mktime() should guess based on the date and time. 百度关于夏令时介绍:http://baike.baidu.com/view/100246.htm UTC介绍可参考下文中关于UTC的介绍:http://www.1sucai.cn/article/40758.htm [b]二、函数介绍 [/b] 1.[code]asctime()[/code] asctime([tuple]) -> string 将一个struct_time(默认为当时时间),转换成字符串 Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'. When the time tuple is not present, current time as returned by localtime() is used. 2.[code]clock()[/code] clock() -> floating point number 该函数有两个功能, 在第一次调用的时候,返回的是程序运行的实际时间; 以第二次之后的调用,返回的是自第一次调用后,到这次调用的时间间隔 示例:
import time
if __name__ == '__main__':
  time.sleep(1)
  print "clock1:%s" % time.clock()
  time.sleep(1)
  print "clock2:%s" % time.clock()
  time.sleep(1)
  print "clock3:%s" % time.clock()

输出:
clock1:3.35238137808e-006
clock2:1.00004944763
clock3:2.00012040636

其中第一个clock输出的是程序运行时间 第二、三个clock输出的都是与第一个clock的时间间隔 3.[code]sleep(...)[/code] sleep(seconds) 线程推迟指定的时间运行,经过测试,单位为秒,但是在帮助文档中有以下这样一句话,这关是看不懂 “The argument may be a floating point number for subsecond precision.” 4.[code]ctime(...)[/code] ctime(seconds) -> string 将一个时间戳(默认为当前时间)转换成一个时间字符串 例如:
time.ctime()

输出为:'Sat Mar 28 22:24:24 2009' 5.[code]gmtime(...)[/code] gmtime([seconds]) -> (tm_year, tm_mon, tm_day, tm_hour, tm_min,tm_sec, tm_wday, tm_yday, tm_isdst) 将一个时间戳转换成一个UTC时区(0时区)的struct_time,如果seconds参数未输入,则以当前时间为转换标准 6.[code]localtime(...)[/code] localtime([seconds]) -> (tm_year,tm_mon,tm_day,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst) 将一个时间戳转换成一个当前时区的struct_time,如果seconds参数未输入,则以当前时间为转换标准 7.[code]mktime(...)[/code] mktime(tuple) -> floating point number 将一个以struct_time转换为时间戳 8.[code]strftime(...)[/code] strftime(format[, tuple]) -> string 将指定的struct_time(默认为当前时间),根据指定的格式化字符串输出 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 当前时区的名称 %% %号本身 9.[code]strptime(...)[/code] strptime(string, format) -> struct_time 将时间字符串根据指定的格式化符转换成数组形式的时间 例如: 2009-03-20 11:45:39  对应的格式化字符串为:%Y-%m-%d %H:%M:%S Sat Mar 28 22:24:24 2009 对应的格式化字符串为:%a %b %d %H:%M:%S %Y 10.[code]time(...)[/code] time() -> floating point number 返回当前时间的时间戳 [b]三、疑点[/b] 1.夏令时 在struct_time中,夏令时好像没有用,例如
a = (2009, 6, 28, 23, 8, 34, 5, 87, 1)
b = (2009, 6, 28, 23, 8, 34, 5, 87, 0)

a和b分别表示的是夏令时和标准时间,它们之间转换为时间戳应该相关3600,但是转换后输出都为646585714.0 [b]四、小应用[/b] [b]1.python获取当前时间[/b] [code]time.time()[/code] 获取当前时间戳 [code]time.localtime()[/code] 当前时间的struct_time形式 [code]time.ctime()[/code] 当前时间的字符串形式 [b]2.python格式化字符串[/b] 格式化成2009-03-20 11:45:39形式
time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

格式化成Sat Mar 28 22:24:24 2009形式
time.strftime("%a %b %d %H:%M:%S %Y", time.localtime())

[b]3.将格式字符串转换为时间戳[/b]
a = "Sat Mar 28 22:24:24 2009"
b = time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y"))

[b]PS:本站还提供了几款关于Unix时间戳转换及日期在线工具,非常实用,提供给大家参考:[/b] [b]Unix时间戳(timestamp)转换工具: [/b][url=http://tools.jb51.net/code/unixtime]http://tools.jb51.net/code/unixtime[/url] [b]在线日期/天数计算器: [/b][url=http://tools.jb51.net/jisuanqi/date_jisuanqi]http://tools.jb51.net/jisuanqi/date_jisuanqi[/url] [b]在线万年历日历: [/b][url=http://tools.jb51.net/bianmin/wannianli]http://tools.jb51.net/bianmin/wannianli[/url] [b]在线阴历/阳历转换工具: [/b][url=http://tools.jb51.net/bianmin/yinli2yangli]http://tools.jb51.net/bianmin/yinli2yangli[/url] 更多关于Python相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/647.htm]Python日期与时间操作技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/809.htm]Python URL操作技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/645.htm]Python图片操作技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/663.htm]Python数据结构与算法教程[/url]》、《[url=http://www.1sucai.cn/Special/648.htm]Python Socket编程技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/642.htm]Python函数使用技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/636.htm]Python字符串操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/520.htm]Python入门与进阶经典教程[/url]》及《[url=http://www.1sucai.cn/Special/516.htm]Python文件与目录操作技巧汇总[/url]》 希望本文所述对大家Python程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部