Debian Codegraph @

localeinfo_linux.go

raw symbols SCIP
1 //go:build cgo
2
3 package localeinfo
4
5 /*
6 #include <langinfo.h>
7 #include <locale.h>
8 #include <stdlib.h>
9 */
10 import "C"
11 import (
12 "runtime"
13 "sync"
14 "time"
15 "unsafe"
16 )
17
18 var _ Locale = &linuxLocale{}
19
20 type linuxLocale struct {
21 l sync.Mutex
22 locale C.locale_t
23 }
24
25 // NewLocale tries creating a new Locale from the specified locale name.
26 func NewLocale(name string) (Locale, error) {
27 clLocale := C.CString(name)
28 defer C.free(unsafe.Pointer(clLocale))
29 l, err := C.newlocale(C.LC_ALL_MASK, clLocale, nil)
30 if l == nil {
31 return nil, err
32 }
33 ll := &linuxLocale{
34 locale: l,
35 }
36 runtime.SetFinalizer(ll, func(l *linuxLocale) {
37 C.freelocale(l.locale)
38 })
39 return ll, nil
40 }
41
42 func (l *linuxLocale) Encoding() string {
43 l.l.Lock()
44 defer l.l.Unlock()
45 p := C.nl_langinfo_l(C.CODESET, l.locale)
46 return C.GoString(p)
47 }
48
49 func (l *linuxLocale) DateTimeFormat() string {
50 l.l.Lock()
51 defer l.l.Unlock()
52 p := C.nl_langinfo_l(C.D_T_FMT, l.locale)
53 return C.GoString(p)
54 }
55
56 func (l *linuxLocale) DateFormat() string {
57 l.l.Lock()
58 defer l.l.Unlock()
59 p := C.nl_langinfo_l(C.D_FMT, l.locale)
60 return C.GoString(p)
61 }
62
63 func (l *linuxLocale) TimeFormat() string {
64 l.l.Lock()
65 defer l.l.Unlock()
66 p := C.nl_langinfo_l(C.T_FMT, l.locale)
67 return C.GoString(p)
68 }
69
70 func (l *linuxLocale) AM() string {
71 l.l.Lock()
72 defer l.l.Unlock()
73 p := C.nl_langinfo_l(C.AM_STR, l.locale)
74 return C.GoString(p)
75 }
76
77 func (l *linuxLocale) PM() string {
78 l.l.Lock()
79 defer l.l.Unlock()
80 p := C.nl_langinfo_l(C.PM_STR, l.locale)
81 return C.GoString(p)
82 }
83
84 func (l *linuxLocale) TimeAMPMFormat() string {
85 l.l.Lock()
86 defer l.l.Unlock()
87 p := C.nl_langinfo_l(C.T_FMT_AMPM, l.locale)
88 return C.GoString(p)
89 }
90
91 var days = []C.nl_item{C.DAY_1, C.DAY_2, C.DAY_3, C.DAY_4, C.DAY_5, C.DAY_6, C.DAY_7}
92
93 func (l *linuxLocale) Day(day time.Weekday) string {
94 l.l.Lock()
95 defer l.l.Unlock()
96 p := C.nl_langinfo_l(days[day], l.locale)
97 return C.GoString(p)
98 }
99
100 var shortDays = []C.nl_item{C.ABDAY_1, C.ABDAY_2, C.ABDAY_3, C.ABDAY_4, C.ABDAY_5, C.ABDAY_6, C.ABDAY_7}
101
102 func (l *linuxLocale) ShortDay(day time.Weekday) string {
103 l.l.Lock()
104 defer l.l.Unlock()
105 p := C.nl_langinfo_l(shortDays[day], l.locale)
106 return C.GoString(p)
107 }
108
109 var months = []C.nl_item{C.MON_1, C.MON_2, C.MON_3, C.MON_4, C.MON_5, C.MON_6, C.MON_7, C.MON_8, C.MON_9, C.MON_10, C.MON_11, C.MON_12}
110
111 func (l *linuxLocale) Month(month time.Month) string {
112 l.l.Lock()
113 defer l.l.Unlock()
114 p := C.nl_langinfo_l(months[month-1], l.locale)
115 return C.GoString(p)
116 }
117
118 var shortMonths = []C.nl_item{C.ABMON_1, C.ABMON_2, C.ABMON_3, C.ABMON_4, C.ABMON_5, C.ABMON_6, C.ABMON_7, C.ABMON_8, C.ABMON_9, C.ABMON_10, C.ABMON_11, C.ABMON_12}
119
120 func (l *linuxLocale) ShortMonth(month time.Month) string {
121 l.l.Lock()
122 defer l.l.Unlock()
123 p := C.nl_langinfo_l(shortMonths[month-1], l.locale)
124 return C.GoString(p)
125 }
126
127 func (l *linuxLocale) Radix() string {
128 l.l.Lock()
129 defer l.l.Unlock()
130 p := C.nl_langinfo_l(C.RADIXCHAR, l.locale)
131 return C.GoString(p)
132 }
133
134 func (l *linuxLocale) ThousandSeparator() string {
135 l.l.Lock()
136 defer l.l.Unlock()
137 p := C.nl_langinfo_l(C.THOUSEP, l.locale)
138 return C.GoString(p)
139 }
140
141 func (l *linuxLocale) Currency() (symbol string, position CurrencyFormat) {
142 l.l.Lock()
143 defer l.l.Unlock()
144 p := C.nl_langinfo_l(C.CRNCYSTR, l.locale)
145 v := C.GoString(p)
146 if len(v) > 0 {
147 switch v[0] {
148 case '-':
149 position = BeforeValue
150 case '+':
151 position = AfterValue
152 case '.':
153 position = ReplaceRadix
154 }
155 symbol = v[1:]
156 }
157 return
158 }