ghong6003的个人空间 https://blog.eetop.cn/1430538 [收藏] [复制] [分享] [RSS]

空间首页 动态 记录 日志 相册 主题 分享 留言板 个人资料

日志

【转】python 之time模块详解

已有 1176 次阅读| 2018-3-24 21:47 |个人分类:Python|系统分类:芯片设计

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ************************************************************
# File Name   : genTimeString.py
# Author      : Hong Guo
# Description : This file used to gen time string.
# From        :
# ************************************************************

import sys
#import xlrd
#import xlwt
import csv
import os
import re
import traceback
from optparse import OptionParser

import time

def main():
    timeSeconds=time.time()                 # Get seconds of the now time 
    print(timeSeconds)
  
    timeTuple=time.localtime(timeSeconds)   # Get the time tuple of seconds.
    print(timeTuple)                        # time.struct_time(tm_year=2018, tm_mon=3, tm_mday=24, tm_hour=21, tm_min=27, tm_sec=57, tm_wday=5, tm_yday=83, tm_isdst=0)
    timeTupleOrg=tuple(timeTuple)           # (2018, 3, 24, 21, 27, 57, 5, 83, 0)
    yearIndex=timeTuple[0]                  #2018
    monthIndex=timeTuple[1]                 #03
    yearDict=timeTuple.tm_year              #2018
    monthDict=timeTuple.tm_mon              #03

    timeString=time.asctime(timeTuple)      # Get the string time frm tuple.
    print(timeString)

    timeFormat=time.strftime("%y_%m_%d_%H_%S_%M", time.localtime())  # Get the formated time string
    print(timeFormat)                        # '18_03_24_21_22_40'
    print(time.strftime("%Y-%m-%d %X", time.localtime()))  #'2011-05-05 16:37:06'

    timeOver=time.strptime('2011-05-05 16:37:06',"%Y-%m-%d %X") # Gen tuple from Format String to Tuple string
    print(timeOver)
    

if __name__ == '__main__':
    try:
        main()
    except (StandardError, ValueError):
        stackTrace = traceback.format_exc()
        sys.stdout.write(stackTrace)
        sys.exit(1) 

[From]->. https://www.cnblogs.com/qq78292959/archive/2013/03/22/2975786.html

在平常的代码中,我们常常需要与时间打交道。在Python中,与时间处理有关的模块就包括:time,datetime以及calendar。这篇文章,主要讲解time模块。

在开始之前,首先要说明这几点:

  1. 在Python中,通常有这几种方式来表示时间:1)时间戳 2)格式化的时间字符串 3)元组(struct_time)共九个元素。由于Python的time模块实现主要调用C库,所以各个平台可能有所不同。
  2. UTC(Coordinated Universal Time,世界协调时)亦即格林威治天文时间,世界标准时间。在中国为UTC+8。DST(Daylight Saving Time)即夏令时。
  3. 时间戳(timestamp)的方式:通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。我们运行“type(time.time())”,返回的是float类型。返回时间戳方式的函数主要有time(),clock()等。
  4. 元组(struct_time)方式:struct_time元组共有9个元素,返回struct_time的函数主要有gmtime(),localtime(),strptime()。下面列出这种方式元组中的几个元素:
索引(Index)属性(Attribute)值(Values)
0 tm_year(年) 比如2011 
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
6 tm_wday(weekday) 0 - 6(0表示周日)
7 tm_yday(一年中的第几天) 1 - 366
8 tm_isdst(是否是夏令时) 默认为-1


接着介绍time模块中常用的几个函数:

1)time.localtime([secs]):将一个时间戳转换为当前时区的struct_time。secs参数未提供,则以当前时间为准。

>>> time.localtime()
time.struct_time(tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=14, tm_min=14, tm_sec=50, tm_wday=3, tm_yday=125, tm_isdst=0)
>>> time.localtime(1304575584.1361799)
time.struct_time(tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=14, tm_min=6, tm_sec=24, tm_wday=3, tm_yday=125, tm_isdst=0)

2)time.gmtime([secs]):和localtime()方法类似,gmtime()方法是将一个时间戳转换为UTC时区(0时区)的struct_time。

>>>time.gmtime()
time.struct_time(tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=6, tm_min=19, tm_sec=48, tm_wday=3, tm_yday=125, tm_isdst=0)

3)time.time():返回当前时间的时间戳。

>>> time.time() 
1304575584.1361799

4)time.mktime(t):将一个struct_time转化为时间戳。

>>> time.mktime(time.localtime())
1304576839.0

5)time.sleep(secs):线程推迟指定的时间运行。单位为秒。

6)time.clock():这个需要注意,在不同的系统上含义不同。在UNIX系统上,它返回的是“进程时间”,它是用秒表示的浮点数(时间戳)。而在WINDOWS中,第一次调用,返回的是进程运行的实际时间。而第二次之后的调用是自第一次调用以后到现在的运行时间。(实际上是以WIN32上QueryPerformanceCounter()为基础,它比毫秒表示更为精确)

1
2
3
4
5
6
7
8
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的时间间隔

7)time.asctime([t]):把一个表示时间的元组或者struct_time表示为这种形式:'Sun Jun 20 23:21:05 1993'。如果没有参数,将会将time.localtime()作为参数传入。

>>> time.asctime()
'T


点赞

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 注册

  • 关注TA
  • 加好友
  • 联系TA
  • 0

    周排名
  • 0

    月排名
  • 0

    总排名
  • 1

    关注
  • 3

    粉丝
  • 0

    好友
  • 2

    获赞
  • 1

    评论
  • 1539

    访问数
关闭

站长推荐 上一条 /2 下一条

小黑屋| 关于我们| 联系我们| 在线咨询| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2024-4-19 19:37 , Processed in 0.028985 second(s), 8 queries , Gzip On, Redis On.

eetop公众号 创芯大讲堂 创芯人才网
返回顶部