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

default_datetime_serializer()   A

Complexity

Conditions 3

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 18
rs 10
c 0
b 0
f 0
cc 3
nop 3
1
from datetime import datetime
2
from typing import Optional
3
from jsons._datetime_impl import get_offset_str
4
from jsons._main_impl import RFC3339_DATETIME_PATTERN
5
6
7
def default_datetime_serializer(obj: datetime,
8
                                strip_microseconds: Optional[bool] = True,
9
                                **kwargs) -> str:
10
    """
11
    Serialize the given datetime instance to a string. It uses the RFC3339
12
    pattern. If datetime is a localtime, an offset is provided. If datetime is
13
    in UTC, the result is suffixed with a 'Z'.
14
    :param obj: the datetime instance that is to be serialized.
15
    :param strip_microseconds: determines whether microseconds should be
16
    omitted.
17
    :param kwargs: not used.
18
    :return: ``datetime`` as an RFC3339 string.
19
    """
20
    pattern = RFC3339_DATETIME_PATTERN
21
    offset = get_offset_str(obj)
22
    if not strip_microseconds and obj.microsecond:
23
        pattern += '.%f'
24
    return obj.strftime("{}{}".format(pattern, offset))
25