• python minus 3 days or n days


    python内置的方法,太简单了。。。
    from datetime import datetime,timedelta

    d = datetime.now()
    z = d + timedelta(days=-7)
    print str(z).split(' ')[0].split('-')[1]

    import calendar

    DAY_QUERY = 4

    def day_end_month(year, month):
    cal = calendar.month(year, month)
    return cal[-3:-1]

    def modify_time(time_str):
    # kwargs = {'earliest_time':'2017-02-20T00:00:00.000',
    # 'latest_time':'',
    # 'search_mode':'normal',
    # 'exec_mode':'blocking'}

    kwargs = {'earliest_time': '', 
              'latest_time': '', 
              'search_mode': 'normal', 
              'exec_mode': 'blocking'}
    
    append_hms = 'T00:00:00.000'
    
    # time_str: 2017-02-20
    tmp_year = time_str.split('-')[0]
    tmp_month = time_str.split('-')[1]
    tmp_day = time_str.split('-')[2]
    
    tmp_month_int = int(tmp_month)
    tmp_day_int = int(tmp_day)
    
    if tmp_day_int > 9+DAY_QUERY:
        # just modify month
        day_gt_13 = tmp_year + '-' + tmp_month + '-' + str(tmp_day_int - DAY_QUERY)
    
        kwargs['earliest_time'] = day_gt_13 + append_hms
        kwargs['latest_time'] = time_str + append_hms
    
    # 4-12
    if tmp_day_int > DAY_QUERY and tmp_day_int < 10+DAY_QUERY and tmp_month_int > 9:
        # modify day with add 0(zero)
        day_gt_3_and_lt_13 = tmp_year + '-' + tmp_month + '-' + '0' + str(tmp_day_int - DAY_QUERY)
    
        kwargs['earliest_time'] = day_gt_3_and_lt_13 + append_hms
        kwargs['latest_time'] = time_str + append_hms
    
    if tmp_day_int > DAY_QUERY and tmp_day_int < 10+DAY_QUERY and tmp_month_int > 1 and tmp_month_int < 10:
        x_day_gt_3_and_lt_13 = tmp_year + '-' + tmp_month + '-' + '0' + str(tmp_day_int - DAY_QUERY)
    
        kwargs['earliest_time'] = x_day_gt_3_and_lt_13 + append_hms
        kwargs['latest_time'] = time_str + append_hms
    
    
    # 1-3 and month > 1
    if tmp_day_int < 1+DAY_QUERY and tmp_month_int > 1:
        # modify day and month
        # put year and month with int
        tmp_new_day_gt_1 = int(day_end_month(int(tmp_year), tmp_month_int-1))
    
        # str(tmp_new_day_gt_1 - (3 - tmp_day_int)): get end of day of last month
        month_day_lt_4_and_month_gt_1 = tmp_year + '-' + '0' + str(tmp_month_int - 1) + '-' + str(
            tmp_new_day_gt_1 - (DAY_QUERY - tmp_day_int))
    
        kwargs['earliest_time'] = month_day_lt_4_and_month_gt_1 + append_hms
        kwargs['latest_time'] = time_str + append_hms
    
    
    if tmp_day_int < 1+DAY_QUERY and tmp_month_int == 1:
        pass
        # modify year, month and day
        tmp_new_month_eq_1 = '12'
        tmp_new_year_eq_1 = str(int(tmp_year) - 1)
    
        tmp_new_day_eq_1 = int(day_end_month(int(tmp_year), tmp_month_int))
    
        year_lt_4_and_month_eq_1 = tmp_new_year_eq_1 + '-' + tmp_new_month_eq_1 + '-' + str(
            tmp_new_day_eq_1 - (DAY_QUERY - tmp_day_int))
    
        kwargs['earliest_time'] = year_lt_4_and_month_eq_1 + append_hms
        kwargs['latest_time'] = time_str + append_hms
    
    if tmp_day_int > DAY_QUERY and tmp_day_int < 10+DAY_QUERY and tmp_month_int == 1:
        l_day_gt_3_and_lt_13 = tmp_year + '-' + tmp_month + '-' + '0' + str(tmp_day_int - DAY_QUERY)
    
        kwargs['earliest_time'] = l_day_gt_3_and_lt_13 + append_hms
        kwargs['latest_time'] = time_str + append_hms
    
    if tmp_day_int > 9+DAY_QUERY and tmp_month_int == 1:
        n_day_gt_13 = tmp_year + '-' + tmp_month + '-' + str(tmp_day_int - DAY_QUERY)
    
        kwargs['earliest_time'] = n_day_gt_13 + append_hms
        kwargs['latest_time'] = time_str + append_hms
    
    return kwargs
    

    print modify_time('2017-01-12')

  • 相关阅读:
    Linux内核配置过程
    Linux内核最顶层文档
    LeetCode 11月第2周题目汇总
    Chapter0
    序列加法的讨论
    ch2-基本工具介绍
    ch1-数据科学概述
    在Linux下制作Linux&windows启动盘
    VMware Workstation 与 Device/Credential Guard 不兼容?
    Linux mint 19.3配置CUDA+安装Tensorflow
  • 原文地址:https://www.cnblogs.com/otfsenter/p/6420764.html
Copyright © 2020-2023  润新知