home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / hyprmail.zip / date.c < prev    next >
C/C++ Source or Header  |  1994-07-31  |  7KB  |  254 lines

  1. /*
  2. ** Copyright (C) 1994, Enterprise Integration Technologies Corp.        
  3. ** All Rights Reserved.
  4. ** Kevin Hughes, kevinh@eit.com 
  5. ** 7/14/94
  6. */
  7.  
  8. /* Date stuff. Everything here is my own code, with the exception
  9. ** of getnweekday(), which is in the public domain. -- Kevin
  10. */
  11.  
  12. #include "hypermail.h"
  13. #include "date.h"
  14.  
  15. /* This converts a long date string into the form D/M/Y.
  16. */
  17.  
  18. void convtoshortdate(date, shortdate)
  19.      char *date;
  20.      char *shortdate;
  21. {
  22.         char c, d, e;
  23.         int i, month;
  24.  
  25.         c = date[4];
  26.         d = date[5];
  27.         e = date[6];
  28.  
  29.         for (i = 0; !(c == (months[i])[0] && d == (months[i])[1] &&
  30.         e == (months[i])[2]); i++)
  31.                 ;
  32.         month = i + 1;
  33.  
  34.         shortdate[0] = (month > 9) ? '1' : '0';
  35.         shortdate[1] = monthnums[month];
  36.         shortdate[2] = '/';
  37.         shortdate[3] = (date[8] == ' ') ? '0' : date[8];
  38.         shortdate[4] = date[9];
  39.         shortdate[5] = '/';
  40.         shortdate[6] = date[22];
  41.         shortdate[7] = date[23];
  42.         shortdate[8] = '\0';
  43. }
  44.  
  45. /* Splits a short date (M/D/Y) into month, day, and year components.
  46. ** It looks ugly because it should be super-fast.
  47. */
  48.  
  49. void splitshortdate(shortdate, month, day, year)
  50.      char *shortdate;
  51.      int *month;
  52.      int *day;
  53.      int *year;
  54. {
  55.         if (shortdate[0] == ' ' || shortdate[0] == '0')
  56.                 *month = (shortdate[1] - '0');
  57.         else if (shortdate[1] == '/')
  58.                 *month = (shortdate[0] - '0');
  59.         else if (shortdate[2] == '/')
  60.                 *month = (shortdate[0] - '0') * 10 + (shortdate[1] - '0');
  61.  
  62.         if (shortdate[2] =='/') {
  63.                 if (shortdate[5] == '/') {
  64.                         *day = (shortdate[3] - '0') * 10 +
  65.                         (shortdate[4] - '0');
  66.                         *year = (shortdate[6] - '0') * 10 +
  67.                         (shortdate[7] - '0');
  68.                 }
  69.                 else if (shortdate[4] == '/') {
  70.                         *day = (shortdate[3] - '0');
  71.                         *year = (shortdate[5] - '0') * 10
  72.                         + (shortdate[6] - '0');
  73.                 }
  74.         }
  75.         else if (shortdate[1] =='/') {
  76.                 if (shortdate[4] == '/') {
  77.                         *day = (shortdate[2] - '0') * 10
  78.                         + (shortdate[3] - '0');
  79.                         *year = (shortdate[5] - '0') * 10
  80.                         + (shortdate[6] - '0');
  81.                 }
  82.                 else if (shortdate[3] == '/') {
  83.                         *day = (shortdate[2] - '0');
  84.                         *year = (shortdate[4] - '0') * 10
  85.                         + (shortdate[5] - '0');
  86.                 }
  87.         }
  88. }
  89.  
  90. /* Given a short date (M/D/Y), it returns the number of seconds
  91. ** since BASEYEAR.
  92. */
  93.  
  94. long getyearsecs(shortdate)
  95.      char *shortdate;
  96. {
  97.         int i, yearday, yearsecs, prevyeardays;
  98.         int month, day, year;
  99.  
  100.         splitshortdate(shortdate, &month, &day, &year);
  101.         year += CENTURY;
  102.  
  103.         for (yearday = i = 0; i < month - 1; i++) {
  104.                 if (i == 1 && IS_LEAP(year))
  105.                         yearday++;
  106.                 yearday += monthdays[i];
  107.         }
  108.         yearday += day;
  109.  
  110.         prevyeardays = 0;
  111.         for (i = BASEYEAR; i != year; i++) {
  112.                 if (IS_LEAP(i))
  113.                         prevyeardays++;
  114.                 prevyeardays += DAYSPERYEAR;
  115.         }
  116.  
  117.         yearsecs = (yearday + prevyeardays) * SECSPERDAY;
  118.  
  119.         return yearsecs;
  120. }
  121.  
  122. /* Given a long date string, it returns the number of seconds
  123. ** since BASEYEAR.
  124. */
  125.  
  126. int convtoyearsecs(date)
  127.      char *date;
  128. {
  129.         char hourstr[3], minstr[3], secstr[3], shortdate[SHORTDATELEN];
  130.         int hours, minutes, seconds;
  131.         long yearsecs;
  132.  
  133.         convtoshortdate(date, shortdate);
  134.         yearsecs = getyearsecs(shortdate);
  135.  
  136.         sprintf(hourstr, "%c%c", date[11], date[12]);
  137.         sprintf(minstr, "%c%c", date[14], date[15]);
  138.         sprintf(secstr, "%c%c", date[17], date[18]);
  139.  
  140.         hours = atoi(hourstr);
  141.         minutes = atoi(minstr);
  142.         seconds = atoi(secstr);
  143.  
  144.         return (int) (yearsecs + (hours * SECSPERHOUR) +
  145.         (minutes * SECSPERMIN) + seconds);
  146. }
  147.  
  148. /* Gets the local time and returns it formatted.
  149. */
  150.  
  151. char *getlocaltime()
  152. {
  153.         static char s[DATESTRLEN];
  154.         time_t tp;
  155.  
  156.         time(&tp);
  157. #ifdef EURODATE
  158.         strftime(s, DATESTRLEN, "%a %d %b %Y - %H:%M:%S", localtime(&tp));
  159. #else
  160.         strftime(s, DATESTRLEN, "%a %b %d %Y - %H:%M:%S", localtime(&tp));
  161. #endif
  162.     sprintf(s, "%s %s", s, timezonestr);
  163.  
  164.         return s;
  165. }
  166.  
  167. /* Gets the local time zone.
  168. */
  169.  
  170. void gettimezone()
  171. {
  172.         time_t tp;
  173.  
  174.         time(&tp);
  175.         strftime(timezonestr, TIMEZONELEN, "%Z", localtime(&tp));
  176. }
  177.  
  178. /* Gets the current year.
  179. */
  180.  
  181. void getthisyear()
  182. {
  183.         time_t tp;
  184.  
  185.         time(&tp);
  186.         strftime(thisyear, YEARLEN, "%Y", localtime(&tp));
  187. }
  188.  
  189. /* From the number of seconds since BASEYEAR, this pretty-prints
  190. ** a date for you.
  191. */
  192.  
  193. char *getdatestr(yearsecs)
  194.      long yearsecs;
  195. {
  196.         register int day, year, month, hours, minutes;
  197.         static char date[DATESTRLEN];
  198.  
  199.         for (day = 0; yearsecs > SECSPERDAY; day++)
  200.                 yearsecs -= SECSPERDAY;
  201.  
  202.         for (year = BASEYEAR; day > DAYSPERYEAR; year++) {
  203.                 if (IS_LEAP(year))
  204.                         day--;
  205.                 day -= DAYSPERYEAR;
  206.         }
  207.  
  208.         if (IS_LEAP(year) && day > (monthdays[0] + monthdays[1])) {
  209.                 day--;
  210.                 yearsecs -= SECSPERDAY;
  211.         }
  212.  
  213.         for (month = 0; day > monthdays[month]; month++)
  214.                 day -= monthdays[month];
  215.  
  216.         for (hours = 0; yearsecs > SECSPERHOUR; hours++)
  217.                 yearsecs -= SECSPERHOUR;
  218.  
  219.         for (minutes = 0; yearsecs > SECSPERMIN; minutes++)
  220.                 yearsecs -= SECSPERMIN;
  221.  
  222. #ifdef EURODATE
  223.         sprintf(date, "%s%s %02d %d - %02d:%02d:%02d %s",
  224.         days[getnweekday(month + 1, day, year)], months[month],
  225.         day, year, hours, minutes, yearsecs, timezonestr);
  226. #else
  227.         sprintf(date, "%s%02d %s %d - %02d:%02d:%02d %s",
  228.         days[getnweekday(month + 1, day, year)], day, months[month],
  229.         year, hours, minutes, yearsecs, timezonestr);
  230. #endif
  231.  
  232.     return date;
  233. }
  234.  
  235. /* Returns the day of the year.
  236. */
  237.  
  238. int getnweekday(mn, dy, yr)
  239.      int mn;
  240.      int dy;
  241.      int yr;
  242. {
  243.         int n1, n2;
  244.  
  245.         if (mn < 3) {
  246.                 mn += 12;
  247.                 yr -= 1;
  248.         }
  249.         n1 = (26 * (mn + 1)) / 10;
  250.         n2 = (int) ((125 * (long) yr) / 100);
  251.  
  252.         return ((dy + n1 + n2 - (yr / 100) + (yr / 400) + -1) % 7);
  253. }
  254.