Copy wininet to branch
[reactos.git] / reactos / subsys / system / cmd / time.c
1 /*
2 * TIME.C - time internal command.
3 *
4 *
5 * History:
6 *
7 * 07/08/1998 (John P. Price)
8 * started.
9 *
10 * 27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
11 * added config.h include
12 *
13 * 09-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
14 * Added locale support.
15 *
16 * 19-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
17 * Unicode and redirection safe!
18 * Added "/t" option.
19 *
20 * 04-Feb-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
21 * Fixed time input bug.
22 */
23
24 #include "precomp.h"
25
26 #ifdef INCLUDE_CMD_TIME
27
28
29 static BOOL ParseTime (LPTSTR s)
30 {
31 SYSTEMTIME t;
32 LPTSTR p = s;
33
34 if (!*s)
35 return TRUE;
36
37 GetLocalTime (&t);
38 t.wHour = 0;
39 t.wMinute = 0;
40 t.wSecond = 0;
41 t.wMilliseconds = 0;
42
43 // first get hour
44 if (_istdigit(*p))
45 {
46 while (_istdigit(*p))
47 {
48 t.wHour = t.wHour * 10 + *p - _T('0');
49 p++;
50 }
51 }
52 else
53 return FALSE;
54
55 // get time separator
56 if (*p != cTimeSeparator)
57 return FALSE;
58 p++;
59
60 // now get minutes
61 if (_istdigit(*p))
62 {
63 while (_istdigit(*p))
64 {
65 t.wMinute = t.wMinute * 10 + *p - _T('0');
66 p++;
67 }
68 }
69 else
70 return FALSE;
71
72 // get time separator
73 if (*p != cTimeSeparator)
74 return FALSE;
75 p++;
76
77 // now get seconds
78 if (_istdigit(*p))
79 {
80 while (_istdigit(*p))
81 {
82 t.wSecond = t.wSecond * 10 + *p - _T('0');
83 p++;
84 }
85 }
86 else
87 return FALSE;
88
89 // get decimal separator
90 if (*p == cDecimalSeparator)
91 {
92 p++;
93
94 // now get hundreths
95 if (_istdigit(*p))
96 {
97 while (_istdigit(*p))
98 {
99 // t.wMilliseconds = t.wMilliseconds * 10 + *p - _T('0');
100 p++;
101 }
102 // t.wMilliseconds *= 10;
103 }
104 }
105
106 /* special case: 12 hour format */
107 if (nTimeFormat == 0)
108 {
109 if (_totupper(*s) == _T('P'))
110 {
111 t.wHour += 12;
112 }
113
114 if ((_totupper(*s) == _T('A')) && (t.wHour == 12))
115 {
116 t.wHour = 0;
117 }
118 }
119
120 if (t.wHour > 23 || t.wMinute > 60 || t.wSecond > 60 || t.wMilliseconds > 999)
121 return FALSE;
122
123 SetLocalTime (&t);
124
125 return TRUE;
126 }
127
128
129 INT cmd_time (LPTSTR cmd, LPTSTR param)
130 {
131 LPTSTR *arg;
132 INT argc;
133 INT i;
134 BOOL bPrompt = TRUE;
135 INT nTimeString = -1;
136
137 if (!_tcsncmp (param, _T("/?"), 2))
138 {
139 ConOutPuts (_T("Displays or sets the system time.\n"
140 "\n"
141 "TIME [/T][time]\n"
142 "\n"
143 " /T display only\n"
144 "\n"
145 "Type TIME with no parameters to display the current time setting and a prompt\n"
146 "for a new one. Press ENTER to keep the same time."));
147 return 0;
148 }
149
150 /* build parameter array */
151 arg = split (param, &argc, FALSE);
152
153 /* check for options */
154 for (i = 0; i < argc; i++)
155 {
156 if (_tcsicmp (arg[i], _T("/t")) == 0)
157 bPrompt = FALSE;
158
159 if ((*arg[i] != _T('/')) && (nTimeString == -1))
160 nTimeString = i;
161 }
162
163 if (nTimeString == -1)
164 PrintTime ();
165
166 if (!bPrompt)
167 {
168 freep (arg);
169 return 0;
170 }
171
172 while (1)
173 {
174 if (nTimeString == -1)
175 {
176 TCHAR s[40];
177
178 ConOutPrintf (_T("Enter new time: "));
179
180 ConInString (s, 40);
181
182 #ifdef _DEBUG
183 DebugPrintf (_T("\'%s\'\n"), s);
184 #endif
185
186 while (*s && s[_tcslen (s) - 1] < _T(' '))
187 s[_tcslen(s) - 1] = _T('\0');
188
189 if (ParseTime (s))
190 {
191 freep (arg);
192 return 0;
193 }
194 }
195 else
196 {
197 if (ParseTime (arg[nTimeString]))
198 {
199 freep (arg);
200 return 0;
201 }
202
203 /* force input the next time around. */
204 nTimeString = -1;
205 }
206 ConErrPuts (_T("Invalid time."));
207 }
208
209 freep (arg);
210
211 return 0;
212 }
213
214 #endif