Skip to content

Add a ConciseDateFormatter like formatter/locator to time_support#19954

Draft
samaloney wants to merge 5 commits into
astropy:mainfrom
samaloney:feature-concise-time-support
Draft

Add a ConciseDateFormatter like formatter/locator to time_support#19954
samaloney wants to merge 5 commits into
astropy:mainfrom
samaloney:feature-concise-time-support

Conversation

@samaloney

@samaloney samaloney commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Description

This pull request adds functionality similar to matplotlibs ConciseDateFormatter to astropy's time_support best demonstrated by this figure (code below).

Looking for early feedback before go adding tests etc or extending

  • Does it make sense to extend to other formats/longer time scales
  • Tests
  • Changelog
  • The very short time scale could potentially be improved to only show changing portions e.g minutes + seconds
concise_time_example
from matplotlib import pyplot as plt
from astropy.time import Time
from astropy.visualization import time_support

SCALE = "tai" # or "utc" etc

intervals = [
    (">> Years\n(2022–2077)",   "2022-03-22T12:30:30.9", "2077-03-22T12:30:32.1"),
    ("> Year\n(2022–2017)",    "2022-03-22T12:30:30.9", "2023-03-22T12:30:32.1"),
    ("< Year>\n(Mar–Jan)",   "2022-03-22T12:30:30.9", "2023-01-22T12:30:32.1"),
    ("Hours\n(12:30–13:31)", "2022-03-22T12:30:30.9", "2022-03-22T13:31:30.9"),
    ("Seconds\n(12:30:30–40)", "2022-03-22T12:30:30.9", "2022-03-22T12:30:40.9"),
]

MODES = [
    (False,     "simplify=False\n(full strings)"),
    (True,      "simplify=True\n(strip trailing zeros)"),
    ("concise", "simplify='concise'\n(vary-prat + offset)"),
]

fig, axes = plt.subplots(
    nrows=len(INTERVALS),
    ncols=len(MODES),
    figsize=(14, 10),
    constrained_layout=True,
)

fig.suptitle("time_support simplify modes", fontsize=14, fontweight="bold")

for col, (mode, mode_label) in enumerate(MODES):
    axes[0, col].set_title(mode_label, fontsize=10)

for row, (interval_label, t1, t2) in enumerate(INTERVALS):
    axes[row, 0].set_ylabel(interval_label, fontsize=9, rotation=0,
                            labelpad=110, va="center")

    for col, (mode, _) in enumerate(MODES):
        ax = axes[row, col]

        with time_support(simplify=mode, scale=SCALE):
            tstart = Time(t1, scale=SCALE)
            tend   = Time(t2, scale=SCALE)
            tmid   = Time((tstart.mjd + tend.mjd) / 2, format="mjd", scale=SCALE)
            # Plot a simple diagonal line so the axis is populated
            ax.plot(Time([t1, t2], scale=SCALE), [0, 1], color="steelblue", lw=1.5)
            ax.set_xlim(tstart, tend)

        ax.set_yticks([])
        ax.tick_params(axis="x", labelsize=7, rotation=30)
  • By checking this box, the PR author has requested that maintainers do NOT use the "Squash and Merge" button. Maintainers should respect this when possible; however, the final decision is at the discretion of the maintainer that merges the PR.

@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.

  • Do the proposed changes actually accomplish desired goals?
  • Do the proposed changes follow the Astropy coding guidelines?
  • Are tests added/updated as required? If so, do they follow the Astropy testing guidelines?
  • Are docs added/updated as required? If so, do they follow the Astropy documentation guidelines?
  • Is rebase and/or squash necessary? If so, please provide the author with appropriate instructions. Also see instructions for rebase and squash.
  • Did the CI pass? If no, are the failures related? If you need to run daily and weekly cron jobs as part of the PR, please apply the "Extra CI" label. Codestyle issues can be fixed by the bot.
  • Is a change log needed? If yes, did the change log check pass? If no, add the "no-changelog-entry-needed" label. If this is a manual backport, use the "skip-changelog-checks" label unless special changelog handling is necessary.
  • Is this a big PR that makes a "What's new?" entry worthwhile and if so, is (1) a "what's new" entry included in this PR and (2) the "whatsnew-needed" label applied?
  • At the time of adding the milestone, if the milestone set requires a backport to release branch(es), apply the appropriate "backport-X.Y.x" label(s) before merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants