home *** CD-ROM | disk | FTP | other *** search
/ YPA: Your Privacy Assured / YPA.ISO / other_goodies / utilities / memomaster.lha / MM2.1 / source / datefunc.c next >
C/C++ Source or Header  |  1994-09-06  |  4KB  |  144 lines

  1. #include <exec/types.h>
  2. #include <ctype.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <time.h>
  6.  
  7. #include "mm2.h"
  8.  
  9. /* Prototypes */
  10.  
  11. Prototype int Month2Num(char *);
  12. Prototype BOOL Month2Txt(int,char *);
  13. Prototype BOOL CheckMemoDate(char *);
  14. Prototype BOOL CheckDate(int,int,int);
  15. Prototype void Today(char *);
  16. Prototype int date2days(int,int,int);
  17.  
  18. /* Date functions for MM2 */
  19.  
  20. char *Months = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
  21. char Today_buf[10];
  22.  
  23. /*-----------------------------------------------------------------------
  24.  * Convert month as MMM (eg JUL) to month number (eg 2 for FEB).
  25.  *   Does not assume case or that text is NULL terminated
  26.  */
  27. Month2Num(char *MonthTxt)
  28.   {
  29.   int x, month;
  30.   char m[4];
  31.  
  32.   for (x=0 ; x<3 ; x++)
  33.     {
  34.     m[x] = toupper(*(MonthTxt+x));
  35.     }
  36.  
  37.   m[3]='\0';
  38.   month=(int) (strstr(Months, m) - Months);
  39.   if (month%3 != 0) return 0;
  40.   month = (month / 3) + 1;
  41.   if ((month < 1) || (month > 12))
  42.     return 0;
  43.   else
  44.     return month;
  45.   }
  46.  
  47. /*-----------------------------------------------------------------------
  48.  * Convert month number (eg 3 for March) to three char string in caller
  49.  * supplied buffer. Return TRUE of FALSE.
  50.  */
  51. BOOL Month2Txt(int MonthNum, char *MonthTxt)
  52.   {
  53.   if ((MonthNum < 1) || (MonthNum > 12))
  54.     {
  55.     *MonthTxt = '\0';
  56.     return FALSE;
  57.     }
  58.   MonthNum = (MonthNum - 1) * 3;
  59.   *MonthTxt = *(Months+MonthNum);
  60.   *(MonthTxt+1) = *(Months+MonthNum+1);
  61.   *(MonthTxt+2) = *(Months+MonthNum+2);
  62.   *(MonthTxt+3) = '\0';
  63.   return TRUE;
  64.   }
  65.  
  66. /*===========================================================================*/
  67. BOOL CheckMemoDate(char *dbuf)
  68.   {
  69.   char DDay[3];
  70.   char DYear[3];
  71.  
  72.   DDay[0]=dbuf[0];
  73.   DDay[1]=dbuf[1];
  74.   DDay[2]='\0';
  75.   DYear[0]=dbuf[7];
  76.   DYear[1]=dbuf[8];
  77.   DYear[2]='\0';
  78.   return (CheckDate(atoi(DDay), Month2Num((char*)(dbuf+3)), atoi(DYear)));
  79.   }
  80. /*==========================================================================*/
  81. BOOL CheckDate(int dd, int mm, int yy)
  82.   {
  83.   int md;
  84.   int cd_md[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
  85.  
  86.   if ((mm<1)||(mm>12)) return FALSE;  /* Validate month */
  87.   md=cd_md[mm];
  88.   if (yy<20) yy+=100;       /* Cope with years from 1920 to 2019  ???  */
  89.   if ((mm==2)&&((yy-68)%4==0))
  90.       md++;               /* If Feb in a leap year, allow 29th */
  91.   if (md<dd) return FALSE;        /* Validate day */
  92.   return TRUE;                 /* Date passed checks so OK */
  93.   }
  94.  
  95.  
  96. /*------------------------------------------------------------------------
  97.  * void Today(char *Today_buf)
  98.  *   Sets up string containing todays date as dd-mmm-yy (eg 04-jan-56) in
  99.  *   buffer supplied by caller. Buffer must be at least 10 bytes long.
  100.  */
  101. void Today(char *Today_buf)
  102.   {
  103.   long GMTsecs;
  104.   char *time_str;
  105.  
  106.   GMTsecs = time(NULL);
  107.   time_str = ctime(&GMTsecs);
  108.   Today_buf[0] = *(time_str+8);
  109.   Today_buf[1] = *(time_str+9);
  110.   Today_buf[2] = '-';
  111.   Today_buf[3] = *(time_str+4);
  112.   Today_buf[4] = *(time_str+5);
  113.   Today_buf[5] = *(time_str+6);
  114.   Today_buf[6] = '-';
  115.   Today_buf[7] = *(time_str+22);
  116.   Today_buf[8] = *(time_str+23);
  117.   Today_buf[9] = '\0';
  118.   }
  119.  
  120. /*==========================================================================*/
  121. date2days(dd,mm,yy)
  122. int dd,mm,yy;
  123. /* Returns number of days since start of 1970, ie 1 Jan 1970 is day 1 */
  124. {
  125.     int wy,wyd,lyd,wm,wmd;
  126.     int ad[]={0,3,3,6,8,11,13,16,19,21,24,26};
  127.  
  128.     wy=yy-70;            /* whole years elapsed */
  129.     wyd=wy*365;         /* days in whole years ignoring leap years */
  130.     if (wy>=3)                  /**/
  131.     lyd=((wy-3)/4)+1;       /* work out additions due to leap years */
  132.     else            /**/
  133.     lyd=0;            /**/
  134.     wm=mm-1;            /* whole months in last year */
  135.     wmd=wm*28;            /**/
  136.     if (((wy-2)%4==0)&&(wm>=2)) /**/
  137.     wmd++;            /* days in whole months in latest year */
  138.     wmd+=ad[wm];        /**/
  139.     return wyd+lyd+wmd+dd;    /* return total */
  140. }
  141.  
  142. /*==========================================================================*/
  143.  
  144.