default_datetime_deserializer()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 18
rs 9.9
c 0
b 0
f 0
cc 2
nop 3
1
import datetime
2
import re
3
4
from jsons._datetime_impl import get_datetime_inst, RFC3339_DATETIME_PATTERN
5
6
7
def default_datetime_deserializer(obj: str,
8
                                  cls: type = datetime,
9
                                  **kwargs) -> datetime:
10
    """
11
    Deserialize a string with an RFC3339 pattern to a datetime instance.
12
    :param obj: the string that is to be deserialized.
13
    :param cls: not used.
14
    :param kwargs: not used.
15
    :return: a ``datetime.datetime`` instance.
16
    """
17
    pattern = RFC3339_DATETIME_PATTERN
18
    if '.' in obj:
19
        pattern += '.%f'
20
        # strptime allows a fraction of length 6, so trip the rest (if exists).
21
        regex_pattern = re.compile(r'(\.[0-9]+)')
22
        frac = regex_pattern.search(obj).group()
23
        obj = obj.replace(frac, frac[0:7])
24
    return get_datetime_inst(obj, pattern)
25