Guest User

ISO8601 date comparison

a guest
Sep 25th, 2026
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import matplotlib.patches as mpatches
  3. import matplotlib.ticker as ticker
  4. import numpy as np
  5.  
  6. SECONDS_PER_DAY = 86_400
  7. SECONDS_PER_MONTH = 365.25 * 86_400 / 12 # average ~2,629,800
  8. SECONDS_PER_YEAR = 365.25 * 86_400 # average ~31,557,600
  9.  
  10. D1 = SECONDS_PER_DAY
  11. D10 = 10 * SECONDS_PER_DAY
  12. M1 = SECONDS_PER_MONTH
  13. M10 = 10 * SECONDS_PER_MONTH
  14. Y1 = SECONDS_PER_YEAR
  15. Y10 = 10 * SECONDS_PER_YEAR
  16. Y100 = 100 * SECONDS_PER_YEAR
  17. Y1000 = 1000 * SECONDS_PER_YEAR
  18.  
  19. # Each tuple: (height_in_seconds, display_label, component)
  20. # component: 'y', 'm', 'd'
  21. formats = {
  22. "yyyy-mm-dd (ISO 8601)": [
  23. (Y1000, "y\n×1000", "y"),
  24. (Y100, "y\n×100", "y"),
  25. (Y10, "y\n×10", "y"),
  26. (Y1, "y\n×1", "y"),
  27. (M10, "m\n×10", "m"),
  28. (M1, "m\n×1", "m"),
  29. (D10, "d\n×10", "d"),
  30. (D1, "d\n×1", "d"),
  31. ],
  32. "dd-mm-yyyy": [
  33. (D10, "d\n×10", "d"),
  34. (D1, "d\n×1", "d"),
  35. (M10, "m\n×10", "m"),
  36. (M1, "m\n×1", "m"),
  37. (Y1000, "y\n×1000", "y"),
  38. (Y100, "y\n×100", "y"),
  39. (Y10, "y\n×10", "y"),
  40. (Y1, "y\n×1", "y"),
  41. ],
  42. "mm/dd/yyyy": [
  43. (M10, "m\n×10", "m"),
  44. (M1, "m\n×1", "m"),
  45. (D10, "d\n×10", "d"),
  46. (D1, "d\n×1", "d"),
  47. (Y1000, "y\n×1000", "y"),
  48. (Y100, "y\n×100", "y"),
  49. (Y10, "y\n×10", "y"),
  50. (Y1, "y\n×1", "y"),
  51. ],
  52. }
  53.  
  54. COLORS = {"y": "#1D6FA4", "m": "#1A7A3C", "d": "#C2530A"}
  55.  
  56. def sci_label(v):
  57. exp = int(np.floor(np.log10(v)))
  58. coeff = v / 10**exp
  59. if abs(coeff - 1.0) < 0.05:
  60. return rf"$10^{{{exp}}}$"
  61. return rf"${coeff:.1f}\times10^{{{exp}}}$"
  62.  
  63. def sci_tick(v, _=None):
  64. exp = int(np.floor(np.log10(v)))
  65. return rf"$10^{{{exp}}}$"
  66.  
  67. fig, axes = plt.subplots(1, 3, figsize=(18, 8), sharey=True)
  68. fig.patch.set_facecolor("white")
  69.  
  70. fig.suptitle(
  71. "Date Format Comparison",
  72. fontsize=15, fontweight="bold", color="#111827", y=0.97,
  73. )
  74.  
  75. YMIN, YMAX = 3e4, 1.5e11
  76.  
  77. for ax, (title, bars) in zip(axes, formats.items()):
  78. ax.set_facecolor("#F8FAFC")
  79. ax.spines[:].set_color("#CBD5E1")
  80. ax.tick_params(colors="#1E293B")
  81. ax.yaxis.label.set_color("#1E293B")
  82.  
  83. x = np.arange(8)
  84. heights = [b[0] for b in bars]
  85. labels = [b[1] for b in bars]
  86. comps = [b[2] for b in bars]
  87. bar_colors = [COLORS[c] for c in comps]
  88.  
  89. rects = ax.bar(x, heights, color=bar_colors, edgecolor="white",
  90. linewidth=0.8, zorder=3, width=0.65, alpha=0.88)
  91.  
  92. ax.set_yscale("log")
  93. ax.set_ylim(YMIN, YMAX)
  94. ax.set_xticks(x)
  95. ax.set_xticklabels(labels, fontsize=9.5, color="#1E293B")
  96. ax.set_title(title, fontsize=13, fontweight="bold", color="#111827", pad=10)
  97. ax.grid(axis="y", color="#CBD5E1", linewidth=0.6, zorder=0)
  98.  
  99. # Annotate each bar with scientific notation
  100. for rect, val in zip(rects, heights):
  101. ax.text(
  102. rect.get_x() + rect.get_width() / 2,
  103. val * 2.0,
  104. sci_label(val),
  105. ha="center", va="bottom",
  106. fontsize=8.5, color="#111827", fontweight="bold",
  107. )
  108.  
  109. ax.yaxis.set_major_formatter(ticker.FuncFormatter(sci_tick))
  110. ax.yaxis.set_minor_locator(ticker.NullLocator())
  111.  
  112. if ax == axes[0]:
  113. ax.set_ylabel("Seconds (log scale)", color="#1E293B", fontsize=11)
  114. ax.set_xlabel("Digit position →", color="#64748B", fontsize=9)
  115.  
  116. # Legend
  117. patches = [
  118. mpatches.Patch(color=COLORS["y"], alpha=0.88, label="Year digit"),
  119. mpatches.Patch(color=COLORS["m"], alpha=0.88, label="Month digit"),
  120. mpatches.Patch(color=COLORS["d"], alpha=0.88, label="Day digit"),
  121. ]
  122. fig.legend(
  123. handles=patches,
  124. loc="lower center",
  125. ncol=4,
  126. fontsize=10,
  127. framealpha=1.0,
  128. labelcolor="#111827",
  129. facecolor="white",
  130. edgecolor="#CBD5E1",
  131. bbox_to_anchor=(0.5, 0.01),
  132. )
  133.  
  134. plt.tight_layout(rect=[0, 0.07, 1, 0.96])
  135. plt.savefig("/workspace/date_formats_comparison.png", dpi=150,
  136. bbox_inches="tight", facecolor=fig.get_facecolor())
  137. print("Saved to /workspace/date_formats_comparison.png")
  138.  
Advertisement
Add Comment
Please, Sign In to add comment