home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************/
- /* */
- /* time.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$ */
-
- #ifdef VERSION_STRINGS
- static char *RcsId = "$Header$";
- #endif
-
- #include <windows.h>
- #include <ctype.h>
- #include "globals.h"
- #include "proto.h"
-
- extern BYTE *tail;
-
- static BOOL parse_time(BYTE *);
-
-
- BOOL cmd_time(INT argc, BYTE *argv[])
- {
- BYTE am_pm;
- BYTE time_in[MAX_CMDLINE] = "";
- WORD wHour;
- SYSTEMTIME st;
-
- if(argc == 1)
- {
- BOOL ret;
-
- /* display the date and get new date */
- GetLocalTime((LPSYSTEMTIME) &st);
- if(st.wHour >=12)
- {
- wHour = st.wHour - 12;
- am_pm = 'p';
- }
- else
- {
- wHour = st.wHour;
- am_pm = 'a';
- }
-
- printf("Current time is %-2d:%02d:%02d.%03d%c\nEnter new time: ",
- wHour == 0 ? 12 : wHour,
- st.wMinute,
- st.wSecond,
- st.wMilliseconds,
- am_pm);
-
- ret = parse_time(time_in);
- printf("\n");
- return ret;
- }
-
- if(argc == 2)
- {
- printf("\n");
- return parse_time(argv[1]);
- }
- else
- {
- error_message(INV_NUM_PARAMS);
- return FALSE;
- }
- }
-
- BOOL parse_time(BYTE *s)
- {
- SYSTEMTIME st;
- BYTE am_pm = ' ';
- DWORD nRead;
-
- st.wHour = 0, st.wMinute = 0, st.wSecond = 0, st.wMilliseconds = 0;
- if(*s == '\0')
- {
- ReadFile(hStdin, (LPVOID)s, MAX_CMDLINE, &nRead, 0);
- if(*s == '\0')
- return TRUE;
- }
-
- if(isdigit(*s))
- {
- while(isdigit(*s))
- {
- st.wHour *= 10;
- st.wHour += tonum(*s++);
- }
- }
- else
- return FALSE;
- if(*s == ':')
- ++s;
- else
- return FALSE;
- if(isdigit(*s))
- {
- while(isdigit(*s))
- {
- st.wMinute *= 10;
- st.wMinute += tonum(*s++);
- }
- if(*s == ':')
- {
- ++s;
-
- if(isdigit(*s))
- {
- while(isdigit(*s))
- {
- st.wSecond *= 10;
- st.wSecond += tonum(*s++);
- }
- }
- if(*s == '.')
- {
- ++s;
-
- if(isdigit(*s))
- {
- while(isdigit(*s))
- {
- st.wMilliseconds *= 10;
- st.wMilliseconds += tonum(*s++);
- }
- }
- }
- }
- }
- if(*s == 'p' || *s == 'P' || *s == 'a' || *s == 'A')
- {
- am_pm = *s++;
- }
- if(*s != '\r' && !(*s == 'm' || *s == 'M'))
- return FALSE;
-
- if((am_pm == ' ' && (st.wHour < 0 || st.wHour > 24)) ||
- (am_pm == 'a' && st.wHour > 12)
- || (am_pm == 'p' && !(st.wHour >= 1 && st.wHour <= 12)))
- return FALSE;
-
- if(st.wMinute < 0 || st.wMinute > 60
- || st.wSecond < 0 || st.wSecond > 60
- || st.wMilliseconds < 0 || st.wMilliseconds > 999 )
- return FALSE;
-
- if(am_pm == 'p' || am_pm == 'P')
- st.wHour += 12;
- if((am_pm == 'a' || am_pm == 'A') && st.wHour == 12)
- st.wHour = 0;
-
- return SetLocalTime(&st);
- }
-