Skip to content

Instantly share code, notes, and snippets.

@realmayus
Last active April 10, 2023 18:22
Show Gist options
  • Save realmayus/7127b01ccc518eb81f094c1f12598531 to your computer and use it in GitHub Desktop.
Save realmayus/7127b01ccc518eb81f094c1f12598531 to your computer and use it in GitHub Desktop.
.ics date offset program

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment