ReactOS  0.4.15-dev-1150-g593bcce
date.c
Go to the documentation of this file.
1 /*
2  * DATE.C - date internal command.
3  *
4  *
5  * History:
6  *
7  * 08 Jul 1998 (John P. Price)
8  * started.
9  *
10  * 20 Jul 1998 (John P. Price)
11  * corrected number of days for December from 30 to 31.
12  * (Thanx to Steffen Kaiser for bug report)
13  *
14  * 27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
15  * added config.h include
16  *
17  * 29-Jul-1998 (Rob Lake)
18  * fixed stand-alone mode.
19  * Added Pacific C compatible dos_getdate functions
20  *
21  * 09-Jan-1999 (Eric Kohl)
22  * Added locale support
23  *
24  * 23-Jan-1999 (Eric Kohl)
25  * Unicode and redirection safe!
26  *
27  * 04-Feb-1999 (Eric Kohl)
28  * Fixed date input bug.
29  *
30  * 03-Apr-2005 (Magnus Olsen <magnus@greatlord.com>)
31  * Remove all hardcoded strings in En.rc
32  */
33 
34 #include "precomp.h"
35 
36 #ifdef INCLUDE_CMD_DATE
37 
38 
39 static WORD awMonths[2][13] =
40 {
41  {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
42  {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
43 };
44 
45 
46 static VOID
48 {
49  switch (nDateFormat)
50  {
51  case 0: /* mmddyy */
52  default:
54  break;
55 
56  case 1: /* ddmmyy */
58  break;
59 
60  case 2: /* yymmdd */
62  break;
63  }
64 }
65 
66 static BOOL
67 ReadNumber (LPTSTR *s, LPWORD lpwValue)
68 {
69  if (_istdigit (**s))
70  {
71  while (_istdigit (**s))
72  {
73  *lpwValue = *lpwValue * 10 + **s - _T('0');
74  (*s)++;
75  }
76  return TRUE;
77  }
78  return FALSE;
79 }
80 
81 static BOOL
83 {
84  if (**s == _T('/') || **s == _T('-') || **s == cDateSeparator)
85  {
86  (*s)++;
87  return TRUE;
88  }
89  return FALSE;
90 }
91 
92 static BOOL
94 {
95  SYSTEMTIME d;
96  unsigned char leap;
97  LPTSTR p = s;
98 
99  if (!*s)
100  return TRUE;
101 
102  GetLocalTime (&d);
103 
104  d.wYear = 0;
105  d.wDay = 0;
106  d.wMonth = 0;
107 
108  switch (nDateFormat)
109  {
110  case 0: /* mmddyy */
111  default:
112  if (!ReadNumber (&p, &d.wMonth))
113  return FALSE;
114  if (!ReadSeparator (&p))
115  return FALSE;
116  if (!ReadNumber (&p, &d.wDay))
117  return FALSE;
118  if (!ReadSeparator (&p))
119  return FALSE;
120  if (!ReadNumber (&p, &d.wYear))
121  return FALSE;
122  break;
123 
124  case 1: /* ddmmyy */
125  if (!ReadNumber (&p, &d.wDay))
126  return FALSE;
127  if (!ReadSeparator (&p))
128  return FALSE;
129  if (!ReadNumber (&p, &d.wMonth))
130  return FALSE;
131  if (!ReadSeparator (&p))
132  return FALSE;
133  if (!ReadNumber (&p, &d.wYear))
134  return FALSE;
135  break;
136 
137  case 2: /* yymmdd */
138  if (!ReadNumber (&p, &d.wYear))
139  return FALSE;
140  if (!ReadSeparator (&p))
141  return FALSE;
142  if (!ReadNumber (&p, &d.wMonth))
143  return FALSE;
144  if (!ReadSeparator (&p))
145  return FALSE;
146  if (!ReadNumber (&p, &d.wDay))
147  return FALSE;
148  break;
149  }
150 
151  /* if only entered two digits: */
152  /* assume 2000's if value less than 80 */
153  /* assume 1900's if value greater or equal 80 */
154  if (d.wYear <= 99)
155  {
156  if (d.wYear >= 80)
157  d.wYear = 1900 + d.wYear;
158  else
159  d.wYear = 2000 + d.wYear;
160  }
161 
162  leap = (!(d.wYear % 4) && (d.wYear % 100)) || !(d.wYear % 400);
163 
164  if ((d.wMonth >= 1 && d.wMonth <= 12) &&
165  (d.wDay >= 1 && d.wDay <= awMonths[leap][d.wMonth]) &&
166  (d.wYear >= 1980 && d.wYear <= 2099))
167  {
168  SetLocalTime (&d);
169  return TRUE;
170  }
171 
172  return FALSE;
173 }
174 
175 
177 {
178  LPTSTR* arg;
179  INT argc;
180  INT i;
181  BOOL bPrompt = TRUE;
182  INT nDateString = -1;
183  TCHAR szDate[40];
184 
185  if (!_tcsncmp(param, _T("/?"), 2))
186  {
188  return 0;
189  }
190 
191  nErrorLevel = 0;
192 
193  /* Build the parameter array */
194  arg = split(param, &argc, FALSE, FALSE);
195 
196  /* Check for options */
197  for (i = 0; i < argc; i++)
198  {
199  if (bEnableExtensions && (_tcsicmp(arg[i], _T("/T")) == 0))
200  bPrompt = FALSE;
201 
202  if ((*arg[i] != _T('/')) && (nDateString == -1))
203  nDateString = i;
204  }
205 
206  if (nDateString == -1)
207  ConOutPrintf(_T("%s"), GetDateString());
208 
209  if (!bPrompt)
210  {
211  freep(arg);
212  return 0;
213  }
214 
215  while (TRUE)
216  {
217  if (nDateString == -1)
218  {
219  PrintDateString();
221 
222  TRACE("\'%s\'\n", debugstr_aw(szDate));
223 
224  while (*szDate && szDate[_tcslen(szDate) - 1] < _T(' '))
225  szDate[_tcslen(szDate) - 1] = _T('\0');
226 
227  if (ParseDate(szDate))
228  {
229  freep(arg);
230  return 0;
231  }
232  }
233  else
234  {
235  if (ParseDate(arg[nDateString]))
236  {
237  freep(arg);
238  return 0;
239  }
240 
241  /* Force input the next time around */
242  nDateString = -1;
243  }
244 
246  nErrorLevel = 1;
247  }
248 
249  freep(arg);
250  return 0;
251 }
252 #endif /* INCLUDE_CMD_DATE */
253 
254 /* EOF */
INT nErrorLevel
Definition: cmd.c:158
static int argc
Definition: ServiceArgs.c:12
#define _istdigit
Definition: tchar.h:1494
#define _tcsicmp
Definition: xmlstorage.h:205
#define TRUE
Definition: types.h:120
#define ConErrResPuts(uID)
Definition: console.h:39
#define debugstr_aw
Definition: precomp.h:43
static VOID ConInString(LPWSTR lpInput, DWORD dwLength)
Definition: label.c:56
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
static WORD awMonths[2][13]
Definition: date.c:39
#define ConOutPrintf(szStr,...)
Definition: console.h:42
void * arg
Definition: msvc.h:10
VOID ConOutResPaging(BOOL StartPaging, UINT resID)
Definition: console.c:182
int32_t INT
Definition: typedefs.h:58
CHAR * LPTSTR
Definition: xmlstorage.h:192
static BOOL ParseDate(LPTSTR s)
Definition: date.c:93
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define FALSE
Definition: types.h:117
unsigned int BOOL
Definition: ntddk_ex.h:94
static VOID PrintDateString(VOID)
Definition: date.c:47
size_t __cdecl _tcslen(const _TCHAR *str)
Definition: tcslen.h:9
static VOID freep(LPSTR *p)
Definition: cmdcons.c:98
VOID WINAPI GetLocalTime(OUT LPSYSTEMTIME lpSystemTime)
Definition: time.c:286
#define STRING_DATE_HELP2
Definition: resource.h:97
char TCHAR
Definition: xmlstorage.h:189
#define _T(x)
Definition: vfdio.h:22
#define TRACE(s)
Definition: solgame.cpp:4
#define d
Definition: ke_i.h:81
INT cmd_date(LPTSTR param)
Definition: date.c:176
GLfloat param
Definition: glext.h:5796
static LPSTR * split(LPSTR s, LPINT args)
Definition: cmdcons.c:163
unsigned short WORD
Definition: ntddk_ex.h:93
#define ConOutResPrintf(uID,...)
Definition: console.h:48
#define STRING_DATE_HELP1
Definition: resource.h:96
static BOOL ReadSeparator(LPTSTR *s)
Definition: date.c:82
BOOL bEnableExtensions
Definition: cmd.c:161
LPTSTR GetDateString(VOID)
Definition: locale.c:58
TCHAR cDateSeparator
Definition: locale.c:16
static const WCHAR szDate[]
Definition: http.c:100
GLdouble s
Definition: gl.h:2039
uint16_t * LPWORD
Definition: typedefs.h:56
#define STRING_DATE_HELP3
Definition: resource.h:98
INT nDateFormat
Definition: locale.c:20
#define STRING_DATE_HELP4
Definition: resource.h:99
GLfloat GLfloat p
Definition: glext.h:8902
#define STRING_DATE_ERROR
Definition: resource.h:39
BOOL WINAPI SetLocalTime(IN CONST SYSTEMTIME *lpSystemTime)
Definition: time.c:356
static BOOL ReadNumber(LPTSTR *s, LPWORD lpwValue)
Definition: date.c:67
int _tcsncmp(const _TCHAR *s1, const _TCHAR *s2, size_t n)
Definition: tcsncmp.h:9