home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / ISDST.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  3KB  |  117 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  ISDST.C - Determine is a date falls within Daylight Savings Time.
  5. **
  6. **  The rule is that DST starts at 2:00 AM on the first Sunday of April
  7. **  and ends at 2:00 AM on the last Sunday of October.
  8. **
  9. **  Original Copyright 1995 by Bob Stout as part of
  10. **  the MicroFirm Function Library (MFL)
  11. **
  12. **  The user is granted a free limited license to use this source file
  13. **  to create royalty-free programs, subject to the terms of the
  14. **  license restrictions specified in the LICENSE.MFL file.
  15. */
  16.  
  17. #include "sniptype.h"
  18. #include "scaldate.h"
  19.  
  20. /*
  21. **  Encode DST rules here - defaults to standard U.S. rules.
  22. */
  23.  
  24. unsigned    DST_start_mo = 4;             /* Month when DST starts      */
  25. unsigned    DST_start_dt = 1;             /* Date when it can start     */
  26. enum DOW_T  DST_start_dy = SUNDAY;        /* Day of week, or DOW_IGNORE */
  27.  
  28. unsigned    DST_stop_mo = 10;             /* Month when DST stops       */
  29. unsigned    DST_stop_dt = 31;             /* Date when it can stop      */
  30. enum DOW_T  DST_stop_dy = SUNDAY;         /* Day of week, or DOW_IGNORE */
  31.  
  32. /*
  33. **  isDST()
  34. **
  35. **  Parameters: 1 - Year of interest.
  36. **              2 - Month to check.
  37. **              3 - Day to check.
  38. **              4 - Pointer to storage for scalar date representation of
  39. **                  the year's DST start date.
  40. **              5 - Pointer to storage for scalar date representation of
  41. **                  the year's DST stop date.
  42. **
  43. **  Returns: True_ if date is in DST range
  44. **           False_ if date is not in DST  range
  45. **           Error_ if date is invalid,
  46. */
  47.  
  48. Boolean_T isDST(unsigned  yr,
  49.                 unsigned  mo,
  50.                 unsigned  dy,
  51.                 long     *Start,
  52.                 long     *Stop)
  53. {
  54.       long date;
  55.  
  56.       if (!valiDate(yr, mo, dy))
  57.             return Error_;
  58.       else  date = ymd_to_scalar(yr, mo, dy);
  59.  
  60.       *Start = ymd_to_scalar(yr, DST_start_mo, DST_start_dt);
  61.       *Stop  = ymd_to_scalar(yr, DST_stop_mo, DST_stop_dt);
  62.  
  63.       if (DST_start_dy != DOW_IGNORE)
  64.       {
  65.             while (DST_start_dy != (*Start % 7L))
  66.                   ++*Start;
  67.       }
  68.  
  69.       if (DST_stop_dy != DOW_IGNORE)
  70.       {
  71.             while (DST_stop_dy != (*Stop % 7L))
  72.                   --*Stop;
  73.       }
  74.  
  75.       return (date >= *Start && date < *Stop);
  76. }
  77.  
  78. #ifdef TEST
  79.  
  80. #include <stdio.h>
  81. #include <stdlib.h>
  82. #include "datetime.h"
  83.  
  84. main(int argc, char *argv[])
  85. {
  86.       long Start, Stop;
  87.       unsigned yr, mo, dy;
  88.       Boolean_T retval;
  89.  
  90.       if (2 > argc)
  91.       {
  92.             puts("Usage: ISDST \"date_string\"");
  93.             return EXIT_FAILURE;
  94.       }
  95.       if (Success_ != parse_date(argv[1], &yr, &mo, &dy, USA))
  96.       {
  97.             printf("Error parsing date \"%s\"\n", argv[1]);
  98.             return EXIT_FAILURE;
  99.       }
  100.  
  101.       if (Error_ == (retval = isDST(yr, mo, dy, &Start, &Stop)))
  102.             printf("%d/%d,%d is invalid!\n", mo, dy, yr);
  103.       else
  104.       {
  105.             printf("%d/%d/%d is%s within DST range\n\n", mo, dy, yr,
  106.                   retval ? "" : " not");
  107.             scalar_to_ymd(Start, &yr, &mo, &dy);
  108.             printf("In %d, DST starts on %d/%d\n", yr, mo, dy);
  109.             scalar_to_ymd(Stop, &yr, &mo, &dy);
  110.             printf("In %d, DST stops on %d/%d\n", yr, mo, dy);
  111.       }
  112.  
  113.       return EXIT_SUCCESS;
  114. }
  115.  
  116. #endif /* TEST */
  117.