home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / fileutil / stuff2.arj / TIME.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-21  |  3.4 KB  |  130 lines

  1. /*
  2. Stuff 2.0.
  3.  
  4. Checksum: 4047342816 (verify with "brik")
  5.  
  6. (C) Copyright 1988 Rahul Dhesi.  Permission is granted to copy and
  7. distribute this file in modified or unmodified form, whether for
  8. noncommercial or commercial use, provided (a) this copyright notice
  9. is preserved, (b) no attempt is made to restrict redistribution of
  10. this file, and (c) this file is not distributed as part of any
  11. collection whose redistribution is restricted by a compilation
  12. copyright.
  13. */
  14.  
  15. #include "stuff.h"
  16. #include <stdio.h>
  17. #include <dos.h>
  18. #include <fcntl.h>
  19. #include <io.h>
  20. /*
  21. converts msdos format date/time into unix format
  22. */
  23.  
  24. long mstonix (unsigned date, unsigned time)
  25. {
  26.    int year, month, day, hour, min, sec, daycount;
  27.    long longtime;
  28.    /* no. of days to beginning of month for each month */
  29.    static int dsboy[12] = { 0, 31, 59, 90, 120, 151, 181, 212,
  30.                               243, 273, 304, 334};
  31.  
  32.    if (date == 0 && time == 0)         /* special case! */
  33.       return (0L);
  34.  
  35.    year  =  (((unsigned int) date >> 9) & 0x7f) + 1980;
  36.    month =  ((unsigned int) date >> 5) & 0x0f;
  37.    day   =  date        & 0x1f;
  38.  
  39.    hour =  ((unsigned int) time >> 11)& 0x1f;
  40.    min   =  ((unsigned int) time >> 5) & 0x3f;
  41.    sec   =  ((unsigned int) time & 0x1f) * 2;
  42.  
  43. /*
  44. DEBUG and leap year fixes thanks to Mark Alexander 
  45. <uunet!amdahl!drivax!alexande>
  46. */
  47. #if 0
  48. #ifdef DEBUG
  49.    printf ("mstonix:  year=%d  month=%d  day=%d  hour=%d  min=%d  sec=%d\n",
  50.            year, month, day, hour, min, sec);
  51. #endif
  52. #endif
  53.  
  54.    /* Calculate days since 1970/01/01 */
  55.    daycount = 365 * (year - 1970) +    /* days due to whole years */
  56.                (year - 1969) / 4 +     /* days due to leap years */
  57.                dsboy[month-1] +        /* days since beginning of this year */
  58.                day-1;                  /* days since beginning of month */
  59.  
  60.    if (year % 4 == 0 && 
  61.        year % 400 != 0 && month >= 3)  /* if this is a leap year and month */
  62.       daycount++;                      /* is March or later, add a day */
  63.  
  64.    /* Knowing the days, we can find seconds */
  65.    longtime = daycount * 24L * 60L * 60L    +
  66.           hour * 60L * 60L   +   min * 60   +    sec;
  67.    return (longtime);
  68. }
  69.  
  70. /*
  71. gets system time
  72. */
  73. long systime(void)
  74. {
  75.    struct date dateblk;
  76.    struct time timeblk;
  77.    getdate (&dateblk);
  78.    gettime (&timeblk);
  79.    return (dostounix (&dateblk, &timeblk));
  80. }
  81.  
  82. /*
  83. compares file times
  84. */
  85. int cmptime (long utime, compare_pt choice, long value)
  86. {
  87.    long diff;
  88.    diff = (systime() - utime);
  89.    if (choice == SAME && diff == value ||
  90.          choice == BIGGER && diff > value ||
  91.          choice == SMALLER && diff < value) {
  92.       return (1);
  93.    } else {
  94.       return (0);
  95.    }
  96. }
  97.  
  98. /*
  99. returns file time in unix format
  100. */
  101. long filetime (char *fname)
  102. {
  103.    union {
  104.       struct ftime bits;
  105.       struct {
  106.          unsigned int time;
  107.          unsigned int date;
  108.       } words;
  109.    } date_time;
  110.  
  111.    int h;
  112.    h = _open (fname, O_RDONLY);
  113.    if (h == -1)
  114.       return (0L);
  115.    getftime (h, &date_time.bits);
  116.    close(h);
  117.    return (mstonix (date_time.words.date, date_time.words.time));
  118. }
  119.  
  120. int cmptime2 (long utime, compare_pt choice, long value)
  121. {
  122.    if (choice == SAME && utime == value ||
  123.          choice == BIGGER && utime > value ||
  124.          choice == SMALLER && utime < value) {
  125.       return (1);
  126.    } else {
  127.       return (0);
  128.    }
  129. }
  130.