一键导入
cldr-format
Guide to understanding how date/time formatting options and locale sequences are structured in the fmt_*.go files.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Guide to understanding how date/time formatting options and locale sequences are structured in the fmt_*.go files.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
| name | cldr-format |
| description | Guide to understanding how date/time formatting options and locale sequences are structured in the fmt_*.go files. |
This skill explains how date and time formatting is structured, routed, and resolved across the fmt_*.go files.
fmt_*.go)Each formatting file handles a specific combination of options. The files are named using a combination of the following letters representing the option fields:
| Character | Option Field | Description |
|---|---|---|
y | Year | Numeric or 2-digit year. |
m | Month | Month width (numeric, 2-digit, long, short, narrow). |
d | Day | Numeric or 2-digit day. |
e | Weekday | Weekday width (long, short, narrow) - corresponds to CLDR E symbol. |
g | Era | Era format (narrow, short, long). |
For example:
Day option.Era and Month.Year, Month, and Day.The entrypoint is NewDateTimeFormat. It checks the default calendar for the locale (cldr.DefaultCalendar(locale)) and returns the appropriate constructor:
gregorianDateTimeFormat(locale, options)persianDateTimeFormat(locale, options)buddhistDateTimeFormat(locale, options)Inside each fmt_*.go file, functions are defined to construct formatting sequences:
seq[OptionFields] (e.g., seqYearMonthDay in fmt_ymd.go)seq[OptionFields]Persian (e.g., seqYearMonthDayPersian in fmt_ymd.go)seq[OptionFields]Buddhist (e.g., seqYearMonthDayBuddhist in fmt_ymd.go)These functions return a *symbols.Seq which holds a slice of formatting symbols.
Each seq... function performs structural dispatching based on language, script, and region:
switch lang block handles language-specific ordering and default separators.switch region) is used if countries have different separator or formatting preferences (e.g., cldr.RegionCL in Spanish).script == cldr.Shaw) are used where script changes the visual pattern representation.Formatting details (like zero-padding or name formats) depend on the requested options width:
opts.Month.numeric(), opts.Day.twoDigit()).symbols.Symbol_dd over opts.Day.symbol()) or change the separators dynamically (e.g., using hyphens instead of slashes when both fields are numeric).symbols.Seq)Formatting is built using a sequence of tokens from internal/symbols/symbols.go:
Symbol_y, Symbol_MM, Symbol_dd, MonthUnit, DayUnit, Symbol_G).'/', '.', '-') and localized texts (e.g., Txt日, Txt일, Txt年).Calling seq.Func() compiles the sequence:
cldr.Text blocks.cldr.YearNumeric(digits), cldr.MonthTwoDigit(digits)) using the locale's numbering system.strings.Builder for zero allocation formatting at runtime.To add support for a new locale:
fmt_*.go file based on which option fields are involved.case cldr.XX: clause to the switch lang block in the appropriate seq* function (and calendar variant if necessary).seq.Add(...) to describe the token sequence. Use the symbol constants for date fields and literal runes or Txt* constants for separators/units.opts.Month.numeric()) to output the correct layout structure.The Go implementation must strictly match the output behavior of JavaScript's Intl.DateTimeFormat (which is backed by ICU/Node.js). In case of discrepancies:
ICU/Node.js is the Source of Truth: Always prioritize matching the exact output of Node.js formatting. Do not override or attempt to make formatting "more correct" if it deviates from what standard Node.js/ICU generates in the tests.
Unsupported Script Fallbacks (e.g., Deseret, Shavian):
Some less common script/locale variants (like en-Dsrt and en-Shaw) are not packaged in standard Node.js/ICU builds, causing them to fall back to the base language (en).
To match this behavior, exclude these locales from the CLDR code generation (in internal/gen/cldr/decoder.go) so that Go's language matcher naturally falls back to en.
Standalone Field Context:
When formatting a field on its own (such as only a weekday or month), ICU/Node.js uses the stand-alone context (Symbol_ccc, Symbol_cccc, Symbol_ccccc / Symbol_LLL, Symbol_LLLL, Symbol_LLLLL) rather than the format context. The weekday formatting sequence (seqWeekday) must map Weekday choices to stand-alone symbols.
CLDR Width Fallback Rules: When a requested width is missing in the CLDR data for a locale, fallback resolution must follow the strict CLDR/ICU width hierarchy:
narrow -> abbreviated -> wideshort -> abbreviated -> wideabbreviated -> wide (Note: abbreviated does not fall back to short).wide -> no fallback.
This resolution must be implemented in the name lookup helpers (e.g., resolveWeekdayIndex in internal/cldr/cldr.go).