summaryrefslogtreecommitdiffstats
path: root/pykolab/xml/utils.py
diff options
context:
space:
mode:
authorJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2014-03-04 13:25:37 +0100
committerJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2014-03-04 13:25:37 +0100
commitb8e3042daddd8a77d97080ed61eb0f135817ce79 (patch)
tree9dae0f994e26915103ee5fd43ecf4e948eac12d7 /pykolab/xml/utils.py
parent4c572065c63e3bb830070ab71583e349cc109455 (diff)
parentdd711f06e4e56ecbc6808a992e84f2a5f8e1a746 (diff)
downloadpykolab-b8e3042daddd8a77d97080ed61eb0f135817ce79.tar.gz
Merge branch 'master' of ssh://git.kolabsys.com/git/pykolab
Diffstat (limited to 'pykolab/xml/utils.py')
-rw-r--r--pykolab/xml/utils.py78
1 files changed, 76 insertions, 2 deletions
diff --git a/pykolab/xml/utils.py b/pykolab/xml/utils.py
index 0959c80..c09da54 100644
--- a/pykolab/xml/utils.py
+++ b/pykolab/xml/utils.py
@@ -1,19 +1,93 @@
import datetime
import pytz
+import kolabformat
def to_dt(dt):
"""
Convert a naive date or datetime to a tz-aware datetime.
"""
- if type(dt) == 'datetime.date' or not hasattr(dt, 'hour'):
+ if isinstance(dt, datetime.date) or not hasattr(dt, 'hour'):
dt = datetime.datetime(dt.year, dt.month, dt.day, 0, 0, 0, 0)
else:
if dt.tzinfo == None:
return dt.replace(tzinfo=pytz.utc)
+
+ return dt
+
+
+def from_cdatetime(_cdatetime, with_timezone=True):
+ """
+ Convert from kolabformat.cDateTime to datetime.date(time)
+ """
+ (
+ year,
+ month,
+ day,
+ ) = (
+ _cdatetime.year(),
+ _cdatetime.month(),
+ _cdatetime.day(),
+ )
+
+ if _cdatetime.hour() == None or _cdatetime.hour() < 0:
+ return datetime.date(year, month, day)
+
+ (
+ hour,
+ minute,
+ second
+ ) = (
+ _cdatetime.hour(),
+ _cdatetime.minute(),
+ _cdatetime.second()
+ )
+
+ if with_timezone:
+ _timezone = _cdatetime.timezone()
+
+ if _timezone == '' or _timezone == None:
+ _timezone = pytz.utc
else:
+ _timezone = pytz.timezone(_timezone)
- return dt
+ return datetime.datetime(year, month, day, hour, minute, second, tzinfo=_timezone)
+
+ else:
+ return datetime.datetime(year, month, day, hour, minute, second)
+
+
+def to_cdatetime(_datetime, with_timezone=True):
+ """
+ Convert a datetime.dateime object into a kolabformat.cDateTime instance
+ """
+ (
+ year,
+ month,
+ day,
+ ) = (
+ _datetime.year,
+ _datetime.month,
+ _datetime.day,
+ )
+
+ if hasattr(_datetime, 'hour'):
+ (
+ hour,
+ minute,
+ second
+ ) = (
+ _datetime.hour,
+ _datetime.minute,
+ _datetime.second
+ )
+ _cdatetime = kolabformat.cDateTime(year, month, day, hour, minute, second)
+
+ else:
+ _cdatetime = kolabformat.cDateTime(year, month, day)
+ if with_timezone and hasattr(_datetime, "tzinfo"):
+ _cdatetime.setTimezone(_datetime.tzinfo.__str__())
+ return _cdatetime