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