File: localeinfo_linux.go

package info (click to toggle)
golang-github-delthas-go-localeinfo 0.0~git20240607.b2e834f-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 92 kB
  • sloc: makefile: 2
file content (158 lines) | stat: -rw-r--r-- 3,457 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//go:build cgo

package localeinfo

/*
#include <langinfo.h>
#include <locale.h>
#include <stdlib.h>
*/
import "C"
import (
	"runtime"
	"sync"
	"time"
	"unsafe"
)

var _ Locale = &linuxLocale{}

type linuxLocale struct {
	l      sync.Mutex
	locale C.locale_t
}

// NewLocale tries creating a new Locale from the specified locale name.
func NewLocale(name string) (Locale, error) {
	clLocale := C.CString(name)
	defer C.free(unsafe.Pointer(clLocale))
	l, err := C.newlocale(C.LC_ALL_MASK, clLocale, nil)
	if l == nil {
		return nil, err
	}
	ll := &linuxLocale{
		locale: l,
	}
	runtime.SetFinalizer(ll, func(l *linuxLocale) {
		C.freelocale(l.locale)
	})
	return ll, nil
}

func (l *linuxLocale) Encoding() string {
	l.l.Lock()
	defer l.l.Unlock()
	p := C.nl_langinfo_l(C.CODESET, l.locale)
	return C.GoString(p)
}

func (l *linuxLocale) DateTimeFormat() string {
	l.l.Lock()
	defer l.l.Unlock()
	p := C.nl_langinfo_l(C.D_T_FMT, l.locale)
	return C.GoString(p)
}

func (l *linuxLocale) DateFormat() string {
	l.l.Lock()
	defer l.l.Unlock()
	p := C.nl_langinfo_l(C.D_FMT, l.locale)
	return C.GoString(p)
}

func (l *linuxLocale) TimeFormat() string {
	l.l.Lock()
	defer l.l.Unlock()
	p := C.nl_langinfo_l(C.T_FMT, l.locale)
	return C.GoString(p)
}

func (l *linuxLocale) AM() string {
	l.l.Lock()
	defer l.l.Unlock()
	p := C.nl_langinfo_l(C.AM_STR, l.locale)
	return C.GoString(p)
}

func (l *linuxLocale) PM() string {
	l.l.Lock()
	defer l.l.Unlock()
	p := C.nl_langinfo_l(C.PM_STR, l.locale)
	return C.GoString(p)
}

func (l *linuxLocale) TimeAMPMFormat() string {
	l.l.Lock()
	defer l.l.Unlock()
	p := C.nl_langinfo_l(C.T_FMT_AMPM, l.locale)
	return C.GoString(p)
}

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}

func (l *linuxLocale) Day(day time.Weekday) string {
	l.l.Lock()
	defer l.l.Unlock()
	p := C.nl_langinfo_l(days[day], l.locale)
	return C.GoString(p)
}

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}

func (l *linuxLocale) ShortDay(day time.Weekday) string {
	l.l.Lock()
	defer l.l.Unlock()
	p := C.nl_langinfo_l(shortDays[day], l.locale)
	return C.GoString(p)
}

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}

func (l *linuxLocale) Month(month time.Month) string {
	l.l.Lock()
	defer l.l.Unlock()
	p := C.nl_langinfo_l(months[month-1], l.locale)
	return C.GoString(p)
}

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}

func (l *linuxLocale) ShortMonth(month time.Month) string {
	l.l.Lock()
	defer l.l.Unlock()
	p := C.nl_langinfo_l(shortMonths[month-1], l.locale)
	return C.GoString(p)
}

func (l *linuxLocale) Radix() string {
	l.l.Lock()
	defer l.l.Unlock()
	p := C.nl_langinfo_l(C.RADIXCHAR, l.locale)
	return C.GoString(p)
}

func (l *linuxLocale) ThousandSeparator() string {
	l.l.Lock()
	defer l.l.Unlock()
	p := C.nl_langinfo_l(C.THOUSEP, l.locale)
	return C.GoString(p)
}

func (l *linuxLocale) Currency() (symbol string, position CurrencyFormat) {
	l.l.Lock()
	defer l.l.Unlock()
	p := C.nl_langinfo_l(C.CRNCYSTR, l.locale)
	v := C.GoString(p)
	if len(v) > 0 {
		switch v[0] {
		case '-':
			position = BeforeValue
		case '+':
			position = AfterValue
		case '.':
			position = ReplaceRadix
		}
		symbol = v[1:]
	}
	return
}