1010# License for the specific language governing permissions and limitations
1111# under the License.
1212
13- from datetime import datetime
14- import numbers
15- import time
16-
17- from iso8601 import iso8601
18- import six
19-
20- from oslo_utils import timeutils
21-
2213
2314class Formatter (object ):
2415
@@ -33,82 +24,6 @@ def deserialize(cls, value):
3324 raise NotImplementedError
3425
3526
36- class ISO8601 (Formatter ):
37-
38- @classmethod
39- def serialize (cls , value ):
40- """Convert a datetime to an ISO8601 string"""
41- if isinstance (value , datetime ):
42- return value .isoformat ()
43- elif isinstance (value , six .string_types ):
44- # If we're already given a string, keep it as-is.
45- # This happens when a string comes back in a response body,
46- # as opposed to the datetime case above which happens when
47- # a user is setting a datetime for a request.
48- return value
49- else :
50- raise ValueError ("Unable to serialize ISO8601: %s" % value )
51-
52- @classmethod
53- def deserialize (cls , value ):
54- """Convert an ISO8601 string to a datetime object"""
55- if isinstance (value , six .string_types ):
56- return timeutils .parse_isotime (value )
57- else :
58- raise ValueError ("Unable to deserialize ISO8601: %s" % value )
59-
60-
61- class UNIXEpoch (Formatter ):
62-
63- EPOCH = datetime (1970 , 1 , 1 , tzinfo = iso8601 .UTC )
64-
65- @classmethod
66- def serialize (cls , value ):
67- """Convert a datetime to a UNIX epoch"""
68- if isinstance (value , datetime ):
69- # Do not try to format using %s as it's platform dependent.
70- return (value - cls .EPOCH ).total_seconds ()
71- elif isinstance (value , numbers .Number ):
72- return value
73- else :
74- raise ValueError ("Unable to serialize UNIX epoch: %s" % value )
75-
76- @classmethod
77- def deserialize (cls , value ):
78- """Convert a UNIX epoch into a datetime object"""
79- try :
80- value = float (value )
81- except ValueError :
82- raise ValueError ("Unable to deserialize UNIX epoch: %s" % value )
83-
84- # gmtime doesn't respect microseconds so we need to parse them out
85- # if they're there and build the datetime appropriately with the
86- # proper precision.
87- # NOTES:
88- # 1. datetime.fromtimestamp sort of solves this but using localtime
89- # instead of UTC, which we need.
90- # 2. On Python 2 we can't just str(value) as it truncates digits
91- # that are significant to us.
92- parsed_value = "%000000f" % value
93- decimal = parsed_value .find ("." )
94-
95- if decimal == - 1 :
96- microsecond = 0
97- else :
98- # Some examples of these timestamps include less precision
99- # than the allowable 6 digits that can represent microseconds,
100- # so since we have a string we need to construct a real
101- # count of microseconds instead of just converting the
102- # stringified amount to an int.
103- fractional_second = float (parsed_value [decimal :]) * 1e6
104- microsecond = int (fractional_second )
105-
106- gmt = time .gmtime (value )
107-
108- return datetime (* gmt [:6 ], microsecond = microsecond ,
109- tzinfo = iso8601 .UTC )
110-
111-
11227class BoolStr (Formatter ):
11328
11429 @classmethod
0 commit comments