Andriy Kornatskyy avatar Andriy Kornatskyy committed d115d52

Added format_iso_time and format_iso_datetime

Comments (0)

Files changed (1)

src/wheezy/core/datetime.py

 """ ``datetime`` module.
 """
 
-from time import gmtime
 from time import localtime
 from time import mktime
 
 # absolute imports are available since python 2.5,
 # however we want keep compatibility with python 2.4
 datetime = import_name('datetime.datetime')
+time = import_name('datetime.time')
 timedelta = import_name('datetime.timedelta')
 tzinfo = import_name('datetime.tzinfo')
 
     )
 
 
+def format_iso_datetime(stamp):
+    """ Return a string representing the date and time in ISO 8601 format.
+        If the time is in UTC, adds a 'Z' directly after the time without
+        a space.
+
+        see http://en.wikipedia.org/wiki/ISO_8601.
+
+        >>> class EET(tzinfo):
+        ...     def utcoffset(self, dt):
+        ...         return timedelta(minutes=120)
+        ...     def dst(self, dt):
+        ...         return timedelta()
+        >>> format_iso_datetime(datetime(2012, 2, 22, 12, 52, 29, 300))
+        '2012-02-22T12:52:29'
+        >>> format_iso_datetime(datetime(2012, 2, 22, 12, 52, 29, 300,
+        ...     tzinfo=UTC))
+        '2012-02-22T12:52:29Z'
+        >>> format_iso_datetime(datetime(2012, 2, 22, 12, 52, 29, 300,
+        ...     tzinfo=EET()))
+        '2012-02-22T12:52:29+02:00'
+    """
+    if stamp.tzinfo:
+        if stamp.utcoffset() == ZERO:
+            return datetime(*stamp.timetuple()[:6]).isoformat() + 'Z'
+    if stamp.microsecond:
+        stamp = stamp.replace(microsecond=0)
+    return stamp.isoformat()
+
+
+def format_iso_time(stamp):
+    """ Return a string representing the time in ISO 8601 format.
+        If the time is in UTC, adds a 'Z' directly after the time without
+        a space.
+
+        see http://en.wikipedia.org/wiki/ISO_8601.
+
+        >>> class EET(tzinfo):
+        ...     def utcoffset(self, dt):
+        ...         return timedelta(minutes=120)
+        ...     def dst(self, dt):
+        ...         return timedelta()
+        >>> format_iso_time(time(12, 52, 29, 300))
+        '12:52:29'
+        >>> format_iso_time(time(12, 52, 29, 300,
+        ...     tzinfo=UTC))
+        '12:52:29Z'
+        >>> format_iso_time(time(12, 52, 29, 300,
+        ...     tzinfo=EET()))
+        '12:52:29+02:00'
+    """
+    if stamp.microsecond:
+        stamp = stamp.replace(microsecond=0)
+    if stamp.tzinfo:
+        if stamp.utcoffset() == ZERO:
+            return stamp.replace(tzinfo=None).isoformat() + 'Z'
+        else:
+            return stamp.isoformat()
+    else:
+        return stamp.isoformat()
+
+
 def parse_http_datetime(stamp):
     """ Parses a string in rfc1123 format to ``datetime``.
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.