A corporation manager and dashboard for EVE Online
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 

67 lignes
2.0 KiB

  1. from datetime import datetime, timedelta
  2. import humanize
  3. __all__ = ["format_isk", "format_isk_compact", "format_utctime",
  4. "format_utctime_compact"]
  5. def format_isk(value):
  6. """Nicely format an ISK value."""
  7. if value < 10**6:
  8. return "{:,.2f}".format(value)
  9. return humanize.intword(value, "%.2f")
  10. def format_isk_compact(value):
  11. """Nicely format an ISK value compactly."""
  12. # Based on humanize.intword().
  13. powers = [10 ** x for x in [3, 6, 9, 12, 15]]
  14. letters = ["k", "m", "b", "t", "q"]
  15. if value < powers[0]:
  16. return "{:,.2f}".format(value)
  17. for ordinal, power in enumerate(powers[1:], 1):
  18. if value < power:
  19. chopped = value / float(powers[ordinal - 1])
  20. return "{:,.2f}{}".format(chopped, letters[ordinal - 1])
  21. return str(value)
  22. def format_utctime(value):
  23. """Format a UTC timestamp."""
  24. return humanize.naturaltime(datetime.utcnow() - value)
  25. def _format_compact_delta(delta):
  26. """Return a snippet of formatting for a time delta."""
  27. # Based on humanize.naturaldelta().
  28. seconds = abs(delta.seconds)
  29. days = abs(delta.days)
  30. years = days // 365
  31. days = days % 365
  32. months = int(days // 30.5)
  33. if years == 0 and days < 1:
  34. if seconds < 60:
  35. return "{}s".format(seconds)
  36. if seconds < 3600:
  37. minutes = seconds // 60
  38. return "{}m".format(minutes)
  39. hours = seconds // 3600
  40. return "{}h".format(hours)
  41. if years == 0:
  42. if months == 0:
  43. return "{}d".format(days)
  44. return "{}mo".format(months)
  45. if years == 1:
  46. if months == 0:
  47. if days == 0:
  48. return "1y"
  49. return "1y {}d".format(days)
  50. return "1y {}mo".format(months)
  51. return "{}y".format(years)
  52. def format_utctime_compact(value):
  53. """Format a UTC timestamp compactly."""
  54. delta = datetime.utcnow() - value
  55. if delta < timedelta(seconds=1):
  56. return "just now"
  57. return "{} ago".format(_format_compact_delta(delta))