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.
 
 
 
 
 

85 lignes
2.6 KiB

  1. from datetime import datetime, timedelta
  2. import humanize
  3. __all__ = [
  4. "format_quantity", "format_isk", "format_isk_compact", "format_utctime",
  5. "format_utctime_compact", "format_security", "get_security_class"
  6. ]
  7. def format_quantity(value):
  8. """Nicely format an integer quantity."""
  9. if value < 10**6:
  10. return "{:,}".format(value)
  11. return humanize.intword(value, "%.2f")
  12. def format_isk(value):
  13. """Nicely format an ISK value."""
  14. if value < 10**6:
  15. return "{:,.2f}".format(value)
  16. return humanize.intword(value, "%.2f")
  17. def format_isk_compact(value):
  18. """Nicely format an ISK value compactly."""
  19. # Based on humanize.intword().
  20. powers = [10 ** x for x in [3, 6, 9, 12, 15]]
  21. letters = ["k", "m", "b", "t", "q"]
  22. if value < powers[0]:
  23. return "{:,.2f}".format(value)
  24. for ordinal, power in enumerate(powers[1:], 1):
  25. if value < power:
  26. chopped = value / float(powers[ordinal - 1])
  27. return "{:,.2f}{}".format(chopped, letters[ordinal - 1])
  28. return str(value)
  29. def format_utctime(value):
  30. """Format a UTC timestamp."""
  31. return humanize.naturaltime(datetime.utcnow() - value)
  32. def _format_compact_delta(delta):
  33. """Return a snippet of formatting for a time delta."""
  34. # Based on humanize.naturaldelta().
  35. seconds = abs(delta.seconds)
  36. days = abs(delta.days)
  37. years = days // 365
  38. days = days % 365
  39. months = int(days // 30.5)
  40. if years == 0 and days < 1:
  41. if seconds < 60:
  42. return "{}s".format(seconds)
  43. if seconds < 3600:
  44. minutes = seconds // 60
  45. return "{}m".format(minutes)
  46. hours = seconds // 3600
  47. return "{}h".format(hours)
  48. if years == 0:
  49. if months == 0:
  50. return "{}d".format(days)
  51. return "{}mo".format(months)
  52. if years == 1:
  53. if months == 0:
  54. if days == 0:
  55. return "1y"
  56. return "1y {}d".format(days)
  57. return "1y {}mo".format(months)
  58. return "{}y".format(years)
  59. def format_utctime_compact(value):
  60. """Format a UTC timestamp compactly."""
  61. delta = datetime.utcnow() - value
  62. if delta < timedelta(seconds=1):
  63. return "just now"
  64. return "{} ago".format(_format_compact_delta(delta))
  65. def format_security(value):
  66. """Given a system security status as a float, return a rounded string."""
  67. return str(round(value, 1))
  68. def get_security_class(value):
  69. """Given a system security status, return the corresponding CSS class."""
  70. if value <= 0.0:
  71. return "sec-null"
  72. return "sec-" + str(round(max(value, 0.1), 1)).replace(".", "_")