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

  1. /****************************************************************/
  2. /*                                */
  3. /*                 date.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.  
  28. /* $Logfile$ */
  29.  
  30. /* $Log$
  31.  * $EndLog$ */
  32.  
  33. #include <windows.h>
  34. #include <ctype.h>
  35. #include "globals.h"
  36. #include "proto.h"
  37.  
  38. #ifdef VERSION_STRINGS
  39. static BYTE *RcsId = "$Header$";
  40. #endif
  41.  
  42. static BYTE *day_of_wk[7] =
  43.     { "Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat" };
  44.  
  45. static INT day_per_mm[2][13] =
  46.     {
  47.         {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 30 },
  48.         {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 30 }
  49.     };
  50.  
  51.  
  52.  
  53. static BOOL parse_date(BYTE *);
  54.  
  55.  
  56. BOOL cmd_date(INT argc, BYTE *argv[])
  57. {
  58.     BYTE date_str[MAX_CMDLINE] = "";
  59.     BOOL DosSetDate();
  60.     BOOL parse_date();
  61.     SYSTEMTIME st;
  62.  
  63.     if(argc == 1)
  64.     {
  65.         BOOL ret;
  66.  
  67.         /* display the date and get new date */
  68.         GetLocalTime((LPSYSTEMTIME) &st); 
  69.  
  70.         printf("Current date is %s %d-%d-%d\nEnter date(mm-dd-yy): ",
  71.         day_of_wk[st.wDayOfWeek], st.wMonth, st.wDay, st.wYear);
  72.  
  73.         ret = parse_date(date_str);
  74.         printf("\n");
  75.         return ret;
  76.     }
  77.  
  78.     if(argc == 2)
  79.     {
  80.         printf("\n");
  81.         return parse_date(argv[1]);
  82.     }
  83.     else
  84.     {
  85.         error_message(INV_NUM_PARAMS);
  86.         return FALSE;
  87.     }
  88. }
  89.  
  90. BOOL parse_date(s)
  91. BYTE *s;
  92. {
  93.     BOOL leap;
  94.     DWORD nRead;
  95.     SYSTEMTIME st;
  96.  
  97.     st.wMonth = st.wDay = st.wYear = 0;
  98.  
  99.     if(*s == '\0')
  100.     {
  101.         ReadFile(hStdin, (LPVOID)s, MAX_CMDLINE, &nRead, 0);
  102.         if(*s == '\0')
  103.             return TRUE;
  104.     }
  105.  
  106.     if(isdigit(*s))
  107.     {
  108.         while(isdigit(*s))
  109.         {
  110.             st.wMonth *= 10;
  111.             st.wMonth += tonum(*s++);
  112.         }
  113.     }
  114.     else
  115.         return FALSE;
  116.     if(*s == '/' || *s == '.' || *s == '-')
  117.         ++s;
  118.     else
  119.         return FALSE;
  120.     if(isdigit(*s))
  121.     {
  122.         while(isdigit(*s))
  123.         {
  124.             st.wDay *= 10;
  125.             st.wDay += tonum(*s++);
  126.         }
  127.         if(*s == '/' || *s == '.' || *s == '-')
  128.         {
  129.             ++s;
  130.  
  131.             if(isdigit(*s))
  132.             {
  133.                 while(isdigit(*s))
  134.                 {
  135.                     st.wYear *= 10;
  136.                     st.wYear += tonum(*s++);
  137.                 }
  138.             }
  139.         }
  140.     }
  141.  
  142.  
  143.     if((st.wYear >= 0) && (st.wYear <= 99))
  144.         st.wYear = 1900 + st.wYear;
  145.  
  146.     leap = ((st.wYear%4 == 0) && (st.wYear%100 != 0)) || (st.wYear%400 == 0);
  147.     if( !((st.wMonth >= 1 && st.wMonth <= 12) &&
  148.         (st.wDay >=1 && st.wDay <= day_per_mm[leap][st.wMonth]) &&
  149.          (st.wYear >= 1980 && st.wYear <= 2099)) )
  150.     {
  151.         error_message(INV_DATE);
  152.         return FALSE;
  153.     }
  154.     /* all ok set the date */
  155.     return SetLocalTime(&st);
  156. }
  157.