summaryrefslogtreecommitdiffstats
path: root/pykolab/xml/utils.py
diff options
context:
space:
mode:
authorThomas Bruederli <bruederli@kolabsys.com>2014-02-21 00:10:39 -0500
committerThomas Bruederli <bruederli@kolabsys.com>2014-02-21 00:10:39 -0500
commit139192fc2cd52cfbacbf5319b0838a044d26bdbb (patch)
treee10b5888f7a2232143a4c9bd1ef7cc27fae007c0 /pykolab/xml/utils.py
parent46a148e0374d0f74ee3cc9ebab77c80f9ff603aa (diff)
downloadpykolab-139192fc2cd52cfbacbf5319b0838a044d26bdbb.tar.gz
Remove duplicated code for datetime conversion
Diffstat (limited to 'pykolab/xml/utils.py')
-rw-r--r--pykolab/xml/utils.py75
1 files changed, 75 insertions, 0 deletions
diff --git a/pykolab/xml/utils.py b/pykolab/xml/utils.py
index d003b92..c09da54 100644
--- a/pykolab/xml/utils.py
+++ b/pykolab/xml/utils.py
@@ -1,5 +1,6 @@
import datetime
import pytz
+import kolabformat
def to_dt(dt):
"""
@@ -16,3 +17,77 @@ def to_dt(dt):
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 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