Formats a DateTime instance with a given token and locale. :param dt: The instance to format :param token: The token to use :param locale: The locale to use
(self, dt: pendulum.DateTime, token: str, locale: Locale)
| 266 | return result |
| 267 | |
| 268 | def _format_token(self, dt: pendulum.DateTime, token: str, locale: Locale) -> str: |
| 269 | """ |
| 270 | Formats a DateTime instance with a given token and locale. |
| 271 | |
| 272 | :param dt: The instance to format |
| 273 | :param token: The token to use |
| 274 | :param locale: The locale to use |
| 275 | """ |
| 276 | if token in self._DATE_FORMATS: |
| 277 | fmt = locale.get(f"custom.date_formats.{token}") |
| 278 | if fmt is None: |
| 279 | fmt = self._DEFAULT_DATE_FORMATS[token] |
| 280 | |
| 281 | return self.format(dt, fmt, locale) |
| 282 | |
| 283 | if token in self._LOCALIZABLE_TOKENS: |
| 284 | return self._format_localizable_token(dt, token, locale) |
| 285 | |
| 286 | if token in self._TOKENS_RULES: |
| 287 | return self._TOKENS_RULES[token](dt) |
| 288 | |
| 289 | # Timezone |
| 290 | if token in ["ZZ", "Z"]: |
| 291 | if dt.tzinfo is None: |
| 292 | return "" |
| 293 | |
| 294 | separator = ":" if token == "Z" else "" |
| 295 | offset = dt.utcoffset() or datetime.timedelta() |
| 296 | minutes = offset.total_seconds() / 60 |
| 297 | |
| 298 | sign = "+" if minutes >= 0 else "-" |
| 299 | |
| 300 | hour, minute = divmod(abs(int(minutes)), 60) |
| 301 | |
| 302 | return f"{sign}{hour:02d}{separator}{minute:02d}" |
| 303 | |
| 304 | return token |
| 305 | |
| 306 | def _format_localizable_token( |
| 307 | self, dt: pendulum.DateTime, token: str, locale: Locale |
no test coverage detected