CodeVetter · Repo Unpack · Behavior traces
iamkun/dayjs
Eight traces follow concrete public operations through their implementation branches. They cover initial construction, calendar manipulation, locale registration, strict parsing, timezone parsing/conversion, durations, and serialization. Each trace describes source execution order rather than an observed runtime result. Persistence here is the caller-facing serialization boundary.
- Construction: module initialization loads English → dayjs('2024-03-20') attaches configuration → Dayjs constructor selects $L → parse calls parseDate → init caches local fields → constructor assigns $x and the Dayjs marker.
- Month arithmetic/display: dayjs('2024-01-31').add(1, 'month') → prettyUnit → set → clone → $set moves to day 1 and clamps to destination daysInMonth → init → format replaces YYYY/MM/DD tokens; the core setter leaves the original date intact.
- Locale display: importing zh-cn calls dayjs.locale(locale, null, true) → parseLocale registers the object without selecting it globally → instance.locale('zh-cn') clones and changes $L → format reads translated month/day/meridiem data.
- Strict parsing: extend(customParseFormat) installs its parse wrapper → dayjs(input, format, true) preserves arguments → parseFormattedInput calls makeParser and constructs a Date → init → format(format) comparison invalidates mismatches; format arrays recursively try candidates until one is valid.
- Named-zone construction: with UTC installed, dayjs.tz(string, zone) → tzOffset(now, zone) chooses an initial offset → dayjs.utc(string, optionalFormat).valueOf() obtains wall-clock fields → fixOffset reconciles guesses or a DST gap → dayjs(targetTs).utcOffset(targetOffset) → stores $timezone; invalid parsed input returns an invalid instance.
- Zone conversion: instance.tz(zone, keepLocalTime) returns immediately for invalid state → toDate → toLocaleString and tzOffset → UTC conversion for zero offset or localized reconstruction otherwise → restore milliseconds and set offset → optionally add oldOffset-newOffset → attach $timezone.
- Duration arithmetic: dayjs.duration(object) captures the global locale → Duration normalizes component keys and calculates $ms → date.add(duration) detects Duration → manipulateDuration applies years, months, days, hours, minutes, seconds, and milliseconds in order; humanize separately calls fromNow and therefore needs relativeTime.
- Serialization: JSON.stringify(instance) reaches toJSON → isValid → toISOString for valid state or null otherwise; UTC overrides toISOString to use toDate, which calls offset-adjusted valueOf before native ISO serialization.