zoukankan      html  css  js  c++  java
  • python获取当前时间戳Long类型

    python获取当前时间戳Long类型

    怎么用python完成获取时间,并且转换成long类型

    timestamp=long(int(round(time.time() * 1000)))
    

    怎么完成59分钟前,26秒前这些字段换成long类型

    def parse_time(time_str, tz=8):
        from datetime import datetime, timedelta
    
        def shift_time(time_delta, time_base=None):
            """
            通过time_base - time_delta计算时间戳
            """
            if not time_base:
                time_base = datetime.utcnow()
    
            return long((time_base + time_delta - datetime.utcfromtimestamp(0)).total_seconds() * 1000)
    
        m = re.search(ur'(d+)s*秒前', time_str)
        if m:
            return shift_time(timedelta(seconds=-int(m.group(1))))
        m = re.search(ur'(d+)s*分钟前', time_str)
        if m:
            return shift_time(timedelta(minutes=-int(m.group(1))))
        m = re.search(ur'(d+)s*小时前', time_str)
        if m:
            return shift_time(timedelta(hours=-int(m.group(1))))
        m = re.search(ur'(d+)s*天前', time_str)
        if m:
            return shift_time(timedelta(days=int(m.group(1))))
        m = re.search(ur'刚刚', time_str)
        if m:
            import time
            return long(int(round(time.time() * 1000)))
        def parse_from_fmt(time_fmt):
            try:
                return long((datetime.strptime(time_str, time_fmt) - timedelta(
                    seconds=tz * 3600) - datetime.utcfromtimestamp(0)).total_seconds() * 1000)
            except (UnicodeEncodeError, ValueError):
                return None
    
        for fmt in ['%y/%m/%d %H:%M:%S', '%Y-%m-%d %H:%M:%S','%y/%m/%d %H:%M','%Y-%m-%d %H:%M']:
            timestamp = parse_from_fmt(fmt)
            if timestamp:
                return timestamp
    
        raise ValueError(u'Invalid time string: %s' % time_str)
    
    联系我

    本人qq群 463175657

  • 相关阅读:
    nodejs获取服务器数据到页面
    Struts 2
    JQuery
    JDBC
    Hiberbate
    EasyUi
    JavaScript
    利用 HashSet 去过滤元素是否重复
    HTML
    MySQL
  • 原文地址:https://www.cnblogs.com/SK1995/p/6677224.html
Copyright © 2011-2022 走看看