Skip to content
C.W.K.
Stream
Lesson 02 of 05 · published

Ask, Do Not Compute

~12 min · provider, timezone, api-design, fallback

Level 0Raw Ore
0 XP0/36 lessons0/12 achievements
0/100 XP to next level100 XP to go0% complete
The session date is a fact the provider already holds. Every attempt to derive it is a re-implementation of somebody else's calendar.

Three ways to get the date, ranked

Once you accept that the machine's calendar is not the market's, there are three candidate fixes, and only the third one holds.

Subtract a fixed offset. "New York is UTC minus five, so subtract five hours before taking the date." This is wrong twice a year in each direction, and the failures cluster exactly where nobody is looking — the week after a daylight saving transition, in one market but not the others.

Consult an exchange calendar. Better, and now you own a table of exchange hours and holidays for three countries, which is a second source of truth that drifts. You will find out it drifted when a market has an unscheduled half-day.

Ask the provider. The quote already carries the moment it was taken and the name of the exchange's timezone. Converting one through the other yields the session date the exchange itself would use. No table, no offsets, no drift — because the answer comes from the same place as the value it describes.

Derive nothing you can be told. If a payload contains the fact, take the fact. Every derivation is a second implementation of somebody else's rules, and it will diverge from theirs on exactly the days those rules are interesting — holidays, transitions, unscheduled closures. The derived version is not only more code, it is more code that is wrong precisely when it matters.

What the implementation actually does

The function is short and the interesting parts are its edges. It reads two fields — the epoch-second timestamp of the last regular-market moment, and the exchange's timezone name — builds an aware datetime in UTC, converts it into the exchange's zone, and takes the date.

Note that it does not convert into the server's zone. The intermediate representation is UTC because that is unambiguous, but the destination is the exchange, because the exchange is who defines what session this is.

The fallback is where honesty lives

The function returns None when the provider is silent — no timestamp, no zone name, or an unparseable one. It does not guess. Its docstring then says callers fall back to the UTC date and say so, rather than inventing a calendar.

That last clause is the whole discipline in miniature. A fallback that is indistinguishable from the real thing is not a fallback, it is a silent downgrade — and downgrades that leave no trace are how a system's accuracy erodes without anybody being able to point at the moment it happened.

When this lesson was first written, that clause was not implemented. The caller substituted a fallback date and stored the provider's original source string unchanged, so a fallback-dated row was byte-identical in provenance to a provider-dated one. The honest half — returning None instead of guessing — was real and load-bearing; the disclosure half lived only in the docstring. A comment describing what the code does is a claim, not evidence. This one had been quoted, believed, and repeated — including by an earlier draft of this lesson.

And on 2026-08-31 it was closed

What closing it looked like teaches more than the gap did. Nobody touched the fallback date — that part was always right. What changed is the source string that travels beside it. The two call paths each carried their own private fallback helper; both were retired and replaced by a single seam both now go through. Provider stated a date: the source is passed along untouched. Provider silent: the UTC date goes out with (date:utc-fallback) appended. That is the entire fix.

Three things are worth taking. First, the fix reached the storage layer. Not a log line, not a metric — the row itself changed, because this store's invariant is provenance on every stored value, and which of the two kinds a date is is provenance. Second, the branch collapsed to one place. Two call sites were falling back independently, so the defect existed in duplicate, and fixing one would have left the other sitting there quietly. Third, the regression test pins both branches — including that a provider-dated row must come back untagged. A test asserting only that the tag appears is also passed by an implementation that tags everything.

The best signal here is how the gap was found. Not by static analysis: it type-checked and the tests passed. It was found by this lesson — by somebody sitting down to teach the codebase, writing out what the docstring promised, and then going to check whether the callers did it. Explanation is a form of verification, and the cheapest audit available for any system is to write down what it says about itself and then read it back against the code, line by line.
Catch narrowly, and know why each one is there. The conversion catches four specific errors — an out-of-range timestamp, an overflow, a bad value, and an unknown timezone name. That is a list somebody thought about, not a bare except. Each entry corresponds to a real thing a provider can send you, and the narrowness means a genuinely unexpected failure still surfaces instead of being flattened into a None.

Code

The session date, asked for — and said so when it cannot be·python
def listing_close_date(info: dict[str, Any]) -> str | None:
    """The session date a quote actually belongs to -- asked of the
    provider, never computed from this machine's clock.

    Measured 2026-08-06 05:49 UTC: a US ETF proxy -> 2026-08-05
    (New York), a Tokyo-listed ETF -> 2026-08-06 (Tokyo). Two
    different session dates at one instant, which is exactly why a
    single local date cannot be right for both.

    Returns None when the provider is silent -- callers then fall
    back to the UTC date and say so -- see `dated_and_sourced`,
    which is what makes "say so" true rather than aspirational."""
    stamp = info.get("regularMarketTime")      # epoch seconds
    zone = info.get("exchangeTimezoneName")    # e.g. "America/New_York"
    if not isinstance(stamp, (int, float)) or not zone:
        return None
    try:
        moment = datetime.datetime.fromtimestamp(
            float(stamp), datetime.UTC)
        return moment.astimezone(
            zoneinfo.ZoneInfo(str(zone))).date().isoformat()
    except (OSError, OverflowError, ValueError,
            zoneinfo.ZoneInfoNotFoundError):
        return None


# The seam that closed the gap, 2026-08-31. Both ingest paths used to
# keep a private `_fallback_date()` and store `result["source"]`
# unchanged either way; both now go through here instead. The date was
# never the bug -- the source string beside it was.
UTC_FALLBACK_TAG = "(date:utc-fallback)"


def dated_and_sourced(result: dict[str, Any]) -> tuple[str, str]:
    """The (data_date, source) pair to store for one provider result."""
    provider_date = result.get("data_date")
    if provider_date:
        return str(provider_date), str(result["source"])
    fallback = datetime.datetime.now(datetime.UTC).date().isoformat()
    return fallback, f"{result['source']} {UTC_FALLBACK_TAG}"


# And the half of the test that is easy to forget: a machine-dated row
# must be tagged, AND a market-dated one must NOT be. Assert only the
# first and an implementation that tags everything still passes.
assert dated_and_sourced(
    {"source": "polygon:AAPL", "data_date": "2026-08-28"}
) == ("2026-08-28", "polygon:AAPL")

External links

Exercise

Find a value your code derives that the upstream payload could have told you directly — a date, a currency, a unit, a status. Replace the derivation with the field, and handle the case where the field is absent by returning an explicit unknown rather than falling through to your derivation. Then check whether any caller silently papers over that unknown; that is usually where the real bug is hiding.
Hint
The giveaway is a constant in your code that encodes somebody else's policy: an offset, a market close time, a rounding rule, a currency assumption. Every one of those is a copy of a fact that lives somewhere authoritative, and copies go stale on the schedule of the original, not yours.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue
💛 by Pippa— warm

Comments 0

🔔 Reply notifications (sign in)
Sign in — Please sign in to comment.

No comments yet — be the first.