| 1 | package locale |
| 2 | |
| 3 | import ( |
| 4 | "slices" |
| 5 | "strconv" |
| 6 | "strings" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | // moment is the fields a pattern reads: month 1 to 12, weekday 0 for |
| 11 | // Sunday, the hour 0 to 23. |
| 12 | type moment struct { |
| 13 | year, month, day, weekday, hour, minute int |
| 14 | } |
| 15 | |
| 16 | func momentOf(at time.Time) moment { |
| 17 | return moment{year: at.Year(), month: int(at.Month()), day: at.Day(), |
| 18 | weekday: int(at.Weekday()), hour: at.Hour(), minute: at.Minute()} |
| 19 | } |
| 20 | |
| 21 | func padded(value int, pad bool) string { |
| 22 | if pad && value < 10 { |
| 23 | return "0" + strconv.Itoa(value) |
| 24 | } |
| 25 | return strconv.Itoa(value) |
| 26 | } |
| 27 | |
| 28 | // hourOf is the hour as the pattern's cycle counts it. |
| 29 | func hourOf(hour int, cycle string) int { |
| 30 | switch cycle { |
| 31 | case "h12": |
| 32 | if hour%12 == 0 { |
| 33 | return 12 |
| 34 | } |
| 35 | return hour % 12 |
| 36 | case "h11": |
| 37 | return hour % 12 |
| 38 | case "h24": |
| 39 | if hour == 0 { |
| 40 | return 24 |
| 41 | } |
| 42 | } |
| 43 | return hour |
| 44 | } |
| 45 | |
| 46 | // write draws the pattern for at: editor/dates.mjs's formatWith, |
| 47 | // line for line. |
| 48 | func (shape *pattern) write(at moment) string { |
| 49 | var out strings.Builder |
| 50 | for _, part := range shape.Parts { |
| 51 | switch part.Type { |
| 52 | case "lit": |
| 53 | out.WriteString(part.Value) |
| 54 | case "weekday": |
| 55 | out.WriteString(shape.Weekdays[at.weekday]) |
| 56 | case "month": |
| 57 | out.WriteString(shape.Months[at.month-1]) |
| 58 | case "monthNumber": |
| 59 | out.WriteString(padded(at.month, part.Pad)) |
| 60 | case "day": |
| 61 | out.WriteString(padded(at.day, part.Pad)) |
| 62 | case "year": |
| 63 | out.WriteString(strconv.Itoa(at.year)) |
| 64 | case "hour": |
| 65 | out.WriteString(padded(hourOf(at.hour, part.Cycle), part.Pad)) |
| 66 | case "minute": |
| 67 | out.WriteString(padded(at.minute, true)) |
| 68 | case "period": |
| 69 | out.WriteString(shape.Periods[min(at.hour/12, 1)]) |
| 70 | } |
| 71 | } |
| 72 | return out.String() |
| 73 | } |
| 74 | |
| 75 | // Format is one kind of date (dates.json's kinds) for a moment, in its |
| 76 | // own zone. |
| 77 | func (in *Locale) Format(kind string, at time.Time) string { |
| 78 | return in.orDefault().data.Patterns[kind].write(momentOf(at)) |
| 79 | } |
| 80 | |
| 81 | // day reads an ISO date, or reports it is none. |
| 82 | func day(iso string) (time.Time, bool) { |
| 83 | at, err := time.Parse(time.DateOnly, iso) |
| 84 | return at, err == nil && len(iso) == len(time.DateOnly) |
| 85 | } |
| 86 | |
| 87 | // iso formats an ISO date as kind; anything that is no date is shown |
| 88 | // as it is. |
| 89 | func (in *Locale) iso(kind, iso string) string { |
| 90 | at, ok := day(iso) |
| 91 | if !ok { |
| 92 | return iso |
| 93 | } |
| 94 | return in.Format(kind, at) |
| 95 | } |
| 96 | |
| 97 | // Day is a date as a chip shows it: "Thursday 12 November" in en-GB, |
| 98 | // with the year when it is not thisYear. "" for text that is no date. |
| 99 | func (in *Locale) Day(iso string, thisYear int) string { |
| 100 | at, ok := day(iso) |
| 101 | if !ok { |
| 102 | return "" |
| 103 | } |
| 104 | if at.Year() == thisYear { |
| 105 | return in.Format("day", at) |
| 106 | } |
| 107 | return in.Format("dayYear", at) |
| 108 | } |
| 109 | |
| 110 | // Long is an ISO date in full, year and all: "Thursday 12 November |
| 111 | // 2026" in en-GB. |
| 112 | func (in *Locale) Long(iso string) string { return in.iso("dayYear", iso) } |
| 113 | |
| 114 | // DayTitle is a daily note's title, the full date up to its year, which |
| 115 | // YearPart sets apart: together they are Long, written as the locale |
| 116 | // composes it ("martes, 17 de noviembre" and " de 2026"; review round |
| 117 | // 8, L4). |
| 118 | func (in *Locale) DayTitle(iso string) string { |
| 119 | head, _ := in.titleParts(iso) |
| 120 | return head |
| 121 | } |
| 122 | |
| 123 | // YearPart is the rest of the full date from the separator before its |
| 124 | // year: " 2026", ", 2026" in en-US. "" for text that is no date. |
| 125 | func (in *Locale) YearPart(iso string) string { |
| 126 | _, tail := in.titleParts(iso) |
| 127 | return tail |
| 128 | } |
| 129 | |
| 130 | // titleParts cuts dayYear before the literal ahead of its year. A |
| 131 | // pattern whose year comes first is not cut. |
| 132 | func (in *Locale) titleParts(iso string) (string, string) { |
| 133 | at, ok := day(iso) |
| 134 | if !ok { |
| 135 | return iso, "" |
| 136 | } |
| 137 | shape := in.orDefault().data.Patterns["dayYear"] |
| 138 | cut := slices.IndexFunc(shape.Parts, func(part field) bool { |
| 139 | return part.Type == "year" |
| 140 | }) |
| 141 | if cut > 0 && shape.Parts[cut-1].Type == "lit" { |
| 142 | cut-- |
| 143 | } |
| 144 | if cut <= 0 { |
| 145 | return shape.write(momentOf(at)), "" |
| 146 | } |
| 147 | head, tail := *shape, *shape |
| 148 | head.Parts, tail.Parts = shape.Parts[:cut], shape.Parts[cut:] |
| 149 | return head.write(momentOf(at)), tail.write(momentOf(at)) |
| 150 | } |
| 151 | |
| 152 | // Short is a day on a pill: "Thu 12 Nov" in en-GB. |
| 153 | func (in *Locale) Short(iso string) string { return in.iso("short", iso) } |
| 154 | |
| 155 | // Time is a time of day: "21:09", or "9:09 PM" in en-US. |
| 156 | func (in *Locale) Time(at time.Time) string { return in.Format("time", at) } |
| 157 | |
| 158 | // Stamp is a moment in full, for a title: "12 November 2026 at 21:09". |
| 159 | func (in *Locale) Stamp(at time.Time) string { return in.Format("stamp", at) } |
| 160 | |
| 161 | // FirstDay is the day the locale's weeks start on. |
| 162 | func (in *Locale) FirstDay() time.Weekday { |
| 163 | return time.Weekday(in.orDefault().data.FirstDay) |
| 164 | } |
| 165 | |
| 166 | // WeekStart is the first day of the week holding day. |
| 167 | func (in *Locale) WeekStart(day time.Time) time.Time { |
| 168 | back := (int(day.Weekday()) - int(in.FirstDay()) + 7) % 7 |
| 169 | return day.AddDate(0, 0, -back) |
| 170 | } |
| 171 | |
| 172 | // Head is a calendar's column: the day's name, and its letter. |
| 173 | type Head struct{ Name, Letter string } |
| 174 | |
| 175 | // Heads are the week's columns from its first day. |
| 176 | func (in *Locale) Heads() []Head { |
| 177 | in = in.orDefault() |
| 178 | out := make([]Head, 7) |
| 179 | for column := range out { |
| 180 | weekday := (in.data.FirstDay + column) % 7 |
| 181 | out[column] = Head{Name: in.data.Weekdays[weekday], |
| 182 | Letter: in.data.Narrow[weekday]} |
| 183 | } |
| 184 | return out |
| 185 | } |
| 186 | |
| 187 | // Letter is a weekday's narrow name: "M" for Monday in English. |
| 188 | func (in *Locale) Letter(weekday time.Weekday) string { |
| 189 | return in.orDefault().data.Narrow[weekday] |
| 190 | } |
| 191 | |
| 192 | // Order is how the locale writes a date in numbers: "dmy", "mdy" or |
| 193 | // "ymd". |
| 194 | func (in *Locale) Order() string { return in.orDefault().data.Order } |
| 195 | |
| 196 | // Separator is the mark between a date's numbers: "/", or "." in |
| 197 | // German. |
| 198 | func (in *Locale) Separator() string { |
| 199 | return in.orDefault().data.Separator |
| 200 | } |
| 201 | |