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
rrfeng
V2EX  ›  Python

有人吐槽 Python 的时间处理模块吗?

  •  2
     
  •   rrfeng · 2017-03-09 18:20:10 +08:00 · 3307 次点击
    这是一个创建于 2598 天前的主题,其中的信息可能已经有所发展或是发生改变。
    >>> dateutil.parser.parse("2017-03-09T09:32Z")
    datetime.datetime(2017, 3, 9, 9, 32, tzinfo=tzutc())
    >>> t1=dateutil.parser.parse("2017-03-09T09:32Z")
    >>> t2=dateutil.parser.parse("2017-03-09T09:32Z").astimezone(dateutil.tz.tzstr("GMT+0800"))
    >>> t1-t2
    datetime.timedelta(0)
    >>>
    >>> t1
    datetime.datetime(2017, 3, 9, 9, 32, tzinfo=tzutc())
    >>> t2
    datetime.datetime(2017, 3, 9, 17, 32, tzinfo=tzstr('GMT+0800'))
    >>> t1-t2
    datetime.timedelta(0)
    >>> t2.strftime("%s")
    '1489051920'
    >>> t1.strftime("%s")
    '1489023120'
    >>>
    
    • datetime parse 的时候直接不管时区
    • 时间相等的 datetime 对象生成的时间戳居然不一样

    为了方便小白理解?遇到好几个人觉得不同时区的时间戳不一样了……说不定是 Python 的锅

    13 条回复    2017-03-12 14:40:15 +08:00
    SpicyCat
        1
    SpicyCat  
       2017-03-09 18:27:35 +08:00   ❤️ 1
    不要用原生的时间处理模块,用第三方的库。比较有名的是 arrow
    https://github.com/crsmithdev/arrow
    neo1218
        2
    neo1218  
       2017-03-09 18:32:50 +08:00   ❤️ 1
    rrfeng
        3
    rrfeng  
    OP
       2017-03-09 18:47:07 +08:00 via Android
    感谢...看来看去还是 go 的比较舒服
    billlee
        4
    billlee  
       2017-03-09 19:47:31 +08:00   ❤️ 1
    自己不看文档还怪 python 咯?
    datetime.datetime 传的参数是本地时间,输出 timestamp 是是要转换到 UTC 的,你指定的时区不同,转换到 UTC 后当然不一样。
    parse 的时候 2017-03-09T09:32Z 里面的 Z 就是 0 区的意思,你拿两个相同的时间相减,当然是等于 0 了。
    rrfeng
        5
    rrfeng  
    OP
       2017-03-09 20:13:00 +08:00
    @billlee 好一个 `输出 timestamp 是是要转换到 UTC 的`
    Muninn
        6
    Muninn  
       2017-03-09 20:18:02 +08:00
    go 的时间模块也被吐槽的不行啊
    herozhang
        7
    herozhang  
       2017-03-09 20:59:14 +08:00
    推荐 pip install arrow
    享受更好的时间处理
    PythonAnswer
        8
    PythonAnswer  
       2017-03-09 21:08:09 +08:00 via Android
    自带电池,只能让你享受一定程度的方便。(总比没电池好)

    想更爽地使用,当然是插上电源,接通更人性化更现代的库, arrow, requests , jinja2 , lxml 。。。

    难道你还用 urllib, str.format, etree ?
    laoyur
        9
    laoyur  
       2017-03-09 21:08:20 +08:00
    Python 渣表示,你贴出的示例,哪一条不好理解呢?我看着都没问题啊
    最后一个 strftime ,你用的%s 小写 s 是啥意思呢, https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior 官方文档中并没有小写 s 的 format
    jarlyyn
        10
    jarlyyn  
       2017-03-10 01:09:54 +08:00
    @rrfeng

    go 的 time 真的舒服嘛……
    rrfeng
        11
    rrfeng  
    OP
       2017-03-10 07:45:13 +08:00 via Android
    @jarlyyn 起码比较容易理解...
    zhihhh
        12
    zhihhh  
       2017-03-10 13:15:34 +08:00
    说 go 比 python 自带电池爽我也是没明白。。
    lalalakakaka
        13
    lalalakakaka  
       2017-03-12 14:40:15 +08:00
    不错了,
    我在用 python 写时间时也郁闷过,相似功能的库太多了,一点都不正交,也不 pythonic
    结果偶尔去写了个 js ,发现 js 居然没有内置时间格式化的函数,还得自己手撸,这才惨呢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   877 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 21:31 · PVG 05:31 · LAX 14:31 · JFK 17:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.