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

关于 UTC 时间的整数值

  •  
  •   Livid · 2012-04-26 04:00:18 +08:00 · 3427 次点击
    这是一个创建于 4382 天前的主题,其中的信息可能已经有所发展或是发生改变。
    在大部分系统的大部分情况下,下面两句应该输出是一样的吧?

    int(time.time())

    int(calendar.timegm(datetime.datetime.utcnow().timetuple()))
    8 条回复    1970-01-01 08:00:00 +08:00
    HowardMei
        1
    HowardMei  
       2012-04-26 10:41:23 +08:00
    写过一个小模块,用来将 5d 2h 3mins 这种字符串转成us/ms为单位的精确timestamp,代码太丑陋,测试也不充分,就没开源。我记得以下写法可保证 windows 7 和 ubuntu 下,得到平台无关的us级timestamp,mac上应该和ubuntu一样,s级没有平台一致性问题:

    long(time.time() * 1000.0 * 1000.0 + (time.clock()*1000.0- \
    int(time.clock()*1000.0))*1000.00)

    不知道直接贴代码会不会很难看:
    from datetime import datetime
    from calendar import timegm

    def nowTimeStamp(unit='us'):
    """
    Returns a long integer representing UTC timestamp in 'us','ms','s'
    Generate identical outputs across platforms
    """
    # unit = _unify(unit) # used to unify unit strings, like 'sec' 'secs' 'seconds' 'second' to 's'
    if unit == 's':
    return long(time.time())
    elif unit == 'ms':
    return long(time.time() * 1000.0)
    elif unit == 'us':
    return long(time.time() * 1000.0 * 1000.0 + (time.clock()*1000.0- \
    int(time.clock()*1000.0))*1000.00)
    else:
    raise TypeError("Not supported timestamp unit:",unit)


    def getStamp(timeutc,unit='us'):
    """
    Convert UTC datetime to long integer timestamp in micro/mili.seconds
    usage: getStamp(utctime,unit='us') # unit could be 'us','ms','s'
    """
    if isinstance(timeutc, datetime):
    #unit=_unify(unit)
    if unit == 's':
    return long(timegm(timeutc.utctimetuple()))
    elif unit == 'ms':
    return long(timegm(timeutc.utctimetuple())*1000.0+ \
    timeutc.microsecond/1000.0)
    elif unit == 'us':
    return long(timegm(timeutc.utctimetuple())*1000000.0+ \
    timeutc.microsecond)
    else:
    raise TypeError("Not supported timestamp unit:"+unit)
    else:
    raise TypeError("Unknow timeutc type:",timeutc,".Use UTC time to retry.")
    HowardMei
        2
    HowardMei  
       2012-04-26 10:45:20 +08:00
    果然缩进全没了,有引入MarkDown Editor的计划么?
    xhacker
        3
    xhacker  
       2012-04-26 10:58:14 +08:00
    @HowardMei:可以用 Gist。
    HowardMei
        4
    HowardMei  
       2012-04-26 11:58:01 +08:00
    Kai
        5
    Kai  
    MOD
       2012-04-26 12:00:31 +08:00
    @HowardMei 直接贴网址就可以了 :)
    HowardMei
        6
    HowardMei  
       2012-04-26 12:06:01 +08:00
    这劲费得,像Stackoverflow那样有个编辑器提示/预览就好了:
    http://gist.github.com/2495445
    qiuai
        7
    qiuai  
       2012-04-26 14:04:50 +08:00
    撑的好开哦....
    reus
        8
    reus  
       2012-04-26 16:10:02 +08:00
    建议给代码框加上收起功能,有时只想继续看贴不想看代码,但是像上面那么长的,要滚很久啊 @Livid
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5639 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 06:06 · PVG 14:06 · LAX 23:06 · JFK 02:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.