update for HEAD-2003050101
[reactos.git] / subsys / system / cmd / locale.c
1 /*
2  *  LOCALE.C - locale handling.
3  *
4  *
5  *  History:
6  *
7  *    09-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
8  *        Started.
9  *
10  *    20-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
11  *        Unicode safe!
12  */
13
14 #include "config.h"
15
16 #include <windows.h>
17 #include <tchar.h>
18 #include <string.h>
19 #include <stdlib.h>
20
21 #include "cmd.h"
22
23
24 TCHAR cDateSeparator;
25 TCHAR cTimeSeparator;
26 TCHAR cThousandSeparator;
27 TCHAR cDecimalSeparator;
28 INT   nDateFormat;
29 INT   nTimeFormat;
30 TCHAR aszDayNames[7][8];
31 INT   nNumberGroups;
32
33
34 VOID InitLocale (VOID)
35 {
36 #ifdef LOCALE_WINDOWS
37         TCHAR szBuffer[256];
38         INT i;
39
40         /* date settings */
41         GetLocaleInfo (LOCALE_USER_DEFAULT, LOCALE_SDATE, szBuffer, 256);
42         CharToOem (szBuffer, szBuffer);
43         cDateSeparator = szBuffer[0];
44         GetLocaleInfo (LOCALE_USER_DEFAULT, LOCALE_IDATE, szBuffer, 256);
45         nDateFormat = _ttoi (szBuffer);
46
47         /* time settings */
48         GetLocaleInfo (LOCALE_USER_DEFAULT, LOCALE_STIME, szBuffer, 256);
49         CharToOem (szBuffer, szBuffer);
50         cTimeSeparator = szBuffer[0];
51         GetLocaleInfo (LOCALE_USER_DEFAULT, LOCALE_ITIME, szBuffer, 256);
52         nTimeFormat = _ttoi (szBuffer);
53
54         /* number settings */
55         GetLocaleInfo (LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, szBuffer, 256);
56         CharToOem (szBuffer, szBuffer);
57         cThousandSeparator = szBuffer[0];
58         GetLocaleInfo (LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, szBuffer, 256);
59         CharToOem (szBuffer, szBuffer);
60         cDecimalSeparator  = szBuffer[0];
61         GetLocaleInfo (LOCALE_USER_DEFAULT, LOCALE_SGROUPING, szBuffer, 256);
62         nNumberGroups = _ttoi (szBuffer);
63
64         /* days of week */
65         for (i = 0; i < 7; i++)
66         {
67                 GetLocaleInfo (LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1 + i, szBuffer, 256);
68                 CharToOem (szBuffer, szBuffer);
69                 _tcscpy (aszDayNames[(i+1)%7], szBuffer); /* little hack */
70         }
71 #endif
72
73 #ifdef LOCALE_GERMAN
74         LPTSTR names [7] = {_T("So"), _T("Mo"), _T("Di"), _T("Mi"), _T("Do"), _T("Fr"), _T("Sa")};
75         INT i;
76
77         /* date settings */
78         cDateSeparator = '.';
79         nDateFormat    = 1;                     /* ddmmyy */
80
81         /* time settings */
82         cTimeSeparator = ':';
83         nTimeFormat    = 1;                     /* 24 hour */
84
85         /* number settings */
86         cThousandSeparator = '.';
87         cDecimalSeparator  = ',';
88         nNumberGroups      = 3;
89
90         /* days of week */
91         for (i = 0; i < 7; i++)
92                 _tcscpy (aszDayNames[i], names[i]);
93 #endif
94
95 #ifdef LOCALE_DEFAULT
96         LPTSTR names [7] = {_T("Sun"), _T("Mon"), _T("Tue"), _T("Wed"), _T("Thu"), _T("Fri"), _T("Sat")};
97         INT i;
98
99         /* date settings */
100         cDateSeparator = '-';
101         nDateFormat = 0;                /* mmddyy */
102
103         /* time settings */
104         cTimeSeparator = ':';
105         nTimeFormat = 0;                /* 12 hour */
106
107         /* number settings */
108         cThousandSeparator = ',';
109         cDecimalSeparator  = '.';
110         nNumberGroups      = 3;
111
112         /* days of week */
113         for (i = 0; i < 7; i++)
114                 _tcscpy (aszDayNames[i], names[i]);
115 #endif
116 }
117
118
119 VOID PrintDate (VOID)
120 {
121 #ifdef __REACTOS__
122         SYSTEMTIME st;
123
124         GetLocalTime (&st);
125
126         switch (nDateFormat)
127         {
128                 case 0: /* mmddyy */
129                 default:
130             ConOutPrintf (_T("%s %02d%c%02d%c%04d"),
131                                           aszDayNames[st.wDayOfWeek], st.wMonth, cDateSeparator, st.wDay, cDateSeparator, st.wYear);
132                         break;
133
134                 case 1: /* ddmmyy */
135             ConOutPrintf (_T("%s %02d%c%02d%c%04d"),
136                                           aszDayNames[st.wDayOfWeek], st.wDay, cDateSeparator, st.wMonth, cDateSeparator, st.wYear);
137                         break;
138
139                 case 2: /* yymmdd */
140             ConOutPrintf (_T("%s %04d%c%02d%c%02d"),
141                                           aszDayNames[st.wDayOfWeek], st.wYear, cDateSeparator, st.wMonth, cDateSeparator, st.wDay);
142                         break;
143         }
144 #else
145         TCHAR szDate[32];
146
147         GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, NULL, NULL,
148                                    szDate, sizeof (szDate));
149         ConOutPrintf (_T("%s"), szDate);
150 #endif
151 }
152
153
154 VOID PrintTime (VOID)
155 {
156 #ifdef __REACTOS__
157         SYSTEMTIME st;
158
159         GetLocalTime (&st);
160
161         switch (nTimeFormat)
162         {
163                 case 0: /* 12 hour format */
164                 default:
165                         ConOutPrintf (_T("Current time is %2d%c%02d%c%02d%c%02d%c\n"),
166                                         (st.wHour == 0 ? 12 : (st.wHour <= 12 ? st.wHour : st.wHour - 12)),
167                                         cTimeSeparator, st.wMinute, cTimeSeparator, st.wSecond, cDecimalSeparator,
168                                         st.wMilliseconds / 10, (st.wHour <= 11 ? 'a' : 'p'));
169                         break;
170
171                 case 1: /* 24 hour format */
172                         ConOutPrintf (_T("Current time is %2d%c%02d%c%02d%c%02d\n"),
173                                         st.wHour, cTimeSeparator, st.wMinute, cTimeSeparator,
174                                         st.wSecond, cDecimalSeparator, st.wMilliseconds / 10);
175                         break;
176         }
177 #else
178     TCHAR szTime[32];
179
180         GetTimeFormat (LOCALE_USER_DEFAULT, 0, NULL, NULL,
181                                    szTime, sizeof (szTime));
182         ConOutPrintf (_T("Current time is: %s\n"), szTime);
183 #endif
184 }