Invoke with:
python main.py calendar.ics 2 0 0
python <script name> <calendar file> <hours> <minutes> <seconds>
import argparse | |
import re | |
from datetime import datetime, timedelta | |
parser = argparse.ArgumentParser() | |
parser.add_argument("file", help="Path to the input .ics file") | |
parser.add_argument("hours", help="Hours to add") | |
parser.add_argument("minutes", help="Minutes to add") | |
parser.add_argument("seconds", help="Seconds to add") | |
args = parser.parse_args() | |
with open(args.file, "r") as f: | |
data = f.read() | |
timestamp_pattern = re.compile(r"(\d{8}T\d{6})") | |
def do_offset(match): | |
timestamp_str = match.group(1) | |
timestamp = datetime.strptime(timestamp_str, "%Y%m%dT%H%M%S") | |
new_timestamp = timestamp + timedelta(hours=int(args.hours), minutes=int(args.minutes), seconds=int(args.seconds)) | |
new_timestamp_str = new_timestamp.strftime("%Y%m%dT%H%M%S") | |
return new_timestamp_str | |
new_data = timestamp_pattern.sub(do_offset, data) | |
with open(args.file[:-4] + "-mod.ics", "w") as f: | |
f.write(new_data) |