diff options
author | Trygve Aaberge <trygveaa@gmail.com> | 2020-02-21 00:35:18 +0100 |
---|---|---|
committer | Trygve Aaberge <trygveaa@gmail.com> | 2020-02-21 00:47:43 +0100 |
commit | aaa7ed528a47d92c1ed9a1793a6bbde9db07cf5d (patch) | |
tree | 63217102b7747113d4f465c87e06dfc64275ef04 /wee_slack.py | |
parent | c0035aa9766e4b20ac597a146cea8b0c69dbedb1 (diff) | |
download | wee-slack-aaa7ed528a47d92c1ed9a1793a6bbde9db07cf5d.tar.gz |
Support parsing date refs
Diffstat (limited to 'wee_slack.py')
-rw-r--r-- | wee_slack.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/wee_slack.py b/wee_slack.py index 9370740..338c23a 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -6,6 +6,7 @@ from __future__ import print_function, unicode_literals from collections import OrderedDict +from datetime import date, datetime, timedelta from functools import wraps from io import StringIO from itertools import chain, count, islice @@ -3562,6 +3563,36 @@ def resolve_ref(ref): subteam = team.subteams.get(subteam_id) if subteam: return subteam.handle + elif ref.startswith("!date"): + parts = ref.split('^') + ref_datetime = datetime.fromtimestamp(int(parts[1])) + link_suffix = ' ({})'.format(parts[3]) if len(parts) > 3 else '' + token_to_format = { + 'date_num': '%Y-%m-%d', + 'date': '%B %d, %Y', + 'date_short': '%b %d, %Y', + 'date_long': '%A, %B %d, %Y', + 'time': '%H:%M', + 'time_secs': '%H:%M:%S' + } + + def replace_token(match): + token = match.group(1) + if token.startswith('date_') and token.endswith('_pretty'): + if ref_datetime.date() == date.today(): + return 'today' + elif ref_datetime.date() == date.today() - timedelta(days=1): + return 'yesterday' + elif ref_datetime.date() == date.today() + timedelta(days=1): + return 'tomorrow' + else: + token = token.replace('_pretty', '') + if token in token_to_format: + return ref_datetime.strftime(token_to_format[token]) + else: + return match.group(0) + + return re.sub(r"{([^}]+)}", replace_token, parts[2]) + link_suffix # Something else, just return as-is return ref |