aboutsummaryrefslogtreecommitdiffstats
path: root/contrib
diff options
context:
space:
mode:
authorLukas Fleischer <lfleischer@calcurse.org>2017-09-02 10:45:58 +0200
committerLukas Fleischer <lfleischer@calcurse.org>2017-09-08 22:35:36 +0200
commit8ee78c14af3b9974ad96cf85f6ea32c4e254f958 (patch)
tree3cd2f8c684c2e60a96031ee80fbe2878440b03e5 /contrib
parentd3cc496e028d26cd577c1027a4adc7224aadcfd7 (diff)
downloadcalcurse-8ee78c14af3b9974ad96cf85f6ea32c4e254f958.tar.gz
calcurse-8ee78c14af3b9974ad96cf85f6ea32c4e254f958.zip
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 <lfleischer@calcurse.org>
Diffstat (limited to 'contrib')
-rwxr-xr-xcontrib/calcurse-dateutil.py77
1 files changed, 77 insertions, 0 deletions
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)