From 8ee78c14af3b9974ad96cf85f6ea32c4e254f958 Mon Sep 17 00:00:00 2001 From: Lukas Fleischer Date: Sat, 2 Sep 2017 10:45:58 +0200 Subject: Add calcurse-dateutil Add a script to iterate over a certain date range, performing operations on each of the dates and printing the result. An additional string can be appended to each of the printed lines. The script can be used to create "extended recurrent events". For example ./calcurse-dateutil.py --date 2017-01-01 --range 365 --unique \ --append ' [1] Test event' bom next-weekday 3 skip-days 14 can be used to print an event for each third Thursday of each month in 2017. Signed-off-by: Lukas Fleischer --- contrib/calcurse-dateutil.py | 77 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100755 contrib/calcurse-dateutil.py diff --git a/contrib/calcurse-dateutil.py b/contrib/calcurse-dateutil.py new file mode 100755 index 0000000..0b3ef61 --- /dev/null +++ b/contrib/calcurse-dateutil.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python3 + +import argparse +import datetime + + +def get_date(s): + return datetime.datetime.strptime(s, '%Y-%m-%d').date() + + +parser = argparse.ArgumentParser('calcurse-dateutil') +parser.add_argument('--date', type=get_date, action='store', dest='date') +parser.add_argument('--range', type=int, action='store', dest='range') +parser.add_argument('--unique', action='store_true', dest='unique') +parser.add_argument('--append', type=str, action='store', dest='append') +parser.add_argument('op', nargs='+') +args = parser.parse_args() + + +def skip_days(d, n): + return d + datetime.timedelta(days=n) + + +def skip_months(d, n): + return d + datetime.timedelta(months=1) + + +def next_weekday(d, w): + return skip_days(d, (w - d.weekday() + 7) % 7) + + +def bow(d): + return skip_days(d, -d.weekday()) + + +def bom(d): + return d.replace(day=1) + + +def eow(d): + return skip_days(bow(d), 6) + + +def eom(d): + return skip_months(bom(d), 1) + + +s = args.date if args.date else datetime.date.today() +r = args.range if args.range else 1 +a = args.append if args.append else '' +seen = set() + +for i in range(0, r): + d = skip_days(s, i) + it = iter(args.op) + for arg in it: + if arg == 'bow': + d = bow(d) + elif arg == 'bom': + d = bom(d) + elif arg == 'eow': + d = eow(d) + elif arg == 'eom': + d = eom(d) + elif arg == 'next-weekday': + d = next_weekday(d, int(next(it))) + elif arg == 'skip-days': + d = skip_days(d, int(next(it))) + elif arg == 'skip-months': + d = skip_months(d, int(next(it))) + out = "{}{}".format(d, a) + if args.unique: + if d not in seen: + print(out) + seen.add(d) + else: + print(out) -- cgit v1.2.3-70-g09d2