home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 4 / CDPD_IV.bin / networking / dnet / dnet2.3.2 / amiga / suplib / datetos.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-29  |  1.7 KB  |  76 lines

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