Skip to content

Instantly share code, notes, and snippets.

@quxiaowei
Created July 27, 2023 16:05
Show Gist options
  • Save quxiaowei/9f86dce35f6ad85d3c7b151f15d92074 to your computer and use it in GitHub Desktop.
Save quxiaowei/9f86dce35f6ad85d3c7b151f15d92074 to your computer and use it in GitHub Desktop.
from datetime import timedelta
from datetime import datetime
#### testing data
raw_set = [
{"begin": "20231201", "end": "20240101", "content": "a"},
{"begin": "20231217", "end": "20240101", "content": "a1"},
{"begin": "20231204", "end": "20231206", "content": "b"},
{"begin": "20231101", "end": "20231215", "content": "c"},
{"begin": "20231101", "end": "20231214", "content": "c1"},
{"begin": "20231202", "end": "20231217", "content": "d"},
{"begin": "20241202", "end": "20241217", "content": "e"},
{"begin": "20241218", "end": "20241220", "content": "e1"},
{"begin": "20241220", "end": "20241231", "content": "e2"},
]
#### init date
data_set = [
{
"begin": datetime.fromisoformat(r["begin"]),
"end": datetime.fromisoformat(r["end"]),
"content": r["content"],
}
for r in raw_set
]
oneday = timedelta(days=1)
#### init date array
date_set = []
for row in data_set:
date_set.append(row["begin"])
date_set.append(row["end"] + oneday)
date_set.append(datetime.fromisoformat("19700101"))
date_set.append(datetime.fromisoformat("99991231"))
date_set = sorted(list(set(date_set)))
#### go!!!
result = []
for i in range(len(date_set)-1):
busy = False
new_row = {"begin": "", "end": "", "content": ""}
begin_date = date_set[i]
end_date = date_set[i+1]
for row in data_set:
if begin_date >= row["begin"] and begin_date <= row["end"]:
busy = True
new_row["content"] = new_row["content"] + row["content"] + ";"
new_row["begin"] = begin_date
new_row["end"] = end_date
if busy:
result.append(new_row)
for r in result:
print(r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment