From 11b50d11a86a461740d217dbebb896d119c955bc Mon Sep 17 00:00:00 2001 From: Tomas Babej Date: Fri, 6 Feb 2015 20:20:35 +0100 Subject: [PATCH] Add documentation for localized timezones --- docs/index.rst | 82 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 2 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index 5c8b387..90cd94b 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -84,7 +84,7 @@ Attributes of task objects are accessible through indices, like so:: >>> task['id'] 15 >>> task['due'] - datetime.datetime(2013, 12, 5, 0, 0) + datetime.datetime(2015, 2, 5, 0, 0, tzinfo=) >>> task['tags'] ['work', 'servers'] @@ -239,6 +239,84 @@ same Python object:: >>> task3 == task1 True +Dealing with dates and time +--------------------------- + +Any timestamp-like attributes of the tasks are converted to timezone-aware +datetime objects. To achieve this, Tasklib leverages ``pytz`` Python module, +which brings the Olsen timezone databaze to Python. + +This shields you from annoying details of Daylight Saving Time shifts +or conversion between different timezones. For example, to list all the +tasks which are due midnight if you're currently in Berlin: + + >>> myzone = pytz.timezone('Europe/Berlin') + >>> midnight = myzone.localize(datetime(2015,2,2,0,0,0)) + >>> tw.tasks.filter(due__before=midnight) + +However, this is still a little bit tedious. That's why TaskWarrior object +is capable of automatic timezone detection, using the ``tzlocal`` Python +module. If your system timezone is set to 'Europe/Berlin', following example +will work the same way as the previous one: + + >>> tw.tasks.filter(due__before=datetime(2015,2,2,0,0,0)) + +You can also use simple dates when filtering: + + >>> tw.tasks.filter(due__before=date(2015,2,2)) + +In such case, a 00:00:00 is used as the time component. + +Of course, you can use datetime naive objects when initializing Task object +or assigning values to datetime atrributes: + + >>> t = Task(tw, description="Buy new shoes", due=date(2015,2,5)) + >>> t['due'] + datetime.datetime(2015, 2, 5, 0, 0, tzinfo=) + >>> t['due'] = date(2015,2,6,15,15,15) + >>> t['due'] + datetime.datetime(2015, 2, 6, 15, 15, 15, tzinfo=) + +However, since timezone-aware and timezone-naive datetimes are not comparable +in Python, this can cause some unexpected behaviour: + + >>> from datetime import datetime + >>> now = datetime.now() + >>> t = Task(tw, description="take out the trash now") + >>> t['due'] = now + >>> now + datetime.datetime(2015, 2, 1, 19, 44, 4, 770001) + >>> t['due'] + datetime.datetime(2015, 2, 1, 19, 44, 4, 770001, tzinfo=) + >>> t['due'] == now + Traceback (most recent call last): + File "", line 1, in + TypeError: can't compare offset-naive and offset-aware datetimes + +If you want to compare datetime aware value with datetime naive value, you need +to localize the naive value first: + + >>> from datetime import datetime + >>> from tasklib.task import local_zone + >>> now = local_zone.localize(datetime.now()) + >>> t['due'] = now + >>> now + datetime.datetime(2015, 2, 1, 19, 44, 4, 770001, tzinfo=) + >>> t['due'] == now + True + +Also, note that it does not matter whether the timezone aware datetime objects +are set in the same timezone: + + >>> import pytz + >>> t['due'] + datetime.datetime(2015, 2, 1, 19, 44, 4, 770001, tzinfo=) + >>> now.astimezone(pytz.utc) + datetime.datetime(2015, 2, 1, 18, 44, 4, 770001, tzinfo=) + >>> t['due'] == now.astimezone(pytz.utc) + True + + Working with annotations ------------------------ @@ -253,7 +331,7 @@ Annotations have only defined ``entry`` and ``description`` values:: >>> annotation = annotated_task['annotations'][0] >>> annotation['entry'] - datetime.datetime(2015, 1, 3, 21, 13, 55) + datetime.datetime(2015, 1, 3, 21, 13, 55, tzinfo=) >>> annotation['description'] u'Yeah, I am annotated!' -- 2.39.2