home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************/
- /* */
- /* date.c */
- /* */
- /* Copyright (c) 2000 */
- /* Pasquale J. Villani */
- /* All Rights Reserved */
- /* */
- /* This file is part of CMD32. */
- /* */
- /* CMD32 is free software; you can redistribute it and/or */
- /* modify it under the terms of the GNU General Public License */
- /* as published by the Free Software Foundation; either version */
- /* 2, or (at your option) any later version. */
- /* */
- /* CMD32 is distributed in the hope that it will be useful, but */
- /* WITHOUT ANY WARRANTY; without even the implied warranty of */
- /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See */
- /* the GNU General Public License for more details. */
- /* */
- /* You should have received a copy of the GNU General Public */
- /* License along with CMD32; see the file COPYING. If not, */
- /* write to the Free Software Foundation, 675 Mass Ave, */
- /* Cambridge, MA 02139, USA. */
- /****************************************************************/
-
-
- /* $Logfile$ */
-
- /* $Log$
- * $EndLog$ */
-
- #include <windows.h>
- #include <ctype.h>
- #include "globals.h"
- #include "proto.h"
-
- #ifdef VERSION_STRINGS
- static BYTE *RcsId = "$Header$";
- #endif
-
- static BYTE *day_of_wk[7] =
- { "Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat" };
-
- static INT day_per_mm[2][13] =
- {
- {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 30 },
- {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 30 }
- };
-
-
-
- static BOOL parse_date(BYTE *);
-
-
- BOOL cmd_date(INT argc, BYTE *argv[])
- {
- BYTE date_str[MAX_CMDLINE] = "";
- BOOL DosSetDate();
- BOOL parse_date();
- SYSTEMTIME st;
-
- if(argc == 1)
- {
- BOOL ret;
-
- /* display the date and get new date */
- GetLocalTime((LPSYSTEMTIME) &st);
-
- printf("Current date is %s %d-%d-%d\nEnter date(mm-dd-yy): ",
- day_of_wk[st.wDayOfWeek], st.wMonth, st.wDay, st.wYear);
-
- ret = parse_date(date_str);
- printf("\n");
- return ret;
- }
-
- if(argc == 2)
- {
- printf("\n");
- return parse_date(argv[1]);
- }
- else
- {
- error_message(INV_NUM_PARAMS);
- return FALSE;
- }
- }
-
- BOOL parse_date(s)
- BYTE *s;
- {
- BOOL leap;
- DWORD nRead;
- SYSTEMTIME st;
-
- st.wMonth = st.wDay = st.wYear = 0;
-
- if(*s == '\0')
- {
- ReadFile(hStdin, (LPVOID)s, MAX_CMDLINE, &nRead, 0);
- if(*s == '\0')
- return TRUE;
- }
-
- if(isdigit(*s))
- {
- while(isdigit(*s))
- {
- st.wMonth *= 10;
- st.wMonth += tonum(*s++);
- }
- }
- else
- return FALSE;
- if(*s == '/' || *s == '.' || *s == '-')
- ++s;
- else
- return FALSE;
- if(isdigit(*s))
- {
- while(isdigit(*s))
- {
- st.wDay *= 10;
- st.wDay += tonum(*s++);
- }
- if(*s == '/' || *s == '.' || *s == '-')
- {
- ++s;
-
- if(isdigit(*s))
- {
- while(isdigit(*s))
- {
- st.wYear *= 10;
- st.wYear += tonum(*s++);
- }
- }
- }
- }
-
-
- if((st.wYear >= 0) && (st.wYear <= 99))
- st.wYear = 1900 + st.wYear;
-
- leap = ((st.wYear%4 == 0) && (st.wYear%100 != 0)) || (st.wYear%400 == 0);
- if( !((st.wMonth >= 1 && st.wMonth <= 12) &&
- (st.wDay >=1 && st.wDay <= day_per_mm[leap][st.wMonth]) &&
- (st.wYear >= 1980 && st.wYear <= 2099)) )
- {
- error_message(INV_DATE);
- return FALSE;
- }
- /* all ok set the date */
- return SetLocalTime(&st);
- }
-