Formats the given date (which should be GMT). By default, we return a relative time (e.g., "2 minutes ago"). You can return an absolute date string with ``relative=False``. You can force a full format date ("July 10, 1980") with ``full_format=True``. This m
(
self,
date: Union[int, float, datetime.datetime],
gmt_offset: int = 0,
relative: bool = True,
shorter: bool = False,
full_format: bool = False,
)
| 326 | raise NotImplementedError() |
| 327 | |
| 328 | def format_date( |
| 329 | self, |
| 330 | date: Union[int, float, datetime.datetime], |
| 331 | gmt_offset: int = 0, |
| 332 | relative: bool = True, |
| 333 | shorter: bool = False, |
| 334 | full_format: bool = False, |
| 335 | ) -> str: |
| 336 | """Formats the given date (which should be GMT). |
| 337 | |
| 338 | By default, we return a relative time (e.g., "2 minutes ago"). You |
| 339 | can return an absolute date string with ``relative=False``. |
| 340 | |
| 341 | You can force a full format date ("July 10, 1980") with |
| 342 | ``full_format=True``. |
| 343 | |
| 344 | This method is primarily intended for dates in the past. |
| 345 | For dates in the future, we fall back to full format. |
| 346 | """ |
| 347 | if isinstance(date, (int, float)): |
| 348 | date = datetime.datetime.utcfromtimestamp(date) |
| 349 | now = datetime.datetime.utcnow() |
| 350 | if date > now: |
| 351 | if relative and (date - now).seconds < 60: |
| 352 | # Due to click skew, things are some things slightly |
| 353 | # in the future. Round timestamps in the immediate |
| 354 | # future down to now in relative mode. |
| 355 | date = now |
| 356 | else: |
| 357 | # Otherwise, future dates always use the full format. |
| 358 | full_format = True |
| 359 | local_date = date - datetime.timedelta(minutes=gmt_offset) |
| 360 | local_now = now - datetime.timedelta(minutes=gmt_offset) |
| 361 | local_yesterday = local_now - datetime.timedelta(hours=24) |
| 362 | difference = now - date |
| 363 | seconds = difference.seconds |
| 364 | days = difference.days |
| 365 | |
| 366 | _ = self.translate |
| 367 | format = None |
| 368 | if not full_format: |
| 369 | if relative and days == 0: |
| 370 | if seconds < 50: |
| 371 | return _("1 second ago", "%(seconds)d seconds ago", seconds) % { |
| 372 | "seconds": seconds |
| 373 | } |
| 374 | |
| 375 | if seconds < 50 * 60: |
| 376 | minutes = round(seconds / 60.0) |
| 377 | return _("1 minute ago", "%(minutes)d minutes ago", minutes) % { |
| 378 | "minutes": minutes |
| 379 | } |
| 380 | |
| 381 | hours = round(seconds / (60.0 * 60)) |
| 382 | return _("1 hour ago", "%(hours)d hours ago", hours) % {"hours": hours} |
| 383 | |
| 384 | if days == 0: |
| 385 | format = _("%(time)s") |