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

Python 中获得今天的 Unix Timestamp

  •  
  •   Livid · 2011-03-07 23:53:24 +08:00 · 7737 次点击
    这是一个创建于 4769 天前的主题,其中的信息可能已经有所发展或是发生改变。
    >>> import datetime
    >>> import time
    >>> int(time.mktime(datetime.date.today().timetuple()))
    1299427200

    还有更简单的办法么?
    6 条回复    1970-01-01 08:00:00 +08:00
    GordianZ
        1
    GordianZ  
    MOD
       2011-03-08 00:22:22 +08:00
    import time
    print int(time.time())
    GordianZ
        2
    GordianZ  
    MOD
       2011-03-08 00:22:57 +08:00
    @Livid oh, sry, didn't notice you need time for the day...
    Then probably yours is the easiest way already :(
    Livid
        3
    Livid  
    MOD
    OP
       2011-03-08 00:23:21 +08:00
    抱歉我没有表述清楚。

    是希望获得今天 0 点时候的 timestamp。
    keakon
        4
    keakon  
       2011-03-08 00:40:26 +08:00 via iPhone
    貌似用calendar.timegm更好,mktime会返回浮点数,而且时区可能有问题。不过也没什么能简化的。
    diamondtin
        5
    diamondtin  
       2011-03-08 17:39:55 +08:00
    def mid_night_timestamp():
    timestamp = int(time.time())
    if timestamp % 86400 == 57600: //57600 = 86400 + (60 * 60 * -8)
    return timestamp
    else:
    return timestamp / 86400 * 86400 + (60 * 60 * -8)

    This is faster :D
    不过仅在时区固定的时候有效,替换一下时区offset就可以了。如果在TimeZone aware的环境,需要用tzinfo的offset修改一下。
    Livid
        6
    Livid  
    MOD
    OP
       2011-03-08 18:14:50 +08:00
    @keakon 嗯,确实应该在服务器端保存 UTC 时间。

    感谢!!!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3059 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 12:50 · PVG 20:50 · LAX 05:50 · JFK 08:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.