Debian Codegraph @

localeinfo_windows.go

raw symbols SCIP
1 //go:build cgo
2
3 package localeinfo
4
5 /*
6 #define WIN32_LEAN_AND_MEAN
7 #include <Windows.h>
8 */
9 import "C"
10 import (
11 "fmt"
12 "reflect"
13 "runtime"
14 "strings"
15 "sync"
16 "time"
17 "unicode/utf16"
18 "unsafe"
19 )
20
21 var _ Locale = &windowsLocale{}
22
23 type windowsLocale struct {
24 l sync.Mutex
25 buf *uint16
26 locale *C.ushort
27 }
28
29 const bufSize = 1024
30
31 func NewLocale(name string) (Locale, error) {
32 var cLocale *uint16
33 if name == "" {
34 // LOCALE_NAME_USER_DEFAULT is NULL
35 cLocale = nil
36 } else {
37 nameUTF16 := utf16.Encode([]rune(name + "\x00"))
38 cLocale = &nameUTF16[0]
39 }
40 buf := make([]uint16, bufSize)
41 return &windowsLocale{
42 buf: &buf[0],
43 locale: (*C.ushort)(unsafe.Pointer(cLocale)),
44 }, nil
45 }
46
47 func (l *windowsLocale) decode(n int) string {
48 if n == 0 {
49 return ""
50 }
51 var data []uint16
52 sh := (*reflect.SliceHeader)(unsafe.Pointer(&data))
53 sh.Data = uintptr(unsafe.Pointer(l.buf))
54 sh.Len = n - 1
55 sh.Cap = n - 1
56 s := string(utf16.Decode(data))
57 runtime.KeepAlive(l.buf)
58 return s
59 }
60
61 func (l *windowsLocale) localeInfo(t C.ulong) string {
62 l.l.Lock()
63 defer l.l.Unlock()
64 n, err := C.GetLocaleInfoEx(l.locale, t, (*C.ushort)(unsafe.Pointer(l.buf)), bufSize)
65 if err != nil {
66 return ""
67 }
68 return l.decode(int(n))
69 }
70
71 func (l *windowsLocale) localeInfoInt(t C.ulong, def int) int {
72 var n int32
73 _, err := C.GetLocaleInfoEx(l.locale, t|C.LOCALE_RETURN_NUMBER, (*C.ushort)(unsafe.Pointer(&n)), 2)
74 if err != nil {
75 return def
76 }
77 return int(n)
78 }
79
80 func convertDateTimeFormat(fmt string) string {
81 var sb strings.Builder
82 sb.Grow(len(fmt))
83 var current = '\x00'
84 var count = 0
85 escape := -1
86 for i, c := range fmt + "\x00" {
87 if escape >= 0 && c != '\x00' {
88 if c == '\'' {
89 if escape == i-1 {
90 escape = -1
91 sb.WriteRune(c)
92 continue
93 }
94 escape = -1
95 continue
96 }
97 if c == '%' {
98 sb.WriteRune('%')
99 }
100 sb.WriteRune(c)
101 continue
102 }
103 if count > 0 && c == current {
104 count++
105 continue
106 }
107 if count > 0 {
108 if current == 'h' {
109 sb.WriteString("%I")
110 } else if current == 'H' {
111 sb.WriteString("%H")
112 } else if current == 'm' {
113 sb.WriteString("%M")
114 } else if current == 's' {
115 sb.WriteString("%S")
116 } else if current == 't' {
117 sb.WriteString("%p")
118 } else if current == 'd' && count == 1 {
119 // zero-padding is better than space-padding when we want no padding
120 // otherwise we would get eg 01/ 1
121 sb.WriteString("%d")
122 } else if current == 'd' && count == 2 {
123 sb.WriteString("%d")
124 } else if current == 'd' && count == 3 {
125 sb.WriteString("%a")
126 } else if current == 'd' && count == 4 {
127 sb.WriteString("%A")
128 } else if current == 'M' && count <= 2 {
129 sb.WriteString("%m")
130 } else if current == 'M' && count == 3 {
131 sb.WriteString("%b")
132 } else if current == 'M' && count == 4 {
133 sb.WriteString("%B")
134 } else if current == 'y' && count <= 2 {
135 sb.WriteString("%y")
136 } else if current == 'y' && count >= 4 {
137 sb.WriteString("%Y")
138 }
139 count = 0
140 }
141 switch c {
142 case '\x00':
143 return sb.String()
144 case '\'':
145 escape = i
146 case 'd', 'M', 'y', 'h', 'H', 'm', 's', 't':
147 current = c
148 count = 1
149 case '%':
150 sb.WriteString("%%")
151 default:
152 sb.WriteRune(c)
153 }
154 }
155 panic("unreachable")
156 }
157
158 func (l *windowsLocale) Encoding() string {
159 return l.localeInfo(C.LOCALE_SNAME)
160 }
161
162 func (l *windowsLocale) DateTimeFormat() string {
163 // Best-effort, because there is no equivalent of %c.
164 // ReactOS outputs date and time separated by a space, let's do that too.
165 d := l.DateFormat()
166 t := l.TimeFormat()
167 if d == "" || t == "" {
168 return ""
169 }
170 return fmt.Sprintf("%s %s", d, t)
171 }
172
173 func (l *windowsLocale) DateFormat() string {
174 // https://learn.microsoft.com/en-us/windows/win32/intl/day--month--year--and-era-format-pictures
175 return convertDateTimeFormat(l.localeInfo(C.LOCALE_SSHORTDATE))
176 }
177
178 func (l *windowsLocale) TimeFormat() string {
179 // https://learn.microsoft.com/en-us/windows/win32/intl/hour--minute--and-second-format-pictures
180 return convertDateTimeFormat(l.localeInfo(C.LOCALE_STIMEFORMAT))
181 }
182
183 func (l *windowsLocale) AM() string {
184 return l.localeInfo(C.LOCALE_S1159)
185 }
186
187 func (l *windowsLocale) PM() string {
188 return l.localeInfo(C.LOCALE_S2359)
189 }
190
191 func (l *windowsLocale) TimeAMPMFormat() string {
192 // Best-effort: use TimeFormat
193 return l.TimeFormat()
194 }
195
196 var days = []C.ulong{C.LOCALE_SDAYNAME7, C.LOCALE_SDAYNAME1, C.LOCALE_SDAYNAME2, C.LOCALE_SDAYNAME3, C.LOCALE_SDAYNAME4, C.LOCALE_SDAYNAME5, C.LOCALE_SDAYNAME6}
197
198 func (l *windowsLocale) Day(day time.Weekday) string {
199 return l.localeInfo(days[day])
200 }
201
202 var shortDays = []C.ulong{C.LOCALE_SABBREVDAYNAME7, C.LOCALE_SABBREVDAYNAME1, C.LOCALE_SABBREVDAYNAME2, C.LOCALE_SABBREVDAYNAME3, C.LOCALE_SABBREVDAYNAME4, C.LOCALE_SABBREVDAYNAME5, C.LOCALE_SABBREVDAYNAME6}
203
204 func (l *windowsLocale) ShortDay(day time.Weekday) string {
205 return l.localeInfo(shortDays[day])
206 }
207
208 var months = []C.ulong{C.LOCALE_SMONTHNAME1, C.LOCALE_SMONTHNAME2, C.LOCALE_SMONTHNAME3, C.LOCALE_SMONTHNAME4, C.LOCALE_SMONTHNAME5, C.LOCALE_SMONTHNAME6, C.LOCALE_SMONTHNAME7, C.LOCALE_SMONTHNAME8, C.LOCALE_SMONTHNAME9, C.LOCALE_SMONTHNAME10, C.LOCALE_SMONTHNAME11, C.LOCALE_SMONTHNAME12}
209
210 func (l *windowsLocale) Month(month time.Month) string {
211 return l.localeInfo(months[month-1])
212 }
213
214 var shortMonths = []C.ulong{C.LOCALE_SABBREVMONTHNAME1, C.LOCALE_SABBREVMONTHNAME2, C.LOCALE_SABBREVMONTHNAME3, C.LOCALE_SABBREVMONTHNAME4, C.LOCALE_SABBREVMONTHNAME5, C.LOCALE_SABBREVMONTHNAME6, C.LOCALE_SABBREVMONTHNAME7, C.LOCALE_SABBREVMONTHNAME8, C.LOCALE_SABBREVMONTHNAME9, C.LOCALE_SABBREVMONTHNAME10, C.LOCALE_SABBREVMONTHNAME11, C.LOCALE_SABBREVMONTHNAME12}
215
216 func (l *windowsLocale) ShortMonth(month time.Month) string {
217 return l.localeInfo(shortMonths[month-1])
218 }
219
220 func (l *windowsLocale) Radix() string {
221 return l.localeInfo(C.LOCALE_SDECIMAL)
222 }
223
224 func (l *windowsLocale) ThousandSeparator() string {
225 return l.localeInfo(C.LOCALE_STHOUSAND)
226 }
227
228 func (l *windowsLocale) Currency() (symbol string, position CurrencyFormat) {
229 symbol = l.localeInfo(C.LOCALE_SCURRENCY)
230 switch l.localeInfoInt(C.LOCALE_ICURRENCY, 0) {
231 case 0:
232 position = BeforeValue
233 case 1:
234 position = AfterValue
235 case 2:
236 position = BeforeValueSpace
237 case 3:
238 position = AfterValueSpace
239 }
240 return
241 }