home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume06 / isdst < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  3.5 KB

  1. From decwrl!labrea!rutgers!mailrus!tut.cis.ohio-state.edu!ucbvax!unisoft!uunet!allbery Sat Feb  4 10:50:45 PST 1989
  2. Article 812 of comp.sources.misc:
  3. Path: granite!decwrl!labrea!rutgers!mailrus!tut.cis.ohio-state.edu!ucbvax!unisoft!uunet!allbery
  4. From: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  5. Newsgroups: comp.sources.misc
  6. Subject: v06i037: Evaluate if a date is DST or not
  7. Message-ID: <48166@uunet.UU.NET>
  8. Date: 4 Feb 89 03:22:00 GMT
  9. Sender: allbery@uunet.UU.NET
  10. Reply-To: dg@lakart.UUCP (David Goodenough)
  11. Lines: 77
  12. Approved: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  13.  
  14. Posting-number: Volume 6, Issue 37
  15. Submitted-by: dg@lakart.UUCP (David Goodenough)
  16. Archive-name: isdst
  17.  
  18. [Does this critter deal with DST ambiguities like the recent change?  ++bsa]
  19.  
  20. For a project I'm doing, I needed to know if a given date was Daylight
  21. Savings or not. After searching long and hard, I dicovered that in the
  22. United States at least, the changes are always the last Sunday of April
  23. and October. The following algorithm will take a date in day, month,
  24. year, century form, and tell if it is DST or not: it returns zero if
  25. not DST, and 1 if the date is DST. Caveat: it assumes that the
  26. changeover is at midnight: it doesn't understand about the time slip at
  27. 2AM. However as long as you aren't up at 2AM I don't predict this will
  28. be a problem. As a freebie, it also includes a function that will
  29. evaluate the day of the week for a given date. This function returns a
  30. number between 0 and 6, where 0 means Sunday, 1 is Monday etc., and 6
  31. is Saturday.
  32. -- 
  33.     dg@lakart.UUCP - David Goodenough        +---+
  34.                         IHS    | +-+-+
  35.     ....... !harvard!xait!lakart!dg            +-+-+ |
  36. AKA:    dg%lakart.uucp@xait.xerox.com                +---+
  37.  
  38. --- cut here --- cut here --- cut here --- cut here --- cut here ---
  39. #! /bin/sh
  40. # This file was wrapped with "dummyshar".  "sh" this file to extract.
  41. # Contents:  isdst.c
  42. echo extracting 'isdst.c'
  43. if test -f 'isdst.c' -a -z "$1"; then echo Not overwriting 'isdst.c'; else
  44. sed 's/^X//' << \EOF > 'isdst.c'
  45. Xint isdst(day, month, year, cent)
  46. Xint day, month, year, cent;
  47. X {
  48. X    if (month < 4 || month > 10)
  49. X      return(0);    /* months 1 - 3, and 11, 12 are always not DST */
  50. X    if (month > 4 && month < 10)
  51. X      return(1);    /* months 5 - 9 are always DST */
  52. X
  53. X    /* see if there's a Sunday in the month after our day. */
  54. X
  55. X    while (++day <= ((month == 4) ? 30 : 31))
  56. X      if (dayofweek(day, month, year, cent) == 0)
  57. X    return(month == 10);    /* found one: that means we're before the
  58. X                 * changeover, so 4 => not DST, 10 => DST */
  59. X
  60. X    return(month == 4);        /* failed, so we're after the changeover,
  61. X                 * so 4 => DST, 10 => not DST */
  62. X }
  63. X
  64. Xint dayofweek(day, month, year, cent)
  65. Xint day, month, year, cent;
  66. X {
  67. X        /* This has it's roots in the Roman calendar, where the first
  68. X         * month was Mar, and Jan and Feb were months 11 and 12 of
  69. X         * the previous year */
  70. X    if ((month = month - 2) < 1) /* back month by two, but check for wrap */
  71. X     {
  72. X    month = month + 12;    /* reset to end of last year */
  73. X    if (year-- == 0)    /* reduce year number */
  74. X     {
  75. X        year = 99;        /* and adjust for century wrap if needed */
  76. X        cent--;
  77. X     }
  78. X     }
  79. X    return(((13 * month - 1) / 5 + day + year + year / 4 +
  80. X                        cent / 4 - 2 * cent) % 7);
  81. X                /* I don't have a clue how this got dreamed
  82. X                 * up, but it works. If memory serves I first
  83. X                 * saw it in the British Science Museum, in
  84. X                 * London, England */
  85. X }
  86. EOF
  87. chars=`wc -c < 'isdst.c'`
  88. if test $chars !=     1425; then echo 'isdst.c' is $chars characters, should be     1425 characters!; fi
  89. fi
  90. exit 0
  91.  
  92.  
  93.