diff --git a/cloudkitty/utils.py b/cloudkitty/utils.py index 4b865284..3ab25b68 100644 --- a/cloudkitty/utils.py +++ b/cloudkitty/utils.py @@ -106,6 +106,27 @@ def isotime(at=None, subsecond=False): return st +def iso8601_from_timestamp(timestamp, microsecond=False): + """Returns an iso8601 formatted date from timestamp""" + + # Python provides a similar instance method for datetime.datetime + # objects called isoformat() and utcfromtimestamp(). The format + # of the strings generated by isoformat() and utcfromtimestamp() + # have a couple of problems: + # 1) The method iso8601_from_timestamp in oslo_utils is realized + # by isotime, the strings generated by isotime are used in + # tokens and other public APIs that we can't change without a + # deprecation period. The strings generated by isoformat are + # not the same format, so we can't just change to it. + # 2) The strings generated by isoformat() and utcfromtimestamp() + # do not include the microseconds if the value happens to be 0. + # This will likely show up as random failures as parsers may be + # written to always expect microseconds, and it will parse + # correctly most of the time. + + return isotime(datetime.datetime.utcfromtimestamp(timestamp), microsecond) + + def dt2ts(orig_dt): """Translate a datetime into a timestamp.""" return calendar.timegm(orig_dt.timetuple()) @@ -129,7 +150,7 @@ def ts2iso(timestamp): """timestamp to is8601 format.""" if not isinstance(timestamp, float): timestamp = float(timestamp) - return timeutils.iso8601_from_timestamp(timestamp) + return iso8601_from_timestamp(timestamp) def dt2iso(orig_dt):