zoukankan      html  css  js  c++  java
  • python_时间戳处理

    def is_weekend(date):
        return date.strftime("%w") == "0" or date.strftime("%w") == "6"
    
    
    def getDays(num, weekend):
        days = []
        for i in range(1, num * 7):
            day = datetime.datetime.now() + datetime.timedelta(days=-i)
            if is_weekend(day) and weekend:
                days.append(day.strftime('%Y%m%d'))
            elif not is_weekend(day) and not weekend:
                days.append(day.strftime('%Y%m%d'))
            if len(days) >= num:
                break
        return days
    
    
    def get_ts(num):
        return int(time.mktime(datetime.date.today().timetuple()) - num * 60 * 60 * 24) * 1000
    
    
    def get_ds(num):
        return (datetime.datetime.now() + datetime.timedelta(days=-num)).strftime('%Y%m%d')
    
    
    def get_ds_hour(days=0, hours=0):
        return (datetime.datetime.now() + datetime.timedelta(days=-days, hours=hours)).strftime('%Y%m%d%H')
    
    
    def get_date(num, format='%Y-%m-%d'):
        return (datetime.datetime.now() + datetime.timedelta(days=-num)).strftime(format)
    
    
    def get_month_first(num):
        date = (datetime.date.today() - relativedelta(months=num))
        return datetime.date(date.year, date.month, 1)
    
    
    def get_month_first_date(num):
        return get_month_first(num).strftime('%Y-%m-%d')
    
    
    def get_month_first_ds(num):
        return get_month_first(num).strftime('%Y%m%d')
    
    
    def get_month_first_ts(num):
        return date2ts(get_month_first(num))
    
    
    def get_month(num):
        return get_month_first(num).strftime('%Y%m')
    
    
    def get_week_first(num):
        now = datetime.date.today()
        return now - datetime.timedelta(days=now.weekday()) - relativedelta(weeks=num)
    
    
    def get_week_last(num):
        now = datetime.date.today()
        return now + datetime.timedelta(days=6 - now.weekday()) - relativedelta(weeks=num)
    
    
    def get_week_first_date(num):
        return get_week_first(num).strftime('%Y-%m-%d')
    
    
    def get_week_first_ds(num):
        return get_week_first(num).strftime('%Y%m%d')
    
    
    def get_week_first_ts(num):
        return date2ts(get_week_first(num))
    
    
    def get_week_last_ds(num):
        return get_week_last(num).strftime('%Y%m%d')
    
    
    def ts2date(ts, format="%Y-%m-%d %H:%M:%S"):
        return time.strftime(format, time.localtime(ts / 1000))
    
    
    def date2ts(date):
        return int(time.mktime(date.timetuple())) * 1000
  • 相关阅读:
    动态规划专题选做
    「HZOJ NOIP2020 Round #13」20201127模拟 题解
    「HZOJ NOIP2020 Round #12」20201124模拟 简要题解
    JOI 2019 Final 硬币收藏 第18回日本情報オリンピック 本選 コイン集め 解説
    0202S-SCP 收容记
    NC50993 The XOR Largest Pair 0-1Trie Xor
    LG3120 [USACO15FEB]Cow Hopscotch G CDQ分治维护DP顺序
    2020牛客NOIP赛前集训营-提高组(第二场)
    「HZOJ NOIP2020 Round #5」20201018 模拟
    关于我
  • 原文地址:https://www.cnblogs.com/xiaokuangnvhai/p/11020118.html
Copyright © 2011-2022 走看看