python数据迁移--未完

有个需求,要求只保留表的上个月的数据,那么我在crontab中定义每个月1号执行这个脚本
那么时间是  xx月份-01日 某点某分
那我就先把这个上个月的时间返回出来,放到sql里面,把对应的id搞出来,那么这个id就是要迁移数据的最大值。在它之前的数据全部都要迁走,迁走之后再调用工具重建该表,来提升查询的效率。我先把第一块,取系统时间的函数写出来,后面的功能我再慢慢加,别急


import datetime

'''

获取上个月的系统时间,下个月的时间只是顺手写写,暂时用不上

'''

def get_last_1_month_time():
    today = datetime.datetime.today()
    year = today.year
    month = today.month
    if month == 1:
        month = 12
        year -= 1
    else:
        month -= 1
    res = datetime.datetime(year,month,1)
    return res


def get_next_1_month_time():
    today = datetime.datetime.today()
    year = today.year
    month = today.month
    if month == 12:
        month = 1
        year += 1
    else:
        month += 1
    res = datetime.datetime(year,month,1)
    return res


if __name__ == '__main__':
    last_month = get_last_1_month_time()
    print last_month
    next_month = get_next_1_month_time()
    print next_month
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值