zoukankan      html  css  js  c++  java
  • python 几分钟前,几小时前,几天前转为时间戳

    一开始我是这么做的

    import time
    import datetime
    
    def conv_time(t):
        min = re.findall('d+', t)[0]
        if u'分钟' in t:
            c = time.time() - int(min) * 60
    
        elif u'小时' in t:
            c = time.time() - int(min)*60*60
    
        elif u'' in t:
            c = time.time() - int(min)*60*60*24
        else:
            return None
        c = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(c))
        c = datetime.datetime.strptime(c, "%Y-%m-%d %H:%M:%S")
        return str(int(time.mktime(c.timetuple()))) + "000"

    后面看到了datetime的timedelta, 所以代码改成了这样

    from datetime import datetime, timedelta
    
    current_year = datetime.today().strftime("%Y")
    
    def conv_time(t):
        min = int(re.findall('d+', t)[0])
        if u'' in t:
            s = (datetime.now() - timedelta(seconds=min))
        elif u'分钟' in t:
            s = (datetime.now() - timedelta(minutes=min))
    
        elif u'小时' in t:
            s = (datetime.now() - timedelta(hours=min))
    
        elif u'' in t:
            s = (datetime.now() - timedelta(days=min))
        else:
            t += ", " + current_year
            s = datetime.strptime(t, "%m-%d, %Y")
        return str(int(time.mktime(s.timetuple()))) + "000"
  • 相关阅读:
    五、MapReduce 发布服务
    四、MapReduce 基础
    三、Hadoop 的 API
    二、HDFS 架构
    php身份证号的验证
    php性能优化
    PHP网站开发方案
    php一个不错的分页
    2013年最流行的php框架盘点
    程序员之路
  • 原文地址:https://www.cnblogs.com/kidl/p/9334386.html
Copyright © 2011-2022 走看看