对自定义天,周,月分区的转换
import datetime,platform
from dateutil.relativedelta import relativedelta
def get_partition_list(default_partition):
partition_list = []
system = platform.system()
# windows本地跑代码
if system == 'Windows':
now = datetime.datetime.now()
# 服务器时间是utc时间,需要加上8小时
else:
now = datetime.datetime.now() + datetime.timedelta(hours=8)
dlp = default_partition
if ':' in dlp:
num_bg, num_ed = 0, 0
dlpv = dlp.split(':')
if '-' in dlpv[0]:
num_bg = int(dlpv[0].split('-')[1])
elif not dlpv[0]:
num_bg = 1
if '-' in dlpv[1]:
num_ed = int(dlpv[1].split('-')[1])
elif not dlpv[1]:
num_ed = 1
# T-60:T-2
if 'T-' in dlp:
for i in range(num_ed, num_bg + 1):
partition_value = datetime.datetime.strftime(now + datetime.timedelta(days=-i), '%Y-%m-%d')
partition_list.append(partition_value)
# M-60:M-2
elif 'M-' in dlp:
# 时间是以昨天的日期为主,以当前月的1号为例,今天是2022-04-01,同步的是3月的数据
now += datetime.timedelta(days=-1)
for i in range(num_ed, num_bg + 1):
partition_value = datetime.datetime.strftime(
datetime.datetime(now.year, now.month, 1) + relativedelta(months=-i), '%Y-%m')
partition_list.append(partition_value)
# W-60:W-2
elif 'W-' in dlp:
for i in range(num_ed, num_bg + 1):
weeks_value = (datetime.datetime(now.year, now.month, now.day) + relativedelta(weeks=-i)).isocalendar()
weeks = '0{weeks}'.format(weeks=weeks_value[1]) if weeks_value[1] and weeks_value[1] != 0 and len(
str(weeks_value[1])) == 1 else weeks_value[1]
partition_value = "{year}-{weeks}".format(year=weeks_value[0], weeks=weeks)
partition_list.append(partition_value)
# T-2
if dlp.split('-') and dlp.split('-')[0] == 'T':
if dlp == 'T':
dt = 0
else:
dt = int(dlp.split('-')[1])
partition_value = datetime.datetime.strftime(now + datetime.timedelta(days=-dt), '%Y-%m-%d')
partition_list.append(partition_value)
# M-1
elif dlp.split('-') and dlp.split('-')[0] == 'M':
# 时间是以昨天的日期为主,以当前月的1号为例,今天是2022-04-01,同步的是3月的数据
now += datetime.timedelta(days=-1)
if dlp == 'M':
dt = 0
else:
dt = int(dlp.split('-')[1])
partition_value = datetime.datetime.strftime(
datetime.datetime(now.year, now.month, 1) + relativedelta(months=-dt), '%Y-%m')
partition_list.append(partition_value)
# W-1
elif dlp.split('-') and dlp.split('-')[0] == 'W':
if dlp == 'W':
dt = 0
else:
dt = int(dlp.split('-')[1])
weeks_value = (datetime.datetime(now.year, now.month, now.day) + relativedelta(weeks=-dt)).isocalendar()
weeks = '0{weeks}'.format(weeks=weeks_value[1]) if weeks_value[1] and weeks_value[1] != 0 and len(
str(weeks_value[1])) == 1 else weeks_value[1]
partition_value = "{year}-{weeks}".format(year=weeks_value[0], weeks=weeks)
partition_list.append(partition_value)
return partition_list
对自定义时分秒的转换
system = platform.system()
# windows本地跑代码
if system == 'Windows':
now = datetime.datetime.now()+ datetime.timedelta(days=-1)
now = now.replace(year=now.date().year, month=now.date().month, day=now.date().day, hour=0, minute=10, second=0)
else:
now = datetime.datetime.now()+ datetime.timedelta(days=-1, hours=8)
now = now.replace(year=now.date().year, month=now.date().month, day=now.date().day, hour=0, minute=10, second=0)
now_date = now.strftime("%Y-%m-%d %H:%M:%S")
|