Major restructuring to get automatic decoding/encoding
[pycalendar.git] / pycalendar / dtype / geo.py
diff --git a/pycalendar/dtype/geo.py b/pycalendar/dtype/geo.py
new file mode 100644 (file)
index 0000000..4a7ac70
--- /dev/null
@@ -0,0 +1,31 @@
+# Copyright
+
+"""Functions for processing geographic position
+
+As defined in :RFC:`5545`, section 3.8.1.6 (Geographic Position).
+"""
+
+from . import base as _base
+
+
+class Geo (_base.DataType):
+    name = 'GEO'
+
+    @classmethod
+    def decode(cls, property, value):
+        """Parse geographic position
+
+        As defined in :RFC:`5545`, section 3.8.1.6 (Geographic
+        Position).
+
+        >>> Geo.decode(property={}, value='37.386013;-122.082932')
+        (37.386013, -122.082932)
+        """
+        geo = tuple(float(x) for x in value.split(';'))
+        if len(geo) != 2:
+            raise ValueError(value)
+        return geo
+
+    @classmethod
+    def encode(cls, property, value):
+        return '{:.6f};{:.6f}'.format(*value)