V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
cevincheung
V2EX  ›  Python

Python3 怎么格式化时间(时区问题)

  •  
  •   cevincheung ·
    cevin · 2017-09-20 19:54:07 +08:00 · 3165 次点击
    这是一个创建于 2408 天前的主题,其中的信息可能已经有所发展或是发生改变。
    from datetime import datetime
    from pytz import timezone
    
    tzc = timezone('PRC')
    tzu = timezone('US/Hawaii')
    
    time = datetime.utcnow()
    
    print('C:'+time.replace(tzinfo=tzc).astimezone(tz=tzc).strftime('%H'))
    print('U:'+time.replace(tzinfo=tzu).astimezone(tz=tzu).strftime('%H'))
    

    为毛两个输出是一样的?

    15 条回复    2017-09-21 13:09:38 +08:00
    petelin
        1
    petelin  
       2017-09-20 20:41:51 +08:00
    In [24]: from datetime import datetime
    ...: from pytz import timezone
    ...: import pytz
    ...: tzc = timezone('PRC')
    ...: tzu = timezone('US/Hawaii')
    ...:
    ...: time = datetime.utcnow()
    ...:
    ...: print('C:'+time.replace(tzinfo=tzc).astimezone(tz=pytz.utc).strftime('%H'))
    ...: print('U:'+time.replace(tzinfo=tzu).astimezone(tz=pytz.utc).strftime('%H'))
    ...:
    C:04
    U:23
    petelin
        2
    petelin  
       2017-09-20 20:43:52 +08:00
    时间的比较应该在同一个时区下. 还有 replace 有坑, 使用 tzc.localize 更好
    cevincheung
        3
    cevincheung  
    OP
       2017-09-20 21:34:54 +08:00
    @petelin #2 关键是 python 输出个时间要这么麻烦吗?
    cevincheung
        4
    cevincheung  
    OP
       2017-09-20 21:36:16 +08:00
    @petelin #1 py3.5.2 表示两个输出一毛一样
    cevincheung
        5
    cevincheung  
    OP
       2017-09-20 21:39:22 +08:00
    @petelin #1
    NoAnyLove
        6
    NoAnyLove  
       2017-09-20 22:04:38 +08:00   ❤️ 1
    Python 本身的时区支持好像有问题,我之前遇到这个问题是直接用的第三方库,pendulum,https://github.com/sdispater/pendulum。简单,好用,

    ```
    pendulum.now('Asia/Shanghai')
    NoAnyLove
        7
    NoAnyLove  
       2017-09-20 22:09:53 +08:00
    Orz,不知道怎么按到 Enter 了,重新来。

    ```
    import pendulum
    t1=pendulum.now('Asia/Shanghai')
    t2=t1.in_timezone('US/Hawaii')
    # 下面两个的输出绝对不一样
    t1.strftime("%H")
    t2.strftime("%H")
    ```
    GTim
        8
    GTim  
       2017-09-20 22:32:20 +08:00
    @cevincheung 因为你理解错了 time.replace 的意思了。 可以这样用

    ```python
    from datetime import datetime
    from pytz import timezone

    tzc = timezone('PRC')
    tzu = timezone('US/Hawaii')

    now = datetime.now(pytz.utc)

    now.astimezone(tzc).strftime('%H')
    now.astimezone(tzu).strftime('%H')
    ```
    cevincheung
        9
    cevincheung  
    OP
       2017-09-21 05:01:25 +08:00
    @NoAnyLove #7 所以说不是我理解有问题对不对
    @GTim #8 我试试
    cevincheung
        10
    cevincheung  
    OP
       2017-09-21 05:03:44 +08:00
    @GTim #8 这样是 ok 了,好吧。replace 不能这么用。

    但是好蛋疼啊。数据库里存的是 utf 时间戳,格式化显示的时候要根据用户浏览器、个人设置重新格式化,看来要弄个全局的 utils.timestamp_format 了
    congminghaoxue92
        11
    congminghaoxue92  
       2017-09-21 09:08:21 +08:00   ❤️ 1
    arrow 大法好,建议用 arrow
    http://arrow.readthedocs.io/en/latest/
    blless
        12
    blless  
       2017-09-21 09:14:43 +08:00
    @GTim pytz 的 Asia/Shanghai 时区好像实际上会差 6 分钟。我现在都改成 dateutil.tz.gettz
    cevincheung
        13
    cevincheung  
    OP
       2017-09-21 11:01:03 +08:00
    @blless #12 所以时区处理没有官方包?
    ijophy
        14
    ijophy  
       2017-09-21 13:00:55 +08:00
    翻了翻文档,给下我的实现,py3 下可以
    from datetime import datetime
    from datetime import timezone
    from datetime import timedelta

    time_format = "%Y-%m-%d %H:%M:%S"
    bj_offset = timezone(timedelta(hours=8))
    bj_datetime = datetime.now(bj_offset)
    time = bj_datetime.strftime(time_format)
    cevincheung
        15
    cevincheung  
    OP
       2017-09-21 13:09:38 +08:00
    @ijophy #14 就没有一个全局设置么?

    比如 php date_default_timezone_set('Asia/Chongqing') 之后代码 date 全是这个时区的时间
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1155 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 23:05 · PVG 07:05 · LAX 16:05 · JFK 19:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.