home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 171.lha / SupLib / datetos.c < prev    next >
C/C++ Source or Header  |  1988-04-28  |  1KB  |  69 lines

  1.  
  2. /*
  3.  *  datetos(date, str, ctl)
  4.  */
  5.  
  6. #include <local/typedefs.h>
  7.  
  8. static char dim[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  9. static char *Month[12] = { "Jan","Feb","Mar","Apr","May","Jun","Jul",
  10.                "Aug","Sep","Oct","Nov","Dec" };
  11.  
  12. char *
  13. datetos(date, str, ctl)
  14. DATESTAMP *date;
  15. char *str;
  16. char *ctl;
  17. {
  18.     long days, years;
  19.     short leap, month;
  20.  
  21.     if (ctl == NULL)
  22.     ctl = "D M Y h:m:s";
  23.     days = date->ds_Days + 731;         /*    1976        */
  24.     years = days / (365*3+366);             /*  #quad yrs   */
  25.     days -= years * (365*3+366);
  26.     leap = (days < 366);                    /*  is a leap yr*/
  27.     years = 1976 + 4 * years;
  28.     if (!leap) {
  29.     days -= 366;
  30.     ++years;
  31.     }
  32.     years += days / 365;
  33.     days -= (days / 365) * 365;
  34.     for (month = 0; (month==1) ? (days >= 28 + leap) : (days >= dim[month]); ++month)
  35.     days -= (month==1) ? (28 + leap) : dim[month];
  36.     {
  37.     register short i = 0;
  38.     for (; *ctl; ++ctl) {
  39.         switch(*ctl) {
  40.         case 'h':
  41.         sprintf(str+i, "%02d", date->ds_Minute / 60);
  42.         break;
  43.         case 'm':
  44.         sprintf(str+i, "%02d", date->ds_Minute % 60);
  45.         break;
  46.         case 's':
  47.         sprintf(str+i, "%02d", date->ds_Tick / 50 % 60);
  48.         break;
  49.         case 'Y':
  50.         sprintf(str+i, "%ld", years);
  51.         break;
  52.         case 'M':
  53.         strcpy(str+i, Month[month]);
  54.         break;
  55.         case 'D':
  56.         sprintf(str+i,"%2ld", days+1);
  57.         break;
  58.         default:
  59.         str[i] = *ctl;
  60.         str[i+1] = 0;
  61.         break;
  62.         }
  63.         i += strlen(str+i);
  64.     }
  65.     }
  66.     return(str);
  67. }
  68.  
  69.