home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / metamail / contrib / ServiceMail / src / pdinq / getdate / dateconv.h next >
Encoding:
C/C++ Source or Header  |  1993-03-15  |  2.1 KB  |  80 lines

  1. static int mdays[12] =
  2.     {31, 0, 31,  30, 31, 30,  31, 31, 30,  31, 30, 31};
  3. #define epoch 1970
  4.  
  5. #include <time.h>
  6. extern struct tm *localtime();
  7. time_t dateconv(mm, dd, yy, h, m, s, mer, zone, dayflag)
  8. int mm, dd, yy, h, m, s, mer, zone, dayflag;
  9. {
  10.     time_t tod, jdate;
  11.     register int i;
  12.     time_t timeconv();
  13.  
  14.     if (yy < 100) yy += 1900;
  15.     mdays[1] = 28 + (yy%4 == 0);
  16.     if (yy < epoch || yy > 1999 || mm < 1 || mm > 12 ||
  17.         dd < 1 || dd > mdays[--mm]) return (-1);
  18.     jdate = dd-1;
  19.         for (i=0; i<mm; i++) jdate += mdays[i];
  20.     for (i = epoch; i < yy; i++) jdate += 365 + (i%4 == 0);
  21.     jdate *= daysec;
  22.     jdate += zone * (60L*60L);
  23.     if ((tod = timeconv(h, m, s, mer)) < 0) return (-1);
  24.     jdate += tod;
  25.     if (dayflag==DAYLIGHT || (dayflag==MAYBE&&localtime(&jdate)->tm_isdst))
  26.         jdate += -1*60*60;
  27.     return (jdate);
  28. }
  29.  
  30. time_t dayconv(ord, day, now) int ord, day; time_t now;
  31. {
  32.     register struct tm *loctime;
  33.     time_t tod;
  34.     time_t daylcorr();
  35.  
  36.     tod = now;
  37.     loctime = localtime(&tod);
  38.     tod += daysec * ((day - loctime->tm_wday + 7) % 7);
  39.     tod += 7*daysec*(ord<=0?ord:ord-1);
  40.     return daylcorr(tod, now);
  41. }
  42.  
  43. time_t timeconv(hh, mm, ss, mer) register int hh, mm, ss, mer;
  44. {
  45.     if (mm < 0 || mm > 59 || ss < 0 || ss > 59) return (-1);
  46.     switch (mer) {
  47.         case AM: if (hh < 1 || hh > 12) return(-1);
  48.              return (60L * ((hh%12)*60L + mm)+ss);
  49.         case PM: if (hh < 1 || hh > 12) return(-1);
  50.              return (60L * ((hh%12 +12)*60L + mm)+ss);
  51.         case 24: if (hh < 0 || hh > 23) return (-1);
  52.              return (60L * (hh*60L + mm)+ss);
  53.         default: return (-1);
  54.     }
  55. }
  56. time_t monthadd(sdate, relmonth) time_t sdate, relmonth;
  57. {
  58.     struct tm *ltime;
  59.     time_t dateconv();
  60.     time_t daylcorr();
  61.     int mm, yy;
  62.  
  63.     if (relmonth == 0) return 0;
  64.     ltime = localtime(&sdate);
  65.     mm = 12*ltime->tm_year + ltime->tm_mon + relmonth;
  66.     yy = mm/12;
  67.     mm = mm%12 + 1;
  68.     return daylcorr(dateconv(mm, ltime->tm_mday, yy, ltime->tm_hour,
  69.         ltime->tm_min, ltime->tm_sec, 24, ourzone, MAYBE), sdate);
  70. }
  71.  
  72. time_t daylcorr(future, now) time_t future, now;
  73. {
  74.     int fdayl, nowdayl;
  75.  
  76.     nowdayl = (localtime(&now)->tm_hour+1) % 24;
  77.     fdayl = (localtime(&future)->tm_hour+1) % 24;
  78.     return (future-now) + 60L*60L*(nowdayl-fdayl);
  79. }
  80.