home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / SRC / msdos_diskaccess.lzh / MS_DISK_ACCESS / convdate.c < prev    next >
Text File  |  1991-08-03  |  2KB  |  96 lines

  1. /*
  2.  * convdate(), convtime(), convstamp()
  3.  */
  4. #include <stdio.h>
  5. /*
  6.  * convert MSDOS directory datestamp to ASCII.
  7.  */
  8.  
  9. char *
  10. convdate(date_high, date_low)
  11. unsigned date_high, date_low;
  12. {
  13. /*
  14.  *        hi byte     |    low byte
  15.  *    |7|6|5|4|3|2|1|0|7|6|5|4|3|2|1|0|
  16.  *      | | | | | | | | | | | | | | | | |
  17.  *      \   7 bits    /\4 bits/\ 5 bits /
  18.  *         year +80      month     day
  19.  */
  20.     static char ans[9];
  21.     unsigned char year, month_hi, month_low, day;
  22.  
  23.     year = (date_high >> 1) + 80;
  24.     month_hi = (date_high & 0x1) << 3;
  25.     month_low = date_low >> 5;
  26.     day = date_low & 0x1f;
  27.     sprintf(ans, "%2d-%02d-%02d", month_hi+month_low, day, year);
  28.     return(ans);
  29. }
  30.  
  31. /*
  32.  * Convert MSDOS directory timestamp to ASCII.
  33.  */
  34.  
  35. char *
  36. convtime(time_high, time_low)
  37. unsigned time_high, time_low;
  38. {
  39. /*
  40.  *        hi byte     |    low byte
  41.  *    |7|6|5|4|3|2|1|0|7|6|5|4|3|2|1|0|
  42.  *      | | | | | | | | | | | | | | | | |
  43.  *      \  5 bits /\  6 bits  /\ 5 bits /
  44.  *         hour      minutes     sec*2
  45.  */
  46.     static char ans[7];
  47.     char am_pm;
  48.     unsigned char hour, min_hi, min_low;
  49.  
  50.     hour = time_high >> 3;
  51.     am_pm = (hour >= 12) ? 'p' : 'a';
  52.     if (hour > 12)
  53.         hour = hour -12;
  54.     if (hour == 0)
  55.         hour = 12;
  56.     min_hi = (time_high & 0x7) << 3;
  57.     min_low = time_low >> 5;
  58.     sprintf(ans, "%2d:%02d%c", hour, min_hi+min_low, am_pm);
  59.     return(ans);
  60. }
  61.  
  62. /*
  63.  * Convert a MSDOS time & date stamp to the Unix time() format
  64.  */
  65.  
  66. long
  67. convstamp(time_field, date_field)
  68. unsigned char *time_field, *date_field;
  69. {
  70.     int year, mon, mday, hour, min, sec, old_leaps;
  71.     long answer, sec_year, sec_mon, sec_mday, sec_hour, sec_min, sec_leap;
  72.     static int month[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304,
  73.     334};
  74.                     /* disect the parts */
  75.     year = (date_field[1] >> 1) + 1980;
  76.     mon = (((date_field[1] & 0x1) << 3) + (date_field[0] >> 5));
  77.     mday = date_field[0] & 0x1f;
  78.     hour = time_field[1] >> 3;
  79.     min = (((time_field[1] & 0x7) << 3) + (time_field[0] >> 5));
  80.     sec = (time_field[0] & 0x1f) * 2;
  81.                     /* how many previous leap years */
  82.     old_leaps = (year -1972) / 4;
  83.     sec_leap = old_leaps * 24 * 60 * 60;
  84.                     /* back off 1 day if before 29 Feb */
  85.     if (!(year % 4) && mon < 3)
  86.         sec_leap -= 24 * 60 * 60;
  87.     sec_year = (year - 1970) * 365 * 24 * 60 * 60;
  88.     sec_mon = month[mon -1] * 24 * 60 * 60;
  89.     sec_mday = mday * 24 * 60 * 60;
  90.     sec_hour = hour * 60 * 60;
  91.     sec_min = min * 60;
  92.     
  93.     answer = sec_leap + sec_year + sec_mon + sec_mday + sec_hour + sec_min + sec;
  94.     return(answer);
  95. }
  96.