• time


    def parse_time(self, date):
        if re.match('刚刚', date):
            date = time.strftime('%Y-%m-%d %H:%M', time.localtime(time.time()))
        if re.match('d+分钟前', date):
            minute = re.match('(d+)', date).group(1)
            date = time.strftime('%Y-%m-%d %H:%M', time.localtime(time.time() - float(minute) * 60))
        if re.match('d+小时前', date):
            hour = re.match('(d+)', date).group(1)
            date = time.strftime('%Y-%m-%d %H:%M', time.localtime(time.time() - float(hour) * 60 * 60))
        if re.match('昨天.*', date):
            date = re.match('昨天(.*)', date).group(1).strip()
            date = time.strftime('%Y-%m-%d', time.localtime() - 24 * 60 * 60) + ' ' + date
        if re.match('d{2}-d{2}', date):
            date = time.strftime('%Y-', time.localtime()) + date + ' 00:00'
        return date
    import time;  # 引入time模块
    
    # 当前时间戳
    ticks = time.time()
    print "当前时间戳为:", ticks         #  1459994552.51
    
    # 获取当前时间
    localtime = time.localtime(time.time())
    print "本地时间为 :", localtime
    import time
    
    # 格式化成2016-03-20 11:45:39形式
    print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 
    
    # 格式化成Sat Mar 28 22:24:24 2016形式
    print time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()) 
      
    # 将格式字符串转换为时间戳
    a = "Sat Mar 28 22:24:24 2016"
    print time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y"))
    
    # 2016-04-07 10:25:09        #  常用
    # Thu Apr 07 10:25:09 2016
    # 1459175064.0
    import datetime
    import json
     
    dic = {
        'k1':123,
        'ctime':datetime.datetime.now()
    }
     
    class MyEncoder(json.JSONEncoder):
        def default(self, o): # o是数据类型
            if isinstance(o,datetime.datetime):
                return o.strftime('%Y-%m-%d')
            else:
                return super(MyEncoder,self).default(o)
     
    v = json.dumps(dic,cls=MyEncoder)
    print(v)
    # {"k1": 123, "ctime": "2018-05-15"}
    View Code
  • 相关阅读:
    浅谈REST[转]
    linuxyum
    XAMPPAccess denied for user 'root'@'localhost' (using password:YES)....& can't connect to localhost...
    ps aux详细解释【转】
    TOP 10:值得关注的十家云计算公司【转】
    isnull在数据库查询中的应用
    正则匹配代码
    推荐一款生成SQL插入语句的软件
    网页自动刷新
    执行SQL脚本语句判断是否已经存在
  • 原文地址:https://www.cnblogs.com/nick477931661/p/9164197.html
Copyright © 2020-2023  润新知