@@ -125,32 +125,52 @@ To decode UTC datetime values as :class:`~bson.datetime_ms.DatetimeMS`,
125
125
:attr: `~bson.datetime_ms.DatetimeConversionOpts.DATETIME_AUTO `,
126
126
:attr: `~bson.datetime_ms.DatetimeConversionOpts.DATETIME_CLAMP `.
127
127
:attr: `~bson.datetime_ms.DatetimeConversionOpts.DATETIME ` is the default
128
- option and has the behavior of raising an exception upon attempting to
129
- decode an out-of-range date.
128
+ option and has the behavior of raising an :class: ` ~builtin.OverflowError ` upon
129
+ attempting to decode an out-of-range date.
130
130
:attr: `~bson.datetime_ms.DatetimeConversionOpts.DATETIME_MS ` will only return
131
131
:class: `~bson.datetime_ms.DatetimeMS ` objects, regardless of whether the
132
- represented datetime is in- or out-of-range.
132
+ represented datetime is in- or out-of-range:
133
+
134
+ .. doctest ::
135
+
136
+ >>> from datetime import datetime
137
+ >>> from bson import encode, decode
138
+ >>> from bson.datetime_ms import DatetimeMS
139
+ >>> from bson.codec_options import CodecOptions, DatetimeConversionOpts
140
+ >>> x = encode({" x" : datetime(1970 , 1 , 1 )})
141
+ >>> codec_ms = CodecOptions(datetime_conversion = DatetimeConversionOpts.DATETIME_MS )
142
+ >>> decode(x, codec_options = codec_ms)
143
+ {'x': DatetimeMS(0)}
144
+
133
145
:attr: `~bson.datetime_ms.DatetimeConversionOpts.DATETIME_AUTO ` will return
134
146
:class: `~datetime.datetime ` if the underlying UTC datetime is within range,
135
147
or :class: `~bson.datetime_ms.DatetimeMS ` if the underlying datetime
136
- cannot be represented using the builtin Python :class: `~datetime.datetime `.
148
+ cannot be represented using the builtin Python :class: `~datetime.datetime `:
149
+
150
+ .. doctest ::
151
+
152
+ >>> x = encode({" x" : datetime(1970 , 1 , 1 )})
153
+ >>> y = encode({" x" : DatetimeMS(- 2 ** 62 )})
154
+ >>> codec_auto = CodecOptions(datetime_conversion = DatetimeConversionOpts.DATETIME_AUTO )
155
+ >>> decode(x, codec_options = codec_auto)
156
+ {'x': datetime.datetime(1970, 1, 1, 0, 0)}
157
+ >>> decode(y, codec_options = codec_auto)
158
+ {'x': DatetimeMS(-4611686018427387904)}
159
+
137
160
:attr: `~bson.datetime_ms.DatetimeConversionOpts.DATETIME_CLAMP ` will clamp
138
161
resulting :class: `~datetime.datetime ` objects to be within
139
162
:attr: `~datetime.datetime.min ` and :attr: `~datetime.datetime.max `
140
- (trimmed to `999000 ` microseconds).
141
-
142
- An example of encoding and decoding using `DATETIME_MS ` is as follows:
163
+ (trimmed to `999000 ` microseconds):
143
164
144
165
.. doctest ::
145
- >>> from datetime import datetime
146
- >>> from bson import encode, decode
147
- >>> from bson.datetime_ms import DatetimeMS
148
- >>> from bson.codec_options import CodecOptions,DatetimeConversionOpts
149
- >>> x = encode({" x" : datetime(1970 , 1 , 1 )})
150
- >>> x
151
- b'\x10\x00\x00\x00\tx\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
152
- >>> decode(x, codec_options = CodecOptions(datetime_conversion = DatetimeConversionOpts.DATETIME_MS ))
153
- {'x': DatetimeMS(0)}
166
+
167
+ >>> x = encode({" x" : DatetimeMS(2 ** 62 )})
168
+ >>> y = encode({" x" : DatetimeMS(- 2 ** 62 )})
169
+ >>> codec_clamp = CodecOptions(datetime_conversion = DatetimeConversionOpts.DATETIME_CLAMP )
170
+ >>> decode(x, codec_options = codec_clamp)
171
+ {'x': datetime.datetime(9999, 12, 31, 23, 59, 59, 999000)}
172
+ >>> decode(y, codec_options = codec_clamp)
173
+ {'x': datetime.datetime(1, 1, 1, 0, 0)}
154
174
155
175
:class: `~bson.datetime_ms.DatetimeMS ` objects have support for rich comparison
156
176
methods against other instances of :class: `~bson.datetime_ms.DatetimeMS `.
0 commit comments