Skip to content

Instantly share code, notes, and snippets.

Embed
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__)
try:
from ics import Calendar
except ImportError:
logger.error("You are missing the ics Python package. Please install it first.")
sys.exit(1)
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(f.read())
offset = timedelta(seconds=args.offset)
for e in c.events:
logger.debug('------')
logger.debug("Before correction: Event '{}' starts {} ends {}".format(e.name, e.begin, e.end))
e.end += offset
e.begin += offset
logger.debug("After correction: Event '{}' starts {} ends {}".format(e.name, e.begin, e.end))
logger.debug('------')
with open(args.output_file, 'w') as f:
f.write(str(c))
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.