home *** CD-ROM | disk | FTP | other *** search
/ Programming Win32 Under the API / ProgrammingWin32UnderTheApiPatVillani.iso / Chapter9 / cmd32 / TIME.C < prev    next >
Encoding:
C/C++ Source or Header  |  2000-04-29  |  3.6 KB  |  180 lines

  1. /****************************************************************/
  2. /*                                */
  3. /*                 time.c                */
  4. /*                                */
  5. /*            Copyright (c) 2000            */
  6. /*            Pasquale J. Villani            */
  7. /*            All Rights Reserved            */
  8. /*                                */
  9. /* This file is part of CMD32.                    */
  10. /*                                */
  11. /* CMD32 is free software; you can redistribute it and/or    */
  12. /* modify it under the terms of the GNU General Public License    */
  13. /* as published by the Free Software Foundation; either version    */
  14. /* 2, or (at your option) any later version.            */
  15. /*                                */
  16. /* CMD32 is distributed in the hope that it will be useful, but    */
  17. /* WITHOUT ANY WARRANTY; without even the implied warranty of    */
  18. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See    */
  19. /* the GNU General Public License for more details.        */
  20. /*                                */
  21. /* You should have received a copy of the GNU General Public    */
  22. /* License along with CMD32; see the file COPYING.  If not,    */
  23. /* write to the Free Software Foundation, 675 Mass Ave,        */
  24. /* Cambridge, MA 02139, USA.                    */
  25. /****************************************************************/
  26.  
  27. /* $Logfile$ */
  28.  
  29. /* $Log$ 
  30.  * $EndLog$ */
  31.  
  32. #ifdef VERSION_STRINGS
  33. static char *RcsId = "$Header$";
  34. #endif
  35.  
  36. #include <windows.h>
  37. #include <ctype.h>
  38. #include "globals.h"
  39. #include "proto.h"
  40.  
  41. extern BYTE *tail;
  42.  
  43. static BOOL parse_time(BYTE *);
  44.  
  45.  
  46. BOOL cmd_time(INT argc, BYTE *argv[])
  47. {
  48.     BYTE am_pm;
  49.     BYTE time_in[MAX_CMDLINE] = "";
  50.     WORD wHour;
  51.     SYSTEMTIME st;
  52.  
  53.     if(argc == 1)
  54.     {
  55.         BOOL ret;
  56.  
  57.         /* display the date and get new date */
  58.         GetLocalTime((LPSYSTEMTIME) &st); 
  59.         if(st.wHour >=12)
  60.         {
  61.             wHour = st.wHour - 12;
  62.             am_pm = 'p';
  63.         }
  64.         else
  65.         {
  66.             wHour = st.wHour;
  67.             am_pm = 'a';
  68.         }
  69.  
  70.         printf("Current time is %-2d:%02d:%02d.%03d%c\nEnter new time: ",
  71.             wHour == 0 ? 12 : wHour,
  72.             st.wMinute,
  73.             st.wSecond,
  74.             st.wMilliseconds,
  75.             am_pm);
  76.  
  77.         ret = parse_time(time_in);
  78.         printf("\n");
  79.         return ret;
  80.     }
  81.  
  82.     if(argc == 2)
  83.     {
  84.         printf("\n");
  85.         return parse_time(argv[1]);
  86.     }
  87.     else
  88.     {
  89.         error_message(INV_NUM_PARAMS);
  90.         return FALSE;
  91.     }
  92. }
  93.  
  94. BOOL parse_time(BYTE *s)
  95. {
  96.     SYSTEMTIME st;
  97.     BYTE am_pm = ' ';
  98.     DWORD nRead;
  99.  
  100.     st.wHour = 0, st.wMinute = 0, st.wSecond = 0, st.wMilliseconds = 0;
  101.     if(*s == '\0')
  102.     {
  103.         ReadFile(hStdin, (LPVOID)s, MAX_CMDLINE, &nRead, 0);
  104.         if(*s == '\0')
  105.             return TRUE;
  106.     }
  107.  
  108.     if(isdigit(*s))
  109.     {
  110.         while(isdigit(*s))
  111.         {
  112.             st.wHour *= 10;
  113.             st.wHour += tonum(*s++);
  114.         }
  115.     }
  116.     else
  117.         return FALSE;
  118.     if(*s == ':')
  119.         ++s;
  120.     else
  121.         return FALSE;
  122.     if(isdigit(*s))
  123.     {
  124.         while(isdigit(*s))
  125.         {
  126.             st.wMinute *= 10;
  127.             st.wMinute += tonum(*s++);
  128.         }
  129.         if(*s == ':')
  130.         {
  131.             ++s;
  132.  
  133.             if(isdigit(*s))
  134.             {
  135.                 while(isdigit(*s))
  136.                 {
  137.                     st.wSecond *= 10;
  138.                     st.wSecond += tonum(*s++);
  139.                 }
  140.             }
  141.             if(*s == '.')
  142.             {
  143.                 ++s;
  144.  
  145.                 if(isdigit(*s))
  146.                 {
  147.                     while(isdigit(*s))
  148.                     {
  149.                         st.wMilliseconds *= 10;
  150.                         st.wMilliseconds += tonum(*s++);
  151.                     }
  152.                 }
  153.             }
  154.         }
  155.     }
  156.     if(*s == 'p' || *s == 'P' || *s == 'a' || *s == 'A')
  157.     {
  158.         am_pm = *s++;
  159.     }
  160.     if(*s != '\r' && !(*s == 'm' || *s == 'M'))
  161.         return FALSE;
  162.  
  163.     if((am_pm == ' ' && (st.wHour < 0 || st.wHour > 24)) ||
  164.         (am_pm == 'a' && st.wHour > 12)
  165.         || (am_pm == 'p' && !(st.wHour >= 1 && st.wHour <= 12)))
  166.             return FALSE;
  167.  
  168.     if(st.wMinute < 0 || st.wMinute > 60
  169.     || st.wSecond < 0 || st.wSecond > 60
  170.     || st.wMilliseconds < 0 || st.wMilliseconds > 999 )
  171.         return FALSE;
  172.  
  173.     if(am_pm == 'p' || am_pm == 'P')
  174.         st.wHour += 12;
  175.     if((am_pm == 'a' || am_pm == 'A') && st.wHour == 12)
  176.         st.wHour = 0;
  177.  
  178.     return SetLocalTime(&st);
  179. }
  180.