Passed
Pull Request — master (#17)
by Ramon
49s
created

jsons.deserializers.default_datetime   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 15
dl 0
loc 25
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A default_datetime_deserializer() 0 18 2
1
import datetime
2
import re
3
from jsons._datetime_impl import get_datetime_inst
4
from jsons._main_impl import 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