Major restructuring to get automatic decoding/encoding
[pycalendar.git] / pycalendar / dtype / date.py
diff --git a/pycalendar/dtype/date.py b/pycalendar/dtype/date.py
new file mode 100644 (file)
index 0000000..8c28755
--- /dev/null
@@ -0,0 +1,34 @@
+# Copyright
+
+"""Functions for processing dates without times
+
+As defined in :RFC:`5545`, section 3.3.4 (Date).
+"""
+
+import datetime as _datetime
+
+from . import base as _base
+
+
+class Date (_base.DataType):
+    name = 'DATE'
+
+    @classmethod
+    def decode(cls, property, value):
+        """Decode dates without times
+
+        As defined in :RFC:`5545`, section 3.3.4 (Date).
+
+        >>> Date.decode(property={}, value='19970714')
+        datetime.date(1997, 7, 14)
+        """
+        if len(value) != 8:
+            raise ValueError(value)
+        year = int(value[0:4])
+        month = int(value[4:6])
+        day = int(value[6:8])
+        return _datetime.date(year=year, month=month, day=day)
+
+    @classmethod
+    def encode(cls, property, value):
+        return value.strftime('%Y%m%d')