Mercurial > libervia-backend
comparison sat/tools/xmpp_datetime.py @ 3933:cecf45416403
plugin XEP-0373 and XEP-0374: Implementation of OX and OXIM:
GPGME is used as the GPG provider.
rel 374
author | Syndace <me@syndace.dev> |
---|---|
date | Tue, 20 Sep 2022 16:22:18 +0200 |
parents | 8289ac1b34f4 |
children |
comparison
equal
deleted
inserted
replaced
3932:7af29260ecb8 | 3933:cecf45416403 |
---|---|
78 def parse_date(value: str) -> date: | 78 def parse_date(value: str) -> date: |
79 """ | 79 """ |
80 @param value: A string containing date information formatted according to the Date | 80 @param value: A string containing date information formatted according to the Date |
81 profile specified in XEP-0082. | 81 profile specified in XEP-0082. |
82 @return: The date parsed from the input string. | 82 @return: The date parsed from the input string. |
83 @raise ValueError: if the input string is not correctly formatted. | 83 @raise exceptions.ParsingError: if the input string is not correctly formatted. |
84 """ | 84 """ |
85 # CCYY-MM-DD | 85 # CCYY-MM-DD |
86 | 86 |
87 # The Date profile of XEP-0082 is equal to the ISO 8601 format. | 87 # The Date profile of XEP-0082 is equal to the ISO 8601 format. |
88 return date.fromisoformat(value) | 88 try: |
89 return date.fromisoformat(value) | |
90 except ValueError as e: | |
91 raise exceptions.ParsingError() from e | |
89 | 92 |
90 | 93 |
91 def format_datetime( | 94 def format_datetime( |
92 value: Optional[datetime] = None, | 95 value: Optional[datetime] = None, |
93 include_microsecond: bool = False | 96 include_microsecond: bool = False |
123 def parse_datetime(value: str) -> datetime: | 126 def parse_datetime(value: str) -> datetime: |
124 """ | 127 """ |
125 @param value: A string containing datetime information formatted according to the | 128 @param value: A string containing datetime information formatted according to the |
126 DateTime profile specified in XEP-0082. | 129 DateTime profile specified in XEP-0082. |
127 @return: The datetime parsed from the input string. | 130 @return: The datetime parsed from the input string. |
128 @raise ValueError: if the input string is not correctly formatted. | 131 @raise exceptions.ParsingError: if the input string is not correctly formatted. |
129 """ | 132 """ |
130 # CCYY-MM-DDThh:mm:ss[.sss]TZD | 133 # CCYY-MM-DDThh:mm:ss[.sss]TZD |
131 | 134 |
132 value, microsecond = __parse_fraction_of_a_second(value) | 135 value, microsecond = __parse_fraction_of_a_second(value) |
133 | 136 |
134 result = datetime.strptime(value, "%Y-%m-%dT%H:%M:%S%z") | 137 try: |
138 result = datetime.strptime(value, "%Y-%m-%dT%H:%M:%S%z") | |
139 except ValueError as e: | |
140 raise exceptions.ParsingError() from e | |
135 | 141 |
136 if microsecond is not None: | 142 if microsecond is not None: |
137 result = result.replace(microsecond=microsecond) | 143 result = result.replace(microsecond=microsecond) |
138 | 144 |
139 return result | 145 return result |
165 def parse_time(value: str) -> time: | 171 def parse_time(value: str) -> time: |
166 """ | 172 """ |
167 @param value: A string containing time information formatted according to the Time | 173 @param value: A string containing time information formatted according to the Time |
168 profile specified in XEP-0082. | 174 profile specified in XEP-0082. |
169 @return: The time parsed from the input string. | 175 @return: The time parsed from the input string. |
170 @raise ValueError: if the input string is not correctly formatted. | 176 @raise exceptions.ParsingError: if the input string is not correctly formatted. |
171 """ | 177 """ |
172 # hh:mm:ss[.sss][TZD] | 178 # hh:mm:ss[.sss][TZD] |
173 | 179 |
174 value, microsecond = __parse_fraction_of_a_second(value) | 180 value, microsecond = __parse_fraction_of_a_second(value) |
175 | 181 |
176 # The format parsed by time.fromisoformat mostly complies with the XEP-0082 Time | 182 # The format parsed by time.fromisoformat mostly complies with the XEP-0082 Time |
177 # profile, except that it doesn't handle the letter Z as time zone information for | 183 # profile, except that it doesn't handle the letter Z as time zone information for |
178 # UTC. This can be fixed with a simple string replacement of 'Z' with "+00:00", which | 184 # UTC. This can be fixed with a simple string replacement of 'Z' with "+00:00", which |
179 # is another way to represent UTC. | 185 # is another way to represent UTC. |
180 result = time.fromisoformat(value.replace('Z', "+00:00")) | 186 try: |
187 result = time.fromisoformat(value.replace('Z', "+00:00")) | |
188 except ValueError as e: | |
189 raise exceptions.ParsingError() from e | |
181 | 190 |
182 if microsecond is not None: | 191 if microsecond is not None: |
183 result = result.replace(microsecond=microsecond) | 192 result = result.replace(microsecond=microsecond) |
184 | 193 |
185 return result | 194 return result |