将月中的日+周转换为日期



我正试图想出一个脚本将以下数据转换为日期和时间:原始数据如下所示:

default_maintenance:hour_start: 13minute_duration: 120周:3工作日:2

我猜拉出DB将把它放在这样的表中:

excel工作表

这显然是每个月不同的日期,那么我如何将该数据转换为日期呢?(我想我将需要运行这个至少每个月找到相应的日期。

使用bash或python是我的首选。我是python的初学者,最近学习了python中的datetime模块,它看起来很有前途。

假设您知道月份和年份(并且周是从第1周开始的已知月份中的一周),您可以使用datetimere如下:

import datetime
import re
# the string containing the information to extract
dstr = "default_maintenance: hour_start: 13 minute_duration: 120 week: 3 weekday: 2"
# the format of this string for parsing by re
fstr = "default_maintenance: hour_start: (d+) minute_duration: (d+) week: (d+) weekday: (d+)"
m = re.compile(fstr)
# extract the relevant numbers from the string
datenumbers = m.match(dstr).groups()
hr = datenumbers[0]
dur = datenumbers[1]
week = datenumbers[2]
day = datenumbers[3]
currentmonth = 9
currentyear = 2020
# day at the start of the current month (in days from start of the year)
startday = int(datetime.datetime(currentyear, currentmonth, 1).strftime("%j"))
# get the day of our data
curday = startday + (int(week) - 1) * 7 + int(day)
# convert everything into the date
date = datetime.datetime.strptime(str(currentyear)+str(curday)+hr, "%Y%j%H")
print(date.isoformat())
2020-09-17T13:00:00

假设持续时间不相关。

最新更新