summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2012-07-13 14:47:01 +0100
committerJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2012-07-13 14:47:01 +0100
commitce7e7072857d3122c89de30543051ef3c5fad2e9 (patch)
treec1df52013b63247af419961169c0393203ddc7f7
parent01abb8447a1f52bbba181c2abacbe916d60b2793 (diff)
downloadpykolab-ce7e7072857d3122c89de30543051ef3c5fad2e9.tar.gz
Make sure timezone handling is implemented for start and end dates
Also, do not accidentally convert dates to datetimes
-rw-r--r--pykolab/xml/event.py59
-rw-r--r--tests/test-003-event.py24
2 files changed, 69 insertions, 14 deletions
diff --git a/pykolab/xml/event.py b/pykolab/xml/event.py
index 7a1c2e7..d2973aa 100644
--- a/pykolab/xml/event.py
+++ b/pykolab/xml/event.py
@@ -3,6 +3,7 @@ import icalendar
from icalendar import vDatetime
from icalendar import vText
import kolabformat
+import pytz
import time
import uuid
@@ -390,19 +391,36 @@ class Event(object):
year,
month,
day,
- hour,
- minute,
- second
) = (
_datetime.year(),
_datetime.month(),
_datetime.day(),
- _datetime.hour(),
- _datetime.minute(),
- _datetime.second()
)
- return datetime.datetime(year, month, day, hour, minute, second)
+ if not _datetime.hour() == None and not _datetime.hour() < 0:
+ (
+ hour,
+ minute,
+ second
+ ) = (
+ _datetime.hour(),
+ _datetime.minute(),
+ _datetime.second()
+ )
+
+ _timezone = _datetime.timezone()
+
+ if _timezone == '':
+ _timezone = pytz.utc
+ elif _timezone == None:
+ _timezone = pytz.utc
+ else:
+ _timezone = pytz.timezone(_timezone)
+
+ if _datetime.hour() == None or _datetime.hour() < 0:
+ return datetime.date(year, month, day)
+ else:
+ return datetime.datetime(year, month, day, hour, minute, second, tzinfo=_timezone)
def get_status(self):
status = self.event.status()
@@ -485,6 +503,10 @@ class Event(object):
valid_datetime = True
if isinstance(_datetime, datetime.datetime):
+ # If no timezone information is passed on, make it UTC
+ if _datetime.tzinfo == None:
+ _datetime = _datetime.replace(tzinfo=pytz.utc)
+
valid_datetime = True
if not valid_datetime:
@@ -509,12 +531,14 @@ class Event(object):
_datetime.minute,
_datetime.second
)
+ _cdatetime = kolabformat.cDateTime(year, month, day, hour, minute, second)
else:
- (hour, minute, second) = (0,0,0)
+ _cdatetime = kolabformat.cDateTime(year, month, day)
- self.event.setEnd(
- kolabformat.cDateTime(year, month, day, hour, minute, second)
- )
+ if hasattr(_datetime, "tzinfo"):
+ _cdatetime.setTimezone(_datetime.tzinfo.__str__())
+
+ self.event.setEnd(_cdatetime)
def set_from_ical(self, attr, value):
if attr == "dtend":
@@ -663,11 +687,16 @@ class Event(object):
self.event.setPriority(priority)
def set_start(self, _datetime):
+
valid_datetime = False
if isinstance(_datetime, datetime.date):
valid_datetime = True
if isinstance(_datetime, datetime.datetime):
+ # If no timezone information is passed on, make it UTC
+ if _datetime.tzinfo == None:
+ _datetime = _datetime.replace(tzinfo=pytz.utc)
+
valid_datetime = True
if not valid_datetime:
@@ -692,10 +721,14 @@ class Event(object):
_datetime.minute,
_datetime.second
)
+ _cdatetime = kolabformat.cDateTime(year, month, day, hour, minute, second)
else:
- (hour, minute, second) = (0,0,0)
+ _cdatetime = kolabformat.cDateTime(year, month, day)
+
+ if hasattr(_datetime, "tzinfo"):
+ _cdatetime.setTimezone(_datetime.tzinfo.__str__())
- self.event.setStart(kolabformat.cDateTime(year, month, day, hour, minute, second))
+ self.event.setStart(_cdatetime)
def set_status(self, status):
if status in self.status_map.keys():
diff --git a/tests/test-003-event.py b/tests/test-003-event.py
index 031f4a1..c18523a 100644
--- a/tests/test-003-event.py
+++ b/tests/test-003-event.py
@@ -1,4 +1,6 @@
import datetime
+import pytz
+import sys
import unittest
from pykolab.xml import Attendee
@@ -14,7 +16,7 @@ class TestEventXML(unittest.TestCase):
self.assertRaises(EventIntegrityError, self.event.__str__)
def test_001_minimal(self):
- self.event.set_start(datetime.datetime.now())
+ self.event.set_start(datetime.datetime.now(pytz.timezone("Europe/London")))
self.assertIsInstance(self.event.get_start(), datetime.datetime)
self.assertIsInstance(self.event.__str__(), basestring)
@@ -73,5 +75,25 @@ class TestEventXML(unittest.TestCase):
self.event.delegate("jane@doe.org", "john@doe.org")
self.assertEqual(len(self.event.get_attendee("jane@doe.org").get_delegated_to()), 2)
+ def test_015_timezone(self):
+ _tz = self.event.get_start()
+ self.assertIsInstance(_tz.tzinfo, datetime.tzinfo)
+
+ def test_016_start_with_timezone(self):
+ _start = datetime.datetime(2012, 05, 23, 11, 58, 00, tzinfo=pytz.timezone("Europe/Zurich"))
+ _start_utc = _start.astimezone(pytz.utc)
+ self.assertEqual(_start.__str__(), "2012-05-23 11:58:00+01:00")
+ self.assertEqual(_start_utc.__str__(), "2012-05-23 10:58:00+00:00")
+ self.event.set_start(_start)
+ self.assertIsInstance(_start.tzinfo, datetime.tzinfo)
+ self.assertEqual(_start.tzinfo, pytz.timezone("Europe/Zurich"))
+
+ def test_017_allday_without_timezone(self):
+ _start = datetime.date(2012, 05, 23)
+ self.assertEqual(_start.__str__(), "2012-05-23")
+ self.event.set_start(_start)
+ self.assertEqual(hasattr(_start,'tzinfo'), False)
+ self.assertEqual(self.event.get_start().__str__(), "2012-05-23")
+
if __name__ == '__main__':
unittest.main()