推荐学习书目
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
282.71D
540.54D
V2EX  ›  Python

Python 中获得今天的 Unix Timestamp

  •  
  •   Livid ·
    PRO
    · Mar 7, 2011 · 8701 views
    This topic created in 5567 days ago, the information mentioned may be changed or developed.
    >>> import datetime
    >>> import time
    >>> int(time.mktime(datetime.date.today().timetuple()))
    1299427200

    还有更简单的办法么?
    6 replies    1970-01-01 08:00:00 +08:00
    GordianZ
        1
    GordianZ  
       Mar 8, 2011
    import time
    print int(time.time())
    GordianZ
        2
    GordianZ  
       Mar 8, 2011
    @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
    PRO
       Mar 8, 2011
    抱歉我没有表述清楚。

    是希望获得今天 0 点时候的 timestamp。
    keakon
        4
    keakon  
       Mar 8, 2011 via iPhone
    貌似用calendar.timegm更好,mktime会返回浮点数,而且时区可能有问题。不过也没什么能简化的。
    diamondtin
        5
    diamondtin  
       Mar 8, 2011
    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
    PRO
       Mar 8, 2011
    @keakon 嗯,确实应该在服务器端保存 UTC 时间。

    感谢!!!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1046 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 22:55 · PVG 06:55 · LAX 15:55 · JFK 18:55
    ♥ Do have faith in what you're doing.