python time模块详解

转自:http://blog.csdn.net/kiki113/article/details/4033017

python 的内嵌time模板翻译及说明
  
一、简介

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介绍:http://wenda.tianya.cn/wenda/thread?tid=283921a9da7c5aef&clk=wttpcts
    
二、函数介绍
1.asctime()
  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.clock()
  clock() -> floating point number
  该函数有两个功能,
  在第一次调用的时候,返回的是程序运行的实际时间;
  以第二次之后的调用,返回的是自第一次调用后,到这次调用的时间间隔
  
  示例:

  1. import time
  2. if __name__ == '__main__':
  3. time.sleep(1)
  4. print "clock1:%s" % time.clock()
  5. time.sleep(1)
  6. print "clock2:%s" % time.clock()
  7. time.sleep(1)
  8. print "clock3:%s" % time.clock()

输出:
  clock1:3.35238137808e-006
  clock2:1.00004944763
  clock3:2.00012040636
  其中第一个clock输出的是程序运行时间
  第二、三个clock输出的都是与第一个clock的时间间隔
  
3.sleep(...)
  sleep(seconds)
  线程推迟指定的时间运行,经过测试,单位为秒,但是在帮助文档中有以下这样一句话,这关是看不懂
  “The argument may be a floating point number for subsecond precision.”

4.ctime(...)
  ctime(seconds) -> string
  将一个时间戳(默认为当前时间)转换成一个时间字符串
  例如:
  time.ctime()
  输出为:'Sat Mar 28 22:24:24 2009'
        
5.gmtime(...)
  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.localtime(...)
  localtime([seconds]) -> (tm_year,tm_mon,tm_day,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst)
  将一个时间戳转换成一个当前时区的struct_time,如果seconds参数未输入,则以当前时间为转换标准
  
        
7.mktime(...)
  mktime(tuple) -> floating point number
  将一个以struct_time转换为时间戳
        
8.strftime(...)
  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.strptime(...)
  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.time(...)
   time() -> floating point number
   返回当前时间的时间戳

三、疑点
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
  
四、小应用
1.python获取当前时间
   time.time() 获取当前时间戳
   time.localtime() 当前时间的struct_time形式
   time.ctime() 当前时间的字符串形式
   
2.python格式化字符串  
  格式化成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()) 
  
3.将格式字符串转换为时间戳
  a = "Sat Mar 28 22:24:24 2009"
  b = time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y"))

python time模块详解的更多相关文章

  1. python docopt模块详解

    python docopt模块详解 docopt 本质上是在 Python 中引入了一种针对命令行参数的形式语言,在代码的最开头使用 """ ""&q ...

  2. (转)python collections模块详解

    python collections模块详解 原文:http://www.cnblogs.com/dahu-daqing/p/7040490.html 1.模块简介 collections包含了一些特 ...

  3. python pathlib模块详解

    python pathlib模块详解    

  4. Python Fabric模块详解

    Python Fabric模块详解 什么是Fabric? 简单介绍一下: ​ Fabric是一个Python的库和命令行工具,用来提高基于SSH的应用部署和系统管理效率. 再具体点介绍一下,Fabri ...

  5. python time 模块详解

    Python中time模块详解 发表于2011年5月5日 12:58 a.m.    位于分类我爱Python 在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括: ...

  6. python常用模块详解

    python常用模块详解 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用p ...

  7. python os模块详解

    一.Python os模块(Linux环境) 1.1 执行shell命令 os.system('cmd') 执行命令不保存结果 os.popen('command') 执行后返回结果,使用.read( ...

  8. Python ZipFile模块详解(转)

    Python zipfile模块用来做zip格式编码的压缩和解压缩的,zipfile里有两个非常重要的class, 分别是ZipFile和ZipInfo, 在绝大多数的情况下,我们只需要使用这两个cl ...

  9. python常用模块详解(一)

    一.简介 模块是一个保存了Python代码的文件.模块能定义函数,类和变量.模块里也能包含可执行的代码 模块分为三种: 自定义模块 内置标准模块 开源模块(第三方) 自定义模块: 模块导入 impor ...

随机推荐

  1. bzoj3389:[Usaco2004 Dec]Cleaning Shifts安排值班

    思路:可以贪心,也可以最短路. 贪心写法:因为在保证合法的前提下,我们选择的区间一定要右端点尽量靠后才行,于是我们每次就选择一个合法的并且右端点最靠后的区间就好了(如果没有合法的输出-1即可).时间复 ...

  2. (POJ 2318)TOYS 向量叉积

    题目链接:http://poj.org/problem?id=2318 #include<stdio.h> #include<cstdlib> #include<cstr ...

  3. AspectJ的简单使用

    aspectj是一款优秀的面向切面的编程框架,下面就简单介绍一下入门教程吧: 1.官网下载AspectJ的jar包,我这里下的是最新版本1.8.7的. 2.因为AspectJ.jar 是一个可执行的j ...

  4. C#定时器

    在C#里关于定时器类就有3个 1.定义在System.Windows.Forms里 2.定义在System.Threading.Timer类里 3.定义在System.Timers.Timer类里 S ...

  5. 一种C# TCP异步编程中遇到的问题

    最近在维护公司的一个socket服务端工具,该工具主要是提供两个socket server服务,对两端连接的程序进行数据的透明转发. 程序运行期间,遇到一个问题,程序的一端是GPRS设备,众所周知,G ...

  6. CentOS 安装配置memcached (转)

    1.先下载memcached 和libevent. libevent 最新的稳定版: wget http://monkey.org/~provos/libevent-1.4.14b-stable.ta ...

  7. dbt

    Procedure Relocate(s : state; b : base_index) { Move base for state s to a new place beginning at b ...

  8. JavaScript加强之自定义callback示例

    callback回调函数在本文以自定义的方式出现.   html:  <select id="select">  <option value="111& ...

  9. 拓展,Fibonacci螺旋

    #该程序由023递归这课中的fibonacci数列递归写法修改而成 #在写的过程中发现,如果要正确引导用户的每一次输入,写的代码比主程序还要多 #当然,为了使程序在用户交互过程中显得更加友好,提供错误 ...

  10. 1037. Magic Coupon (25)

    #include<iostream> #include<vector> #include<stdio.h> #include<algorithm> us ...