| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "strconv" |
| 5 | "strings" |
| 6 | "time" |
| 7 | |
| 8 | "amadan.net/oficina/mapa/internal/mapa/locale" |
| 9 | "amadan.net/oficina/mapa/internal/mapa/markup" |
| 10 | ) |
| 11 | |
| 12 | // Typed dates (level-up §4.6): ⌘K reads "today", "yesterday", |
| 13 | // "tomorrow", a weekday ("friday" is the most recent Friday, today |
| 14 | // included; "next friday" the coming one), "12 nov" or "12 november" |
| 15 | // (this year), and ISO dates as a day. They are resolved on the server, |
| 16 | // counted from today in the viewer's own zone. The words are English; |
| 17 | // a date in numbers is read in the reader's locale's order (build note |
| 18 | // 85): 12/11 is 12 November in en-IE and December 11 in en-US. |
| 19 | |
| 20 | // DateHit is ⌘K's date row: the viewer's own note for a typed day. |
| 21 | type DateHit struct { |
| 22 | Date string `json:"date"` |
| 23 | Label string `json:"label"` |
| 24 | Href string `json:"href"` |
| 25 | // Numeric marks a day typed in numbers, such as 12/11, which might |
| 26 | // as well be a search: the palette lists it after the pages and |
| 27 | // text found, so Enter opens what was searched for (review round 8, |
| 28 | // M1). |
| 29 | Numeric bool `json:"numeric,omitempty"` |
| 30 | } |
| 31 | |
| 32 | // numericQuery reports whether query is a date in numbers (12/11 or |
| 33 | // 12.11.), not words or an ISO date. |
| 34 | func numericQuery(query string) bool { |
| 35 | return strings.ContainsAny(query, "/.") |
| 36 | } |
| 37 | |
| 38 | var weekdays = map[string]time.Weekday{ |
| 39 | "sunday": time.Sunday, "monday": time.Monday, "tuesday": time.Tuesday, |
| 40 | "wednesday": time.Wednesday, "thursday": time.Thursday, |
| 41 | "friday": time.Friday, "saturday": time.Saturday, |
| 42 | "sun": time.Sunday, "mon": time.Monday, "tue": time.Tuesday, |
| 43 | "tues": time.Tuesday, "wed": time.Wednesday, "thu": time.Thursday, |
| 44 | "thur": time.Thursday, "thurs": time.Thursday, "fri": time.Friday, |
| 45 | "sat": time.Saturday, |
| 46 | } |
| 47 | |
| 48 | var months = map[string]time.Month{ |
| 49 | "jan": time.January, "feb": time.February, "mar": time.March, |
| 50 | "apr": time.April, "may": time.May, "jun": time.June, |
| 51 | "jul": time.July, "aug": time.August, "sep": time.September, |
| 52 | "sept": time.September, "oct": time.October, "nov": time.November, |
| 53 | "dec": time.December, "january": time.January, |
| 54 | "february": time.February, "march": time.March, "april": time.April, |
| 55 | "june": time.June, "july": time.July, "august": time.August, |
| 56 | "september": time.September, "october": time.October, |
| 57 | "november": time.November, "december": time.December, |
| 58 | } |
| 59 | |
| 60 | // typedDate is the day query names, as an ISO date, counted from today |
| 61 | // (an ISO date in the viewer's zone), reading numbers in order ("dmy", |
| 62 | // "mdy" or "ymd", locale.Locale.Order); "" when it names none. With a |
| 63 | // separator, a date in numbers must use it unless its year comes first: |
| 64 | // ⌘K passes the locale's, so "1.5" in English is a search, not 1 May |
| 65 | // (review round 8, M1); the picker, a date field, passes "". |
| 66 | func typedDate(query, today, order, separator string) string { |
| 67 | base, err := time.Parse(time.DateOnly, today) |
| 68 | if err != nil { |
| 69 | return "" |
| 70 | } |
| 71 | words := strings.Fields(strings.ToLower(strings.TrimSpace(query))) |
| 72 | switch len(words) { |
| 73 | case 1: |
| 74 | return oneWordDate(words[0], base, order, separator) |
| 75 | case 2: |
| 76 | return twoWordDate(words[0], words[1], base) |
| 77 | } |
| 78 | return "" |
| 79 | } |
| 80 | |
| 81 | func oneWordDate(text string, base time.Time, |
| 82 | order, separator string) string { |
| 83 | switch text { |
| 84 | case "today": |
| 85 | return iso(base) |
| 86 | case "yesterday": |
| 87 | return iso(base.AddDate(0, 0, -1)) |
| 88 | case "tomorrow": |
| 89 | return iso(base.AddDate(0, 0, 1)) |
| 90 | } |
| 91 | if day, ok := weekdays[text]; ok { |
| 92 | back := (int(base.Weekday()) - int(day) + 7) % 7 |
| 93 | return iso(base.AddDate(0, 0, -back)) |
| 94 | } |
| 95 | if markup.IsDate(text) { |
| 96 | return text |
| 97 | } |
| 98 | return numericDate(text, base, order, separator) |
| 99 | } |
| 100 | |
| 101 | // numericDate is a day in numbers, split by / or by . as the locale |
| 102 | // writes it: day and month in order's order, this year unless a |
| 103 | // four-digit year follows; 12.11. as German ends it. A four-digit year |
| 104 | // first is year, month, day anywhere. Anything else is none: a |
| 105 | // two-digit year, a mixed or missing separator, a day that is not one. |
| 106 | func numericDate(text string, base time.Time, |
| 107 | order, only string) string { |
| 108 | separator := "/" |
| 109 | if !strings.Contains(text, separator) { |
| 110 | separator = "." |
| 111 | } |
| 112 | parts := strings.Split(text, separator) |
| 113 | if separator == "." && len(parts) == 3 && parts[2] == "" { |
| 114 | parts = parts[:2] |
| 115 | } |
| 116 | if len(parts) < 2 || len(parts) > 3 || !allDigits(parts) { |
| 117 | return "" |
| 118 | } |
| 119 | yearFirst := len(parts) == 3 && len(parts[0]) == 4 |
| 120 | if only != "" && separator != only && !yearFirst { |
| 121 | return "" |
| 122 | } |
| 123 | year, month, day := strconv.Itoa(base.Year()), parts[0], parts[1] |
| 124 | switch { |
| 125 | case yearFirst: |
| 126 | year, month, day = parts[0], parts[1], parts[2] |
| 127 | case len(parts) == 3 && order == "dmy": |
| 128 | day, month, year = parts[0], parts[1], parts[2] |
| 129 | case len(parts) == 3 && order == "mdy": |
| 130 | year = parts[2] |
| 131 | case len(parts) == 3: |
| 132 | return "" |
| 133 | case order == "dmy": |
| 134 | day, month = parts[0], parts[1] |
| 135 | } |
| 136 | return calendarDate(year, month, day) |
| 137 | } |
| 138 | |
| 139 | // allDigits reports whether every part is ASCII digits, and some. |
| 140 | func allDigits(parts []string) bool { |
| 141 | for _, part := range parts { |
| 142 | if part == "" || strings.Trim(part, "0123456789") != "" { |
| 143 | return false |
| 144 | } |
| 145 | } |
| 146 | return true |
| 147 | } |
| 148 | |
| 149 | // calendarDate is the ISO date for a four-digit year and a month and a |
| 150 | // day of one or two digits, if that day exists. |
| 151 | func calendarDate(year, month, day string) string { |
| 152 | if len(year) != 4 || len(month) > 2 || len(day) > 2 { |
| 153 | return "" |
| 154 | } |
| 155 | whole, _ := strconv.Atoi(year) |
| 156 | number, _ := strconv.Atoi(month) |
| 157 | date, _ := strconv.Atoi(day) |
| 158 | at := time.Date(whole, time.Month(number), date, 0, 0, 0, 0, time.UTC) |
| 159 | if number < 1 || number > 12 || at.Day() != date || |
| 160 | int(at.Month()) != number { |
| 161 | return "" |
| 162 | } |
| 163 | return iso(at) |
| 164 | } |
| 165 | |
| 166 | // twoWordDate is "next friday", "12 nov" or "nov 12". |
| 167 | func twoWordDate(first, second string, base time.Time) string { |
| 168 | if day, ok := weekdays[second]; ok && first == "next" { |
| 169 | ahead := (int(day) - int(base.Weekday()) + 7) % 7 |
| 170 | if ahead == 0 { |
| 171 | ahead = 7 |
| 172 | } |
| 173 | return iso(base.AddDate(0, 0, ahead)) |
| 174 | } |
| 175 | if _, ok := months[first]; ok { |
| 176 | first, second = second, first |
| 177 | } |
| 178 | month, ok := months[second] |
| 179 | number, err := strconv.Atoi(first) |
| 180 | if !ok || err != nil || number < 1 || number > 31 { |
| 181 | return "" |
| 182 | } |
| 183 | day := time.Date(base.Year(), month, number, 0, 0, 0, 0, time.UTC) |
| 184 | if day.Month() != month { |
| 185 | return "" |
| 186 | } |
| 187 | return iso(day) |
| 188 | } |
| 189 | |
| 190 | func iso(day time.Time) string { return day.Format(time.DateOnly) } |
| 191 | |
| 192 | // dateRow is the row for date, labelled as a chip is: "Friday 13 |
| 193 | // November" in en-GB, with the year only when it is not today's. |
| 194 | func dateRow(dates *locale.Locale, date, today string) *DateHit { |
| 195 | if !markup.IsDate(date) { |
| 196 | return nil |
| 197 | } |
| 198 | thisYear, _ := strconv.Atoi(today[:min(4, len(today))]) |
| 199 | return &DateHit{Date: date, Label: dates.Day(date, thisYear), |
| 200 | Href: "/d/" + date} |
| 201 | } |
| 202 | |