* Sync up to trunk head (r65426).
[reactos.git] / base / shell / 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)
14 * Added locale support.
15 *
16 * 19-Jan-1999 (Eric Kohl)
17 * Unicode and redirection safe!
18 * Added "/t" option.
19 *
20 * 04-Feb-1999 (Eric Kohl)
21 * Fixed time input bug.
22 *
23 * 30-Apr-2005 (Magnus Olsen <magnus@greatlord.com>)
24 * Remove all hardcoded strings in En.rc.
25 */
26
27 #include "precomp.h"
28
29 #ifdef INCLUDE_CMD_TIME
30
31
32 static BOOL ParseTime (LPTSTR s)
33 {
34 SYSTEMTIME t;
35 LPTSTR p = s;
36
37 if (!*s)
38 return TRUE;
39
40 GetLocalTime (&t);
41 t.wHour = 0;
42 t.wMinute = 0;
43 t.wSecond = 0;
44 t.wMilliseconds = 0;
45
46 // first get hour
47 if (_istdigit(*p))
48 {
49 while (_istdigit(*p))
50 {
51 t.wHour = t.wHour * 10 + *p - _T('0');
52 p++;
53 }
54 }
55 else
56 return FALSE;
57
58 // get time separator
59 if (*p != cTimeSeparator)
60 return FALSE;
61 p++;
62
63 // now get minutes
64 if (_istdigit(*p))
65 {
66 while (_istdigit(*p))
67 {
68 t.wMinute = t.wMinute * 10 + *p - _T('0');
69 p++;
70 }
71 }
72 else
73 return FALSE;
74
75 // get time separator
76 if (*p != cTimeSeparator)
77 return FALSE;
78 p++;
79
80 // now get seconds
81 if (_istdigit(*p))
82 {
83 while (_istdigit(*p))
84 {
85 t.wSecond = t.wSecond * 10 + *p - _T('0');
86 p++;
87 }
88 }
89 else
90 return FALSE;
91
92 // get decimal separator
93 if (*p == cDecimalSeparator)
94 {
95 p++;
96
97 // now get hundreths
98 if (_istdigit(*p))
99 {
100 while (_istdigit(*p))
101 {
102 // t.wMilliseconds = t.wMilliseconds * 10 + *p - _T('0');
103 p++;
104 }
105 // t.wMilliseconds *= 10;
106 }
107 }
108
109 /* special case: 12 hour format */
110 if (nTimeFormat == 0)
111 {
112 if (_totupper(*s) == _T('P'))
113 {
114 t.wHour += 12;
115 }
116
117 if ((_totupper(*s) == _T('A')) && (t.wHour == 12))
118 {
119 t.wHour = 0;
120 }
121 }
122
123 if (t.wHour > 23 || t.wMinute > 60 || t.wSecond > 60 || t.wMilliseconds > 999)
124 return FALSE;
125
126 SetLocalTime (&t);
127
128 return TRUE;
129 }
130
131
132 INT cmd_time (LPTSTR param)
133 {
134 LPTSTR *arg;
135 INT argc;
136 INT i;
137 INT nTimeString = -1;
138
139 if (!_tcsncmp (param, _T("/?"), 2))
140 {
141 ConOutResPaging(TRUE,STRING_TIME_HELP1);
142 return 0;
143 }
144
145 nErrorLevel = 0;
146
147 /* build parameter array */
148 arg = split (param, &argc, FALSE, FALSE);
149
150 /* check for options */
151 for (i = 0; i < argc; i++)
152 {
153 if (_tcsicmp (arg[i], _T("/t")) == 0)
154 {
155 /* Display current time in short format */
156 SYSTEMTIME st;
157 TCHAR szTime[20];
158 GetLocalTime(&st);
159 FormatTime(szTime, &st);
160 ConOutPuts(szTime);
161 freep(arg);
162 return 0;
163 }
164
165 if ((*arg[i] != _T('/')) && (nTimeString == -1))
166 nTimeString = i;
167 }
168
169 if (nTimeString == -1)
170 {
171 ConOutResPrintf(STRING_LOCALE_HELP1);
172 ConOutPrintf(_T(": %s\n"), GetTimeString());
173 }
174
175 while (1)
176 {
177 if (nTimeString == -1)
178 {
179 TCHAR s[40];
180
181 ConOutResPuts(STRING_TIME_HELP2);
182
183 ConInString (s, 40);
184
185 TRACE ("\'%s\'\n", debugstr_aw(s));
186
187 while (*s && s[_tcslen (s) - 1] < _T(' '))
188 s[_tcslen(s) - 1] = _T('\0');
189
190 if (ParseTime (s))
191 {
192 freep (arg);
193 return 0;
194 }
195 }
196 else
197 {
198 if (ParseTime (arg[nTimeString]))
199 {
200 freep (arg);
201 return 0;
202 }
203
204 /* force input the next time around. */
205 nTimeString = -1;
206 }
207
208 ConErrResPuts(STRING_TIME_ERROR1);
209 nErrorLevel = 1;
210 }
211
212 freep (arg);
213
214 return 0;
215 }
216
217 #endif