1# coding=utf-8
2# --------------------------------------------------------------------------
3# Copyright (c) Microsoft Corporation. All rights reserved.
4# Licensed under the MIT License. See License.txt in the project root for
5# license information.
6# --------------------------------------------------------------------------
7import base64
8from json import JSONEncoder
9from typing import Union, cast, Any
10from datetime import datetime, date, time, timedelta
11from datetime import timezone
12
13
14__all__ = ["NULL", "AzureJSONEncoder"]
15TZ_UTC = timezone.utc
16
17
18class _Null:
19 """To create a Falsy object"""
20
21 def __bool__(self) -> bool:
22 return False
23
24
25NULL = _Null()
26"""
27A falsy sentinel object which is supposed to be used to specify attributes
28with no data. This gets serialized to `null` on the wire.
29"""
30
31
32def _timedelta_as_isostr(td: timedelta) -> str:
33 """Converts a datetime.timedelta object into an ISO 8601 formatted string, e.g. 'P4DT12H30M05S'
34
35 Function adapted from the Tin Can Python project: https://github.com/RusticiSoftware/TinCanPython
36
37 :param td: The timedelta object to convert
38 :type td: datetime.timedelta
39 :return: An ISO 8601 formatted string representing the timedelta object
40 :rtype: str
41 """
42
43 # Split seconds to larger units
44 seconds = td.total_seconds()
45 minutes, seconds = divmod(seconds, 60)
46 hours, minutes = divmod(minutes, 60)
47 days, hours = divmod(hours, 24)
48
49 days, hours, minutes = list(map(int, (days, hours, minutes)))
50 seconds = round(seconds, 6)
51
52 # Build date
53 date_str = ""
54 if days:
55 date_str = "%sD" % days
56
57 # Build time
58 time_str = "T"
59
60 # Hours
61 bigger_exists = date_str or hours
62 if bigger_exists:
63 time_str += "{:02}H".format(hours)
64
65 # Minutes
66 bigger_exists = bigger_exists or minutes
67 if bigger_exists:
68 time_str += "{:02}M".format(minutes)
69
70 # Seconds
71 try:
72 if seconds.is_integer():
73 seconds_string = "{:02}".format(int(seconds))
74 else:
75 # 9 chars long w/ leading 0, 6 digits after decimal
76 seconds_string = "%09.6f" % seconds
77 # Remove trailing zeros
78 seconds_string = seconds_string.rstrip("0")
79 except AttributeError: # int.is_integer() raises
80 seconds_string = "{:02}".format(seconds)
81
82 time_str += "{}S".format(seconds_string)
83
84 return "P" + date_str + time_str
85
86
87def _datetime_as_isostr(dt: Union[datetime, date, time, timedelta]) -> str:
88 """Converts a datetime.(datetime|date|time|timedelta) object into an ISO 8601 formatted string.
89
90 :param dt: The datetime object to convert
91 :type dt: datetime.datetime or datetime.date or datetime.time or datetime.timedelta
92 :return: An ISO 8601 formatted string representing the datetime object
93 :rtype: str
94 """
95 # First try datetime.datetime
96 if hasattr(dt, "year") and hasattr(dt, "hour"):
97 dt = cast(datetime, dt)
98 # astimezone() fails for naive times in Python 2.7, so make make sure dt is aware (tzinfo is set)
99 if not dt.tzinfo:
100 iso_formatted = dt.replace(tzinfo=TZ_UTC).isoformat()
101 else:
102 iso_formatted = dt.astimezone(TZ_UTC).isoformat()
103 # Replace the trailing "+00:00" UTC offset with "Z" (RFC 3339: https://www.ietf.org/rfc/rfc3339.txt)
104 return iso_formatted.replace("+00:00", "Z")
105 # Next try datetime.date or datetime.time
106 try:
107 dt = cast(Union[date, time], dt)
108 return dt.isoformat()
109 # Last, try datetime.timedelta
110 except AttributeError:
111 dt = cast(timedelta, dt)
112 return _timedelta_as_isostr(dt)
113
114
115class AzureJSONEncoder(JSONEncoder):
116 """A JSON encoder that's capable of serializing datetime objects and bytes."""
117
118 def default(self, o: Any) -> Any: # pylint: disable=too-many-return-statements
119 if isinstance(o, (bytes, bytearray)):
120 return base64.b64encode(o).decode()
121 try:
122 return _datetime_as_isostr(o)
123 except AttributeError:
124 pass
125 return super(AzureJSONEncoder, self).default(o)