comparison libervia/backend/tools/xmpp_datetime.py @ 4270:0d7bb4df2343

Reformatted code base using black.
author Goffi <goffi@goffi.org>
date Wed, 19 Jun 2024 18:44:57 +0200
parents 4b842c1fb686
children
comparison
equal deleted inserted replaced
4269:64a85ce8be70 4270:0d7bb4df2343
27 "format_date", 27 "format_date",
28 "parse_date", 28 "parse_date",
29 "format_datetime", 29 "format_datetime",
30 "parse_datetime", 30 "parse_datetime",
31 "format_time", 31 "format_time",
32 "parse_time" 32 "parse_time",
33 ] 33 ]
34 34
35 35
36 def __parse_fraction_of_a_second(value: str) -> Tuple[str, Optional[int]]: 36 def __parse_fraction_of_a_second(value: str) -> Tuple[str, Optional[int]]:
37 """ 37 """
49 # processing. 49 # processing.
50 match = re.search(r"\.(\d*)", value) 50 match = re.search(r"\.(\d*)", value)
51 microsecond: Optional[int] = None 51 microsecond: Optional[int] = None
52 if match is not None: 52 if match is not None:
53 # Remove the fraction of a second from the input string 53 # Remove the fraction of a second from the input string
54 value = value[:match.start()] + value[match.end():] 54 value = value[: match.start()] + value[match.end() :]
55 55
56 # datetime supports microsecond resolution for the fraction of a second, thus 56 # datetime supports microsecond resolution for the fraction of a second, thus
57 # limit/pad the parsed fraction of a second to six digits 57 # limit/pad the parsed fraction of a second to six digits
58 microsecond = int(match.group(1)[:6].ljust(6, '0')) 58 microsecond = int(match.group(1)[:6].ljust(6, "0"))
59 59
60 return value, microsecond 60 return value, microsecond
61 61
62 62
63 def format_date(value: Optional[date] = None) -> str: 63 def format_date(value: Optional[date] = None) -> str:
90 except ValueError as e: 90 except ValueError as e:
91 raise exceptions.ParsingError() from e 91 raise exceptions.ParsingError() from e
92 92
93 93
94 def format_datetime( 94 def format_datetime(
95 value: Optional[datetime] = None, 95 value: Optional[datetime] = None, include_microsecond: bool = False
96 include_microsecond: bool = False
97 ) -> str: 96 ) -> str:
98 """ 97 """
99 @param value: The datetime to format. Defaults to the current datetime. 98 @param value: The datetime to format. Defaults to the current datetime.
100 must be an aware datetime object (timezone must be specified) 99 must be an aware datetime object (timezone must be specified)
101 @param include_microsecond: Include the microsecond of the datetime in the output. 100 @param include_microsecond: Include the microsecond of the datetime in the output.
182 # The format parsed by time.fromisoformat mostly complies with the XEP-0082 Time 181 # The format parsed by time.fromisoformat mostly complies with the XEP-0082 Time
183 # profile, except that it doesn't handle the letter Z as time zone information for 182 # profile, except that it doesn't handle the letter Z as time zone information for
184 # UTC. This can be fixed with a simple string replacement of 'Z' with "+00:00", which 183 # UTC. This can be fixed with a simple string replacement of 'Z' with "+00:00", which
185 # is another way to represent UTC. 184 # is another way to represent UTC.
186 try: 185 try:
187 result = time.fromisoformat(value.replace('Z', "+00:00")) 186 result = time.fromisoformat(value.replace("Z", "+00:00"))
188 except ValueError as e: 187 except ValueError as e:
189 raise exceptions.ParsingError() from e 188 raise exceptions.ParsingError() from e
190 189
191 if microsecond is not None: 190 if microsecond is not None:
192 result = result.replace(microsecond=microsecond) 191 result = result.replace(microsecond=microsecond)