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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
{
This file is part of the Free Pascal run time library.
Copyright (c) 2008 by the Free Pascal development team.
Init rtl formating variables based on libc locales
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
{ Initial implementation by petr kristan }
unit clocale;
{$mode objfpc}
interface
{$ifdef localedebug}
// for easier debugging, allows to print untransformed values in test
Type TOrgFormatSettings = record
ShortDateFormat,
LongDateFormat ,
ShortTimeFormat,
LongTimeFormat ,
CurrencyString1,
CurrencyString2: string;
end;
var OrgFormatSettings : TOrgFormatSettings;
{$endif}
implementation
{$linklib c}
Uses
SysUtils, unixtype, initc;
Const
{$if defined(BSD) or defined(SUNOS)}
// Darwin and FreeBSD. Note the lead underscores are added.
{$i clocale.inc}
{$else}
// checked for Linux only, but might be general glibc.
__LC_CTYPE = 0;
__LC_NUMERIC = 1;
__LC_TIME = 2;
__LC_COLLATE = 3;
__LC_MONETARY = 4;
__LC_MESSAGES = 5;
__LC_ALL = 6;
ABDAY_1 = (__LC_TIME shl 16);
DAY_1 = (ABDAY_1)+7;
ABMON_1 = (ABDAY_1)+14;
MON_1 = (ABDAY_1)+26;
AM_STR = (ABDAY_1)+38;
PM_STR = (ABDAY_1)+39;
D_T_FMT = (ABDAY_1)+40;
D_FMT = (ABDAY_1)+41;
T_FMT = (ABDAY_1)+42;
T_FMT_AMPM = (ABDAY_1)+43;
__DECIMAL_POINT = (__LC_NUMERIC shl 16);
RADIXCHAR = __DECIMAL_POINT;
__THOUSANDS_SEP = (__DECIMAL_POINT)+1;
__INT_CURR_SYMBOL = (__LC_MONETARY shl 16);
__CURRENCY_SYMBOL = (__INT_CURR_SYMBOL)+1;
__MON_DECIMAL_POINT = (__INT_CURR_SYMBOL)+2;
__MON_THOUSANDS_SEP = (__INT_CURR_SYMBOL)+3;
__MON_GROUPING = (__INT_CURR_SYMBOL)+4;
__POSITIVE_SIGN = (__INT_CURR_SYMBOL)+5;
__NEGATIVE_SIGN = (__INT_CURR_SYMBOL)+6;
__INT_FRAC_DIGITS = (__INT_CURR_SYMBOL)+7;
__FRAC_DIGITS = (__INT_CURR_SYMBOL)+8;
__P_CS_PRECEDES = (__INT_CURR_SYMBOL)+9;
__P_SEP_BY_SPACE = (__INT_CURR_SYMBOL)+10;
__N_CS_PRECEDES = (__INT_CURR_SYMBOL)+11;
__N_SEP_BY_SPACE = (__INT_CURR_SYMBOL)+12;
__P_SIGN_POSN = (__INT_CURR_SYMBOL)+13;
__N_SIGN_POSN = (__INT_CURR_SYMBOL)+14;
_NL_MONETARY_CRNCYSTR = (__INT_CURR_SYMBOL)+15;
{$endif}
{$ifdef netbsd}
{ NetBSD has a new setlocale function defined in /usr/include/locale.h
that should be used }
function setlocale(category: cint; locale: pchar): pchar; cdecl; external clib name '__setlocale_mb_len_max_32';
{$else}
function setlocale(category: cint; locale: pchar): pchar; cdecl; external clib name 'setlocale';
{$endif}
function nl_langinfo(__item: cint):Pchar;cdecl;external clib name 'nl_langinfo';
procedure GetFormatSettings(out fmts: TFormatSettings);
function GetLocaleStr(item: cint): string;
begin
GetLocaleStr := AnsiString(nl_langinfo(item));
end;
function GetLocaleChar(item: cint): char;
begin
GetLocaleChar := nl_langinfo(item)^;
end;
procedure OmitModifiers(const s: string; var i: integer);
var
l: Integer;
begin
l := Length(s);
//possible flag, with specifier or modifier - glibc exension
while (i<=l) and (s[i] in ['0'..'9', '_', '-', '^', '#', 'E', 'O']) do
inc(i);
end;
function FindSeparator(const s: string; Def: char): char;
var
i: integer;
begin
FindSeparator := Def;
i := Pos('%', s);
if i=0 then
Exit;
inc(i);
OmitModifiers(s, i);
inc(i);
if i<=Length(s) then
FindSeparator := s[i];
end;
function TransformFormatStr(const s: string): string;
var
i, l: integer;
clock12:boolean;
begin
clock12:=false; // should ampm get appended?
TransformFormatStr := '';
i := 1;
l := Length(s);
while i<=l do begin
if s[i]='%' then begin
inc(i);
OmitModifiers(s, i);
if i>l then
Exit;
case s[i] of
'a': TransformFormatStr := TransformFormatStr + 'ddd';
'A': TransformFormatStr := TransformFormatStr + 'dddd';
'b': TransformFormatStr := TransformFormatStr + 'mmm';
'B': TransformFormatStr := TransformFormatStr + 'mmmm';
'c': TransformFormatStr := TransformFormatStr + 'c';
//'C':
'd': TransformFormatStr := TransformFormatStr + 'dd';
'D': TransformFormatStr := TransformFormatStr + 'mm"/"dd"/"yy';
'e': TransformFormatStr := TransformFormatStr + 'd';
'F': TransformFormatStr := TransformFormatStr + 'yyyy-mm-dd';
'g': TransformFormatStr := TransformFormatStr + 'yy';
'G': TransformFormatStr := TransformFormatStr + 'yyyy';
'h': TransformFormatStr := TransformFormatStr + 'mmm';
'H': TransformFormatStr := TransformFormatStr + 'hh';
'I': begin
TransformFormatStr := TransformFormatStr + 'hh';
clock12:=true;
end;
//'j':
'k': TransformFormatStr := TransformFormatStr + 'h';
'l': begin
TransformFormatStr := TransformFormatStr + 'h';
clock12:=true;
end;
'm': TransformFormatStr := TransformFormatStr + 'mm';
'M': TransformFormatStr := TransformFormatStr + 'nn';
'n': TransformFormatStr := TransformFormatStr + sLineBreak;
'p','P':
begin
TransformFormatStr := TransformFormatStr + 'ampm';
clock12:=false;
end;
'r': begin
TransformFormatStr := TransformFormatStr + 'hh:nn:ss';
clock12:=true;
end;
'R': TransformFormatStr := TransformFormatStr + 'hh:nn';
//'s':
'S': TransformFormatStr := TransformFormatStr + 'ss';
't': TransformFormatStr := TransformFormatStr + #9;
'T': TransformFormatStr := TransformFormatStr + 'hh:nn:ss';
//'u':
//'U':
//'V':
//'w':
//'W':
'x': TransformFormatStr := TransformFormatStr + 'ddddd';
'X': TransformFormatStr := TransformFormatStr + 't';
'y': TransformFormatStr := TransformFormatStr + 'yy';
'Y': TransformFormatStr := TransformFormatStr + 'yyyy';
//'z':
//'Z':
'%': TransformFormatStr := TransformFormatStr + '%';
end;
end else
TransformFormatStr := TransformFormatStr + s[i];
inc(i);
end;
i:=length(TransformFormatStr);
if clock12 and (i>0) then
begin
if transformformatstr[i]<>' ' then
TransformFormatStr := TransformFormatStr + ' ';
TransformFormatStr := TransformFormatStr + 'ampm';
end;
end;
const
// sign prec sep
NegFormatsTable: array [0..4, 0..1, 0..1] of byte = (
( (4, 15), (0, 14) ), //Parentheses surround the quantity and currency_symbol
( (5, 8), (1, 9) ), //The sign string precedes the quantity and currency_symbol
( (7, 10), (3, 11) ), //The sign string follows the quantity and currency_symbol
( (6, 13), (1, 9) ), //The sign string immediately precedes the currency_symbol
( (7, 10), (2, 12) ) //The sign string immediately follows the currency_symbol
);
var
i: integer;
prec, sep, signp: byte;
{$if defined(BSD) or defined(SUNOS)}
plocale : plconv;
{$ENDIF}
begin
setlocale(__LC_ALL,'');
for i := 1 to 12 do
begin
fmts.ShortMonthNames[i]:=GetLocaleStr(ABMON_1+i-1);
fmts.LongMonthNames[i]:=GetLocaleStr(MON_1+i-1);
end;
for i := 1 to 7 do
begin
fmts.ShortDayNames[i]:=GetLocaleStr(ABDAY_1+i-1);
fmts.LongDayNames[i]:=GetLocaleStr(DAY_1+i-1);
end;
//Date stuff
fmts.ShortDateFormat := GetLocaleStr(D_FMT);
{$ifdef localedebug}
OrgFormatSettings.ShortDateFormat:=fmts.shortdateformat;
{$endif}
fmts.DateSeparator := FindSeparator(fmts.ShortDateFormat, fmts.DateSeparator);
fmts.ShortDateFormat := TransformFormatStr(fmts.ShortDateFormat);
fmts.LongDateFormat := GetLocaleStr(D_FMT);
{$ifdef localedebug}
OrgFormatSettings.LongDateFormat:=fmts.longdateformat;
{$endif}
fmts.LongDateFormat := TransformFormatStr(fmts.LongDateFormat);
//Time stuff
fmts.TimeAMString := GetLocaleStr(AM_STR);
fmts.TimePMString := GetLocaleStr(PM_STR);
fmts.ShortTimeFormat := GetLocaleStr(T_FMT);
{$ifdef localedebug}
OrgFormatSettings.ShortTimeFormat:=fmts.shorttimeformat;
{$endif}
fmts.TimeSeparator := FindSeparator(fmts.ShortTimeFormat, fmts.TimeSeparator);
fmts.ShortTimeFormat := TransformFormatStr(fmts.ShortTimeFormat);
fmts.LongTimeFormat := GetLocaleStr(T_FMT_AMPM);
{$ifdef localedebug}
OrgFormatSettings.LongTimeFormat:=fmts.longtimeformat;
{$endif}
if (fmts.LongTimeFormat='') then
fmts.LongTimeFormat:=fmts.ShortTimeFormat
else
fmts.LongTimeFormat := TransformFormatStr(fmts.LongTimeFormat);
{$if defined(BSD) or defined(SUNOS)}
plocale:=localeconv;
// for these fields there is a separate BSD derived POSIX function.
if not assigned(plocale) then exit; // for now.
fmts.CurrencyString:=plocale^.currency_symbol; // int_CURR_SYMBOL (in latin chars)
if fmts.CurrencyString='' then
fmts.CurrencyString:=plocale^.int_curr_symbol;
fmts.CurrencyDecimals:=ord(plocale^.FRAC_DIGITS);
{$ifdef localedebug}
OrgFormatSettings.CurrencyString1:=plocale^.currency_symbol;
OrgFormatSettings.CurrencyString2:=plocale^.int_curr_symbol;
{$endif}
prec:=ord(plocale^.P_CS_PRECEDES);
sep:=ord(plocale^.P_SEP_BY_SPACE);
if (prec<=1) and (sep<=1) then
fmts.CurrencyFormat := byte(not boolean(prec)) + sep shl 1;
prec := ord(plocale^.N_CS_PRECEDES);
sep := ord(plocale^.N_SEP_BY_SPACE);
signp := ord(plocale^.N_SIGN_POSN);
if (signp in [0..4]) and (prec in [0, 1]) and (sep in [0, 1]) then
fmts.NegCurrFormat := NegFormatsTable[signp, prec, sep];
//Number stuff
fmts.ThousandSeparator:=plocale^.THOUSANDS_SEP[0];
{$else}
//Currency stuff
fmts.CurrencyString := GetLocaleStr(_NL_MONETARY_CRNCYSTR);
{$ifdef localedebug}
OrgFormatSettings.CurrencyString1:=fmts.currencystring;
OrgFormatSettings.CurrencyString2:='';
{$endif}
fmts.CurrencyString := Copy(fmts.CurrencyString, 2, Length(fmts.CurrencyString));
fmts.CurrencyDecimals := StrToIntDef(GetLocaleStr(__FRAC_DIGITS), fmts.CurrencyDecimals);
prec := byte(GetLocaleChar(__P_CS_PRECEDES));
sep := byte(GetLocaleChar(__P_SEP_BY_SPACE));
if (prec<=1) and (sep<=1) then
fmts.CurrencyFormat := byte(not boolean(prec)) + sep shl 1;
prec := byte(GetLocaleChar(__N_CS_PRECEDES));
sep := byte(GetLocaleChar(__N_SEP_BY_SPACE));
signp := byte(GetLocaleChar(__N_SIGN_POSN));
if (signp in [0..4]) and (prec in [0, 1]) and (sep in [0, 1]) then
fmts.NegCurrFormat := NegFormatsTable[signp, prec, sep];
//Number stuff
fmts.ThousandSeparator:=GetLocaleChar(__THOUSANDS_SEP);
Sep := ord(GetLocaleChar(__MON_THOUSANDS_SEP));
if fmts.ThousandSeparator=#0 then
fmts.ThousandSeparator := char(Sep);
{$endif}
fmts.DecimalSeparator:=GetLocaleChar(RADIXCHAR);
end;
initialization
GetFormatSettings(DefaultFormatSettings);
end.
|