在Python中,和时间处理相关的模块有time,datatime,calendar(不常用)三个。

UTCC(Coordinated Universal Time,世界协调时)亦即格林威治天文时间,世界标准时间。在中国为UTC+8。DST(Daylight Saving Time)即夏令时。

在Python中时间的表示方式分三种:

  1. 时间戳(timestamp):时间戳表示从1970年1月1日00:00:00开始按秒计算的偏移量。我们运行“type(time.time())”,返回的是float类型。

    1. >>> time.time()
    2. 1521948761.733449
    3. >>> type(time.time())
    4. <class 'float'>
  2. 格式化的时间字符串:2014-11-11 11:11,即time.strftime('%Y-%m-%d')
    1. >>> import time
    2. >>> time.strftime('%Y-%m-%d')
    3. '2018-03-25'
  3. 元组(struct_time)共九个元素,返回struct_time的函数主要有gmtime(),localtime(),strptime()
    1. >>> import time
    2. >>> time.localtime()
    3. time.struct_time(
    4. tm_year=2018, # 年
    5. tm_mon=2, # 月
    6. tm_mday=26, # 日
    7. tm_hour=2, # 时
    8. tm_min=47, # 分
    9. tm_sec=49, # 秒
    10. tm_wday=0, # 星期几(0代表星期日)
    11. tm_yday=57, # 一年中第几天
    12. tm_isdst=0) # 是否夏令时,默认是-1
time模块

      

time常用方法:

  1. >>> time.time() # 返回当前时间的时间戳(按秒计算的浮点数),从1970到现在的秒数
  2. 1521948276.9173918
  3.  
  4. >>> time.localtime() # 打印本地时间(操作系统时间)
  5. time.struct_time(tm_year=2018, tm_mon=3, tm_mday=25, tm_hour=11, tm_min=22, tm_sec=27, tm_wday=6, tm_yday=84, tm_isdst=0)
  6.  
  7. >>> time.gmtime() # 打印格林威治时间(比北京时间晚8个小时)
  8. time.struct_time(tm_year=2018, tm_mon=3, tm_mday=25, tm_hour=3, tm_min=55, tm_sec=55, tm_wday=6, tm_yday=84, tm_isdst=0)
  9.  
  10. time.mktime() # 把一个时间对象转化为时间戳
  11. >>> time.mktime(time.localtime())
  12. 1521950235.0
  13.  
  14. time.sleep() # 线程推迟指定的时间运行,单位为秒
  15.  
  16. time.asctime() # 把一个表示时间的元祖或struct time转换表示形式
  17. >>> time.asctime() # 如果没有参数将time.localtime作为参数传入
  18. 'Mon Feb 26 10:59:10 2018'
  19.  
  20. time.ctime() # 把一个时间戳转化为time_asctime()形式,默认以time.time()为参数
  21. >>> time.ctime() # 相当于time.asctime(time.localtime(secs))
  22. 'Mon Feb 26 11:06:29 2018'
  23. >>> time.ctime(-231334422) # 参数可以为负
  24. 'Sun Sep 2 20:26:18 1962't
  25.  
  26. time.strftime(format,a) # 把一个代表时间的元祖或struct time转化为格式化的时间字符串
  27. >>> time.strftime('%Y-%m-%d')
  28. '2018-02-26'
  29. >>> time.strftime('%Y-%m-%d %H:%M:%S')
  30. '2018-02-26 11:19:47'
  31. >>> time.strftime('%Y-%m-%d %H:%M:%S',a)
  32. '2018-02-26 10:40:18'
  33. >>> time.strftime('%Y-%m-%d %H:%M:%S %A',a)
  34. '2018-02-26 10:40:18 Monday'
  35. >>> time.strftime('%Y-%m-%d %H:%M:%S %p')
  36. '2018-02-26 11:21:44 AM'
  37. >>> time.strftime('%Y-%m-%d %H:%M:%S %U') # 今年的第几周
  38. '2018-02-26 11:21:55 08'
  39. >>> time.strftime('%Y-%m-%d %H:%M:%S %w') # 0-6,星期日是0
  40. '2018-02-26 11:24:56 1'
  41.  
  42. time.strptime('string', format) # 把一个格式化时间字符串转化为struct_time,stftime的逆操作
  43. >>> s = time.strftime('%Y %m-%d %H:%M:%S')
  44. >>> s
  45. '2018 02-26 11:42:16'
  46. >>> time.strptime(s,'%Y %m-%d %H:%M:%S')
  47. time.struct_time(tm_year=2018, tm_mon=2, tm_mday=26, tm_hour=11, tm_min=42, \
  48. tm_sec=16, tm_wday=0, tm_yday=57, tm_isdst=-1)
