Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Fix time in .ics files
#!/usr/bin/env python
A script to shift times in a calendar (iCalendar / .ics) files
from datetime import timedelta
import argparse, sys, os, logging
logger = logging.getLogger(__name__)
from ics import Calendar
except ImportError:
logger.error("You are missing the ics Python package. Please install it first.")
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('input_file', help='The .ics file to read and correct')
parser.add_argument('output_file', nargs='?', help='The output filename. If not given, will use input file name + _time-fix.')
parser.add_argument('--offset', type=int, required=True, help='Time to add (+) or subtract (-) from all times')
parser.add_argument('--debug', action='store_true', help='Enable debugging output')
args = parser.parse_args()
if not args.output_file:
parts = os.path.splitext(args.input_file)
args.output_file = parts[0] + '_times-fixed' + parts[1]
level = logging.DEBUG if args.debug else logging.WARNING
logging.basicConfig(level=level, format='%(message)s')
with open(args.input_file, 'r') as f:
c = Calendar(
offset = timedelta(seconds=args.offset)
for e in
logger.debug("Before correction: Event '{}' starts {} ends {}".format(, e.begin, e.end))
e.end += offset
e.begin += offset
logger.debug("After correction: Event '{}' starts {} ends {}".format(, e.begin, e.end))
with open(args.output_file, 'w') as f:
print("Calendar file with corrected times written to {}".format(args.output_file))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.