home *** CD-ROM | disk | FTP | other *** search
/ Hall of Fame / HallofFameCDROM.cdr / proglc / zoo141_c.lzh / MSTIME.I < prev    next >
Text File  |  1987-02-07  |  3KB  |  88 lines

  1. #define BASEYEAR 1970
  2.  
  3. /****************
  4. Function mstime() converts time in seconds since January 1 of BASEYEAR
  5. to MS-DOS format date and time.
  6. */
  7. mstime(longtime, date, time)
  8. long longtime;       /* input:  seconds since Jan 1, BASEYEAR   */
  9. int *date, *time;    /* output: MS-DOS format date and time */
  10.  
  11. {
  12.    static int daysinmo[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  13. #define FEBRUARY 1
  14.    int year, month, day, hour, min, sec;
  15.    long secsinhour, secsinday, secsinyear, secsinleapyear;
  16.  
  17.    int leapyear;                             /* is this a leap year? */
  18.    int done;                                 /* control variable */
  19.  
  20.    secsinhour = (long) (60 * 60);            /* seconds in an hour */
  21.    secsinday  = 24 * secsinhour;             /* seconds in a day */
  22.    secsinyear = 365 * secsinday;             /* seconds in a year */
  23.    secsinleapyear = secsinyear + secsinday;  /* seconds in a leap year */
  24.  
  25. #ifdef DEBUG
  26. printf("mstime:  input longtime = %ld\n", longtime);
  27. #endif
  28.  
  29.    /* We can't handle dates before 1970 so force longtime positive */
  30.    if (longtime < 0)
  31.       longtime = 0;
  32.  
  33.    /* 
  34.    Step through years from BASEYEAR onwards, subtracting number of
  35.    seconds in each, stopping just before longtime would become negative.
  36.    */
  37.    year = BASEYEAR;
  38.    done = 0;
  39.    while (!done) {
  40.       long yearlength;
  41.       leapyear = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0);
  42.       if (leapyear)
  43.          yearlength = secsinleapyear;
  44.       else
  45.          yearlength = secsinyear;
  46.  
  47.       if (longtime >= yearlength) {
  48.          longtime -= yearlength;
  49.          year++;
  50.       } else
  51.          done++;
  52.    }
  53.  
  54.    /* Now `year' contains year and longtime contains remaining seconds */
  55.    daysinmo[FEBRUARY] = leapyear ? 29 : 28;
  56.  
  57.    month = 0; /* range is 0:11 */
  58.    while (longtime > daysinmo[month] * secsinday) {
  59.       longtime = longtime - daysinmo[month] * secsinday;
  60.       month++;
  61.    }
  62.    month++; /* range now 1:12 */
  63.  
  64.    day = longtime / secsinday;     /* day of month, range 0:30 */
  65.    longtime = longtime % secsinday;
  66.    day++;                         /* day of month, range 1:31 */
  67.  
  68.    hour = longtime / secsinhour;       /* hours, range 0:23 */
  69.    longtime = longtime % secsinhour;
  70.  
  71.    min = longtime / 60L;               /* minutes, range 0:59 */
  72.    longtime = longtime % 60L;
  73.  
  74.    sec = longtime;                     /* seconds, range 0:59 */
  75.  
  76. #ifdef DEBUG
  77. printf("mstime:  date = %4d/%02d/%02d   time = %02d:%02d:%02d\n",
  78.       year, month, day, hour, min, sec);
  79. if (leapyear)
  80.    printf("(leap year)\n");
  81. #endif
  82.  
  83.    if (year < 1980)
  84.       year = 1980;
  85.    *date = day + (month << 5) + ((year - 1980) << 9);
  86.    *time = (sec / 2) + (min << 5) + (hour << 11);
  87. }
  88.