|
| 1 | +package scope |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "regexp" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +// LocationFromText extracts a timezone from free-form profile text (the |
| 12 | +// chatter's USER.md, where they record their timezone in their own |
| 13 | +// words) and returns the matching *time.Location, or nil when the text |
| 14 | +// contains no recognizable timezone. |
| 15 | +// |
| 16 | +// The deployment clock is UTC and inbound message timestamps are UTC; |
| 17 | +// USER.md is the only place the chatter's real timezone lives, so this |
| 18 | +// is what lets the agent render "now" and per-message timestamps in the |
| 19 | +// chatter's local time. Recognized forms, in priority order (most |
| 20 | +// explicit first): |
| 21 | +// |
| 22 | +// 1. an IANA name token — "Asia/Shanghai", "America/New_York" |
| 23 | +// (validated with time.LoadLocation, so a stray "他/她" never matches) |
| 24 | +// 2. a UTC/GMT offset — "UTC+8", "GMT+08:00", "UTC-5" |
| 25 | +// 3. a bare signed HH:MM offset — "+08:00", "-05:30" |
| 26 | +// 4. a Chinese zone — "东八区" / "西五区" (东 = east/ahead, 西 = west/behind) |
| 27 | +// 5. a named zone — "北京时间" / "中国标准时间" / "Beijing time" |
| 28 | +// |
| 29 | +// Offset and Chinese forms become a fixed-offset zone (no DST — that's |
| 30 | +// the correct semantics for "the user said UTC+8"); named zones resolve |
| 31 | +// to their IANA location so DST still applies. |
| 32 | +func LocationFromText(text string) *time.Location { |
| 33 | + if strings.TrimSpace(text) == "" { |
| 34 | + return nil |
| 35 | + } |
| 36 | + if loc := ianaFromText(text); loc != nil { |
| 37 | + return loc |
| 38 | + } |
| 39 | + if loc := offsetFromText(text); loc != nil { |
| 40 | + return loc |
| 41 | + } |
| 42 | + if loc := chineseZoneFromText(text); loc != nil { |
| 43 | + return loc |
| 44 | + } |
| 45 | + return namedZoneFromText(text) |
| 46 | +} |
| 47 | + |
| 48 | +// ianaRe matches a "Region/City" token (optionally a three-segment name |
| 49 | +// like "America/Argentina/Salta"). Validation against the tz database is |
| 50 | +// what filters out non-timezone slashes. |
| 51 | +var ianaRe = regexp.MustCompile(`\b([A-Za-z]+(?:_[A-Za-z]+)*(?:/[A-Za-z]+(?:_[A-Za-z]+)*){1,2})\b`) |
| 52 | + |
| 53 | +func ianaFromText(text string) *time.Location { |
| 54 | + for _, m := range ianaRe.FindAllStringSubmatch(text, -1) { |
| 55 | + // "UTC" is handled by the offset branch; skip obvious non-zones |
| 56 | + // fast, but ultimately LoadLocation is the gate. |
| 57 | + if loc, err := time.LoadLocation(m[1]); err == nil { |
| 58 | + return loc |
| 59 | + } |
| 60 | + } |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
| 64 | +// offsetRe matches "UTC+8", "GMT+08:00", "UTC-5", and bare "+08:00". |
| 65 | +// The UTC/GMT prefix is optional, but a bare offset must carry a colon |
| 66 | +// (HH:MM) so we don't grab unrelated numbers like "+8 points". |
| 67 | +var offsetRe = regexp.MustCompile(`(?i)(UTC|GMT)?\s*([+-])\s*(\d{1,2})(?::?([0-5]\d))?`) |
| 68 | + |
| 69 | +func offsetFromText(text string) *time.Location { |
| 70 | + for _, m := range offsetRe.FindAllStringSubmatch(text, -1) { |
| 71 | + prefix := m[1] |
| 72 | + hasMinutes := m[4] != "" |
| 73 | + // Bare sign+digits with no UTC/GMT prefix and no minutes is too |
| 74 | + // ambiguous (could be any signed number) — require either the |
| 75 | + // prefix or an explicit HH:MM. |
| 76 | + if prefix == "" && !hasMinutes { |
| 77 | + continue |
| 78 | + } |
| 79 | + hours, _ := strconv.Atoi(m[3]) |
| 80 | + if hours > 14 { // max real UTC offset is +14 |
| 81 | + continue |
| 82 | + } |
| 83 | + mins := 0 |
| 84 | + if hasMinutes { |
| 85 | + mins, _ = strconv.Atoi(m[4]) |
| 86 | + } |
| 87 | + secs := hours*3600 + mins*60 |
| 88 | + if m[2] == "-" { |
| 89 | + secs = -secs |
| 90 | + } |
| 91 | + return fixedZone(secs) |
| 92 | + } |
| 93 | + return nil |
| 94 | +} |
| 95 | + |
| 96 | +// chineseZoneRe matches "东八区" / "西五区" / "东十二区" etc. 东 (east) is |
| 97 | +// ahead of UTC, 西 (west) is behind. |
| 98 | +var chineseZoneRe = regexp.MustCompile(`([东西])([一二三四五六七八九十]{1,3}|\d{1,2})区`) |
| 99 | + |
| 100 | +func chineseZoneFromText(text string) *time.Location { |
| 101 | + m := chineseZoneRe.FindStringSubmatch(text) |
| 102 | + if m == nil { |
| 103 | + return nil |
| 104 | + } |
| 105 | + n := parseCJKNumeral(m[2]) |
| 106 | + if n < 0 || n > 12 { |
| 107 | + return nil |
| 108 | + } |
| 109 | + secs := n * 3600 |
| 110 | + if m[1] == "西" { |
| 111 | + secs = -secs |
| 112 | + } |
| 113 | + return fixedZone(secs) |
| 114 | +} |
| 115 | + |
| 116 | +// namedZones maps a few well-known colloquial timezone names to IANA |
| 117 | +// locations. Kept short on purpose — broad city-name matching invites |
| 118 | +// false positives; the model is expected to write an IANA name or an |
| 119 | +// offset for anything exotic. |
| 120 | +var namedZones = map[string]string{ |
| 121 | + "北京时间": "Asia/Shanghai", |
| 122 | + "中国标准时间": "Asia/Shanghai", |
| 123 | + "中国时间": "Asia/Shanghai", |
| 124 | + "beijing": "Asia/Shanghai", |
| 125 | +} |
| 126 | + |
| 127 | +func namedZoneFromText(text string) *time.Location { |
| 128 | + lower := strings.ToLower(text) |
| 129 | + for name, iana := range namedZones { |
| 130 | + if strings.Contains(text, name) || strings.Contains(lower, name) { |
| 131 | + if loc, err := time.LoadLocation(iana); err == nil { |
| 132 | + return loc |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + return nil |
| 137 | +} |
| 138 | + |
| 139 | +// fixedZone builds a fixed-offset *time.Location with a readable name |
| 140 | +// like "UTC+08:00" / "UTC-05:30" / "UTC". |
| 141 | +func fixedZone(offsetSecs int) *time.Location { |
| 142 | + name := "UTC" |
| 143 | + if offsetSecs != 0 { |
| 144 | + sign := "+" |
| 145 | + s := offsetSecs |
| 146 | + if s < 0 { |
| 147 | + sign = "-" |
| 148 | + s = -s |
| 149 | + } |
| 150 | + name = fmt.Sprintf("UTC%s%02d:%02d", sign, s/3600, (s%3600)/60) |
| 151 | + } |
| 152 | + return time.FixedZone(name, offsetSecs) |
| 153 | +} |
| 154 | + |
| 155 | +// parseCJKNumeral parses 1–2 digit Arabic numbers and Chinese numerals |
| 156 | +// up to 十二 (12). Returns -1 on anything it can't read. |
| 157 | +func parseCJKNumeral(s string) int { |
| 158 | + if n, err := strconv.Atoi(s); err == nil { |
| 159 | + return n |
| 160 | + } |
| 161 | + digits := map[rune]int{'一': 1, '二': 2, '三': 3, '四': 4, '五': 5, '六': 6, '七': 7, '八': 8, '九': 9} |
| 162 | + runes := []rune(s) |
| 163 | + switch { |
| 164 | + case len(runes) == 1 && runes[0] == '十': |
| 165 | + return 10 |
| 166 | + case len(runes) == 1: |
| 167 | + if d, ok := digits[runes[0]]; ok { |
| 168 | + return d |
| 169 | + } |
| 170 | + case len(runes) == 2 && runes[0] == '十': // 十一, 十二 |
| 171 | + if d, ok := digits[runes[1]]; ok { |
| 172 | + return 10 + d |
| 173 | + } |
| 174 | + case len(runes) == 2 && runes[1] == '十': // 二十 (unlikely for zones) |
| 175 | + if d, ok := digits[runes[0]]; ok { |
| 176 | + return d * 10 |
| 177 | + } |
| 178 | + } |
| 179 | + return -1 |
| 180 | +} |
0 commit comments