python datetime offset-aware与offset-navie相互转换

2016年11月13日 16:20:43

阅读数:2393

有时,我们使用python 的datetime模块比较两个时间的前后关系时,会出现报错:

TypeError: can't compare offset-naive and offset-aware datetimes

这是因为两个时间不属于同一类型,offset-naive是不含时区的类型,而offset-aware是有时区类型,两者自然不能比较。

我们可以通过判断datetime对象的tzinfo属性,来获悉他是何种类型

  1.  
    In [17]: import datetime
  2.  
     
  3.  
    In [18]: import pytz
  4.  
     
  5.  
    In [19]: now=datetime.datetime.now()
  6.  
     
  7.  
    In [20]: now
  8.  
    Out[20]: datetime.datetime(2016, 11, 13, 16, 23, 37, 488000)
  9.  
     
  10.  
    In [21]: now.tzinfo==None
  11.  
    Out[21]: True
  12.  
     

上述代码中now就是一个offset-naive型,就是一个不含时区的datetime。下面代码可以把它转换为一个含时区的offset-aware型:

  1.  
    In [22]: now=now.replace(tzinfo=pytz.timezone('UTC'))
  2.  
     
  3.  
    In [23]: now
  4.  
    Out[23]: datetime.datetime(2016, 11, 13, 16, 23, 37, 488000, tzinfo=<UTC>)
  5.  
     
  6.  
    In [24]: now.tzinfo
  7.  
    Out[24]: <UTC>

同样的,将一个offset-aware型转换为offset-naive型:

  1.  
    In [25]: now=now.replace(tzinfo=None)
  2.  
     
  3.  
    In [26]: now
  4.  
    Out[26]: datetime.datetime(2016, 11, 13, 16, 23, 37, 488000)

python datetime offset-aware与offset-navie相互转换的更多相关文章

  1. python datetime和unix时间戳之间相互转换

                                python datetime和unix时间戳之间相互转换 1.代码:    import time    import datetime # ...

  2. OFFSET约束(OFFSET IN 和OFFSET OUT)

    OFFSET 的意思是偏移.对于同步时序电路来说,数据和时钟之间的偏移量是必须要关注的.OFFSET IN和OUT分别对应的是输入和输出FPGA数据和时钟之间的偏移关系,本文将分析这一种关系.阅读本文 ...

  3. Django model '__week_day'与python datetime的weekday()

    上周出了个bug,按星期几查询数据的时候,发现查到的数据与显示的星期几并不相符,后来发现代码中按星期几查询,有的地方用的是Django QuerySet提供的'__week_day',有的地方用的是p ...

  4. python datetime 时间日期处理小结

    python datetime 时间日期处理小结 转载请注明出处:http://hi.baidu.com/leejun_2005/blog/item/47f340f1a85b5cb3a50f5232. ...

  5. python datetime笔记

    python datetime笔记 http://mint-green.diandian.com/post/2011-09-09/4892024 获取当前时间,并通过字符串输出. 格式为:%Y-%m- ...

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

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

  7. Python datetime 格式化字符串:strftime()

    Python datetime 格式化字符串:strftime()   Python 的datetime模块 其实就是date和time 模块的结合, 常见的属性方法都比较常用 比如: datetim ...

  8. Python datetime 转 JSON

    Python datetime 转 JSON Python 中将 datetime 转换为 JSON 类型,在使用 Django 时遇到的问题. 环境: Python2.7 代码: import js ...

  9. Python datetime.md

    datetime datetime模块包含了一些用于时间解析.格式化.计算的函数. Times 时间值由time类来表示, Times有小时, 分, 秒和微秒属性. 以及包含时区信息. 初始化time ...

随机推荐

  1. nginx的安装及简单负载均衡配置

    Nginx 是一个很强大的高性能Web和反向代理服务器,它具有很多非常优越的特性.本人目前所在公司也使用nginx,主要用来做负载均衡服务器.同时也可以作为邮件代理服务器. 1. nginx的安装.本 ...

  2. java读取指定package下的所有class

     JAVA如何扫描一个包下面的所有类,并加载到内存中去? spring中有一个<context:component-scan base-package="com.controller& ...

  3. JavaScript 编写随机四位数验证码(大小写字母和数字)

    1.JavaScript编写随机四位数验证码,用到的知识点为: a.Math对象的随机数:Math.random() b.Math对象的取整    :Math.floor() c.处理所需要的下标个数 ...

  4. WINCC runtime连接SIMOTION simulator SIMOSIM

    测试使用的软件版本 TIA Portal V14sp1 Windows7 sp1 (professional) Scout 5.1(integrated in TIA 集成项目) VMware wor ...

  5. tomcat7 的The Apache Tomcat Native library which allows optimal performance 的解决

    1.        用Myeclipse启动tomcat7启动时可能会收到下面的信息: 七月 24, 2014 10:13:30 上午 org.apache.catalina.core.AprLife ...

  6. java 通过接口在后台管理器中生成数据

    需求:测试人员在后台批量添加数据很麻烦,特别是针对一款商品配置了英语,还需要手动添加法语.俄语.阿拉伯语,很麻烦,但是因为没有项目组配合,做个小工具批量生成数据就只有自己去研究了 第一步:通过抓包工具 ...

  7. 【转】Xcode真机测试could not find developer disk image解决方法

    在使用Xcode进行真机调试的时候,有时根据真机的系统不同,会出现could not find developer disk image 错误,这是由于真机系统过高或者过低,Xcode中没有匹配的配置 ...

  8. [ difflib] simple1.py

    #!/usr/bin/env python # _*_ coding:utf-8 _*_ import difflib text1 = """text1: # 定义字符串 ...

  9. 创建Android环境并且安装cordova

    需要eclipse.Andriod SDK.java.Apache ant.Node.js.Genymotion 目录链接: 1.安装adt-eclipse 2.安装JAVA 3.安装Apache a ...

  10. xml中${}的使用含义(美元符号大括号,以Spring、ibatis、mybatis为例)

    项目中,经常会在xml中看到这样的写法: <properties resource="properties/database.properties"/> <dat ...