Reactos
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
32static 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
132INT cmd_time(LPTSTR param)
133{
134 LPTSTR* arg;
135 INT argc;
136 INT i;
137 INT nTimeString = -1;
138 TCHAR szTime[40];
139
140 if (!_tcsncmp(param, _T("/?"), 2))
141 {
142 ConOutResPaging(TRUE, STRING_TIME_HELP1);
143 return 0;
144 }
145
146 nErrorLevel = 0;
147
148 /* Build the parameter array */
149 arg = split(param, &argc, FALSE, FALSE);
150
151 /* Check for options */
152 for (i = 0; i < argc; i++)
153 {
154 if (bEnableExtensions && (_tcsicmp(arg[i], _T("/T")) == 0))
155 {
156 /* Display current time in short format */
157 SYSTEMTIME st;
158 GetLocalTime(&st);
159 FormatTime(szTime, &st);
160 ConOutPrintf(_T("%s\n"), 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 ConOutResPuts(STRING_TIME_NOW);
172 ConOutPrintf(_T("%s\n"), GetTimeString());
173 }
174
175 while (TRUE)
176 {
177 if (nTimeString == -1)
178 {
179 ConOutResPuts(STRING_TIME_HELP2);
180 ConInString(szTime, ARRAYSIZE(szTime));
181
182 TRACE("\'%s\'\n", debugstr_aw(szTime));
183
184 while (*szTime && szTime[_tcslen(szTime) - 1] < _T(' '))
185 szTime[_tcslen(szTime) - 1] = _T('\0');
186
187 if (ParseTime(szTime))
188 {
189 freep(arg);
190 return 0;
191 }
192 }
193 else
194 {
195 if (ParseTime(arg[nTimeString]))
196 {
197 freep(arg);
198 return 0;
199 }
200
201 /* Force input the next time around */
202 nTimeString = -1;
203 }
204
205 ConErrResPuts(STRING_TIME_ERROR);
206 nErrorLevel = 1;
207 }
208
209 freep(arg);
210 return 0;
211}
212
213#endif