diff --git a/README.md b/README.md index d96af6b..37cc20a 100644 --- a/README.md +++ b/README.md @@ -22,16 +22,22 @@ This hook leverages tasklib, so you need to install that too: pip --user install tasklib ``` +You’ll also need to install the [taskw library](https://github.com/ralphbean/taskw/tree/develop), hich allows us to read the configuration file: + +``` +git clone -b develop https://github.com/ralphbean/taskw.git +cp -r taskw/taskw ~/.local/lib/python2.7/site-packages/ +``` + Configuration ------------- -Edit both scripts and set ```LOCAL_TZ``` to your timezone and your desired -local ```DEFAULT_TIME```. For EST, default task date time of 21:30, it should look -as follows:: +Configure the timezone and default time with ```task config```: ``` -LOCALTZ = pytz.timezone('EST') # Your timezone -DEFAULT_TIME = time(21,30,0) # Your wanted default time +task config default.time 22:00 +task config default.timezone Europe/Madrid + ``` Example of usage diff --git a/pirate_add_default_time.py b/pirate_add_default_time.py index e9d9d19..3a364a5 100644 --- a/pirate_add_default_time.py +++ b/pirate_add_default_time.py @@ -1,19 +1,43 @@ #!/usr/bin/python -from datetime import time -from tasklib.task import Task, local_zone +from datetime import time, datetime, timedelta +from taskw import TaskWarrior +from pytz import timezone, utc -DEFAULT_TIME = time(22,0,0) # Your wanted default time +CONFIG = TaskWarrior.load_config() +if 'dst' in CONFIG['default']: + if CONFIG['default']['dst'].lower() in ['true', '1', 'yes']: + dst = True + else: + dst = False + +if 'timezone' in CONFIG['default']: + DEFAULT_ZONE = timezone(CONFIG['default']['timezone']) +else: + DEFAULT_ZONE = utc + +if 'time' in CONFIG["default"]: + DEFAULT_TIME = datetime.strptime(CONFIG['default']['time'], u'%H:%M').time() +else: + DEFAULT_TIME = time(0, 0, 0, 0, DEFAULT_ZONE) def is_local_midnight(timestamp): - return timestamp.astimezone(local_zone).time() == time(0,0,0) + tmp = utc.localize(timestamp).astimezone(DEFAULT_ZONE) + return tmp.time() == time(0, 0, 0) def set_default_time(timestamp): - return timestamp.astimezone(local_zone).replace( + utcoffset = DEFAULT_ZONE.utcoffset(timestamp, is_dst=dst).days * 24 + DEFAULT_ZONE.utcoffset(timestamp, is_dst=dst).seconds / 3600 + + tmp = timestamp.replace( hour=DEFAULT_TIME.hour, minute=DEFAULT_TIME.minute, second=DEFAULT_TIME.second, ) + if utcoffset > 0: + tmp += timedelta(days=1) + + return DEFAULT_ZONE.localize(tmp).astimezone(utc) + def hook_default_time(task): if task['due'] and is_local_midnight(task['due']): task['due'] = set_default_time(task['due'])