datetime模块

  datetime模块相比time模块,datetime模块的接口更直观,更容易调用。

  datetime模块定义了下面几个类:

    1、datetime.date:表示日期的类,常用的属性有year,month,day

    2、datetime.time:表示时间的类,常用的属性有hour,minute,second,microsecond

    3、datetime.datetime:表示日期时间

    4、datetime.timedelta:表示时间间隔,即两个时间点之间的长度

    5、datetime.tzinfo:与时区相关信息

  1. >>> a = datetime.datetime.now()
  2. datetime.datetime(2018, 2, 26, 12, 8, 18, 805166)
  3.  
  4. >>> import time
  5. >>> d2 = datetime.date.fromtimestamp(time.time()) # 时间戳转化为年月日
  6. datetime.date(2018, 2, 26)
  7.  
  8. >>> d2.timetuple() # 转化为时间对象
  9. time.struct_time(tm_year=2018, tm_mon=2, tm_mday=26, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=57, tm_isdst=-1)
  10.  
  11. # 时间运算
  12. >>> a = datetime.datetime.now()
  13. >>> t1 = datetime.timedelta(1)
  14. >>> a - t1
  15. datetime.datetime(2018, 2, 25, 13, 37, 13, 812339)
  16.  
  17. >>> a - datetime.timedelta(days=1)
  18. datetime.datetime(2018, 2, 25, 13, 37, 13, 812339)
  19. >>> a - datetime.timedelta(days=3)
  20. datetime.datetime(2018, 2, 23, 13, 37, 13, 812339)
  21. # 还支持hours、minutes、secends的运算
  22. >>> a - datetime.timedelta(hours=3)
  23. datetime.datetime(2018, 2, 26, 10, 37, 13, 812339)
  24. >>> a + datetime.timedelta(hours=3)
  25. datetime.datetime(2018, 2, 26, 16, 37, 13, 812339)
  26.  
  27. # 时间替换
  28. >>> n = datetime.datetime.now()
  29. >>> n.replace(year=2016)
  30. datetime.datetime(2016, 2, 26, 13, 45, 26, 863002)
  31. >>> n.replace(year=2017,month=4)
  32. datetime.datetime(2017, 4, 26, 13, 45, 26, 863002)
  33. >>> n.replace(year=2017,month=4,day=13)
  34. datetime.datetime(2017, 4, 13, 13, 45, 26, 863002)
  35.  
  36. # replace实现时间计算
  37. >>> expire_year = time.localtime()[0] # 当前年份
  38. >>> expire_date = datetime.datetime.now().replace(year=expire_year+10).strftime('%Y-%m-%d')
  39. >>> expire_date
  40. '2028-03-25'

time&datetime模块的更多相关文章

  1. python中datetime模块

    Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供 的接口与C标准库time.h基本一致.相比于time模块 ...

  2. python datetime模块参数详解

    Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块,它提供 的接口与C标准库time.h基本一致.相比于time模块,datetime模块的接 ...

  3. Python处理时间 time && datetime 模块

    Python处理时间 time  &&  datetime 模块 个人整理,获取时间方式: import datetime import time #获取当前时间:Thu Nov 03 ...

  4. python time模块和datetime模块详解

    一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共 ...

  5. python-Day5-深入正则表达式--冒泡排序-时间复杂度 --常用模块学习:自定义模块--random模块:随机验证码--time & datetime模块

    正则表达式   语法:             mport re #导入模块名 p = re.compile("^[0-9]") #生成要匹配的正则对象 , ^代表从开头匹配,[0 ...

  6. python datetime模块strptime/strptime format常见格式命令_施罗德_新浪博客

    python datetime模块strptime/strptime format常见格式命令_施罗德_新浪博客     python datetime模块strptime/strptime form ...

  7. Python datetime模块的datetime类

    datetime模块定义了下面这几个类: datetime.date:表示日期的类.常用的属性有year, month, day. datetime.time:表示时间的类.常用的属性有hour, m ...

  8. python处理时间--- datetime模块

    1   Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供的接口与C标准库time.h基本一致.相比于tim ...

  9. Python,datetime模块实例

    Python的标准模块datetime模块,在我们的工作中应用非常频繁,下面对datetime中常用的方法进行了总结和测试:对每一个方法都使用了单元测试框架Unittest来配合测试. 主要的类型有: ...

  10. python3 time模块与datetime模块

    time模块 在Python中,通常有这几种方式来表示时间:1)时间戳 2)格式化的时间字符串 3)元组(struct_time)共九个元素.由于Python的time模块实现主要调用C库,所以各个平 ...

随机推荐

  1. Java_Ant详解(转载)

    Java_Ant详解   1,什么是antant是构建工具2,什么是构建概念到处可查到,形象来说,你要把代码从某个地方拿来,编译,再拷贝到某个地方去等等操作,当然不仅与此,但是主要用来干这个3,ant ...

  2. golang包引用解析

    golang包引用解析 环境变量配置如下: GOROOT----[C:\Go] GOPATH----[F:\workspace\go_home] vs code配置如下: F:\workspace\g ...

  3. Centos7服务器启动jar包项目最佳方式

    jar后台运行:nohup java -jar xx.jar >/dev/null & 此处的“>/dev/null”作用是将终端输出信息输出到空洞中,即不保存输出信息,若要查看输 ...

  4. kibana安装汉化包

    kibana安装汉化包其实很简单!但要找到汉化包可能就很麻烦了.我这里提供了6.2的版本的汉化包!至于能不能在其他版本用,我就没试过了.但6.2的kibana本人亲测.没问题!!!! 下载——解压.这 ...

  5. UVA - 11996 可持久化Treap 维护Hash Ver.2

    这回总算是过了.. 4600ms+,服务器抖一抖又没了 对于极端卡时间的情况还是考虑屈服于Splay吧 #include<iostream> #include<algorithm&g ...

  6. 【总结】sqlmap-tamper编写小结

    目的:修改sqlmap中的tamper脚本来绕过代码对特定参数的过滤和转义 环境:win10.phpstudy2016.sqli-labs-master平台 工具:sqlmap.burpsuite 地 ...

  7. 二分函数 lower_bound()

    这篇博客说是STL源码.... https://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html 头文件 algorithm 1.lowe ...

  8. python中变量,常量

    1.变量 变量的作用:一个变化的值 把程序运算的中间结果临时存到内存里,以备后面的代码继续调用,这几个名字的学名就叫做“变量“ 查看变量在内存中的位置用id(name) 变量定义规则: 变量名只能是 ...

  9. PIE SDK打开Micaps数据

    1. 功能简介 Micaps数据是气象信息处理和天气预报制作中的一种气象数据格式.其包含多种气象信息产品(地面常规气象观测数据产品.高空常规气象观测数据产品等),目前Micaps数据被Micaps软件 ...

  10. mssql 死锁方面的一些资料。

    -- 1.查询死锁的进程,表名 select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) as tableNa ...