home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / ZOO21E.EXE / NIXTIME.I < prev    next >
Text File  |  1991-07-11  |  4KB  |  109 lines

  1. #ifndef LINT
  2. static char nixtimeid[]="@(#) nixtime.i 2.3 88/01/24 12:49:28";
  3. #endif /* LINT */
  4.  
  5. /*
  6. Time handling routines for UNIX systems.  These are included by the file
  7. machine.c as needed.
  8.  
  9. The contents of this file are hereby released to the public domain.
  10.  
  11.                                     -- Rahul Dhesi  1986/12/31
  12. */
  13.  
  14. struct tm *localtime();
  15.  
  16. /*****************
  17. Function gettime() gets the date and time of the file handle supplied.
  18. Date and time is in MSDOS format.
  19. */
  20. int gettime (file, date, time)
  21. ZOOFILE file;
  22. unsigned *date, *time;
  23. {
  24.    struct stat buf;           /* buffer to hold file information */
  25.    struct tm *tm;             /* will hold year/month/day etc. */
  26.     int handle;
  27.     handle = fileno(file);
  28.    if (fstat (handle, &buf) == -1) {
  29.       prterror ('w', "Could not get file time\n");
  30.       *date = *time = 0;
  31.    } else {
  32.       tm = localtime (&buf.st_mtime); /* get info about file mod time */
  33.       *date = tm->tm_mday + ((tm->tm_mon + 1) << 5) +
  34.          ((tm->tm_year - 80) << 9);
  35.       *time = tm->tm_sec / 2 + (tm->tm_min << 5) +
  36.          (tm->tm_hour << 11);
  37.    }
  38.  
  39. }
  40.  
  41. /*****************
  42. Function setutime() sets the date and time of the filename supplied.
  43. Date and time is in MSDOS format.  It assumes the existence of a function
  44. mstonix() that accepts MSDOS format time and returns **IX format time,
  45. and a function gettz() that returns the difference (localtime - gmt)
  46. in seconds, taking daylight savings time into account.
  47. */
  48. int setutime(path,date,time)
  49. char *path;
  50. unsigned int date, time;
  51. {
  52.     long mstonix();
  53.     long gettz();
  54.     long utimbuf[2];
  55.     utimbuf[0] = utimbuf[1] = gettz() + mstonix (date, time);
  56.     return (utime (path, utimbuf));
  57. }
  58.  
  59. /****************
  60. Function mstonix() accepts an MSDOS format date and time and returns
  61. a **IX format time.  No adjustment is done for timezone.
  62. */
  63.  
  64. long mstonix (date, time)
  65. unsigned int date, time;
  66. {
  67.    int year, month, day, hour, min, sec, daycount;
  68.    long longtime;
  69.    /* no. of days to beginning of month for each month */
  70.    static int dsboy[12] = { 0, 31, 59, 90, 120, 151, 181, 212,
  71.                               243, 273, 304, 334};
  72.  
  73.    if (date == 0 && time == 0)            /* special case! */
  74.       return (0L);
  75.  
  76.    /* part of following code is common to zoolist.c */
  77.    year  =  (((unsigned int) date >> 9) & 0x7f) + 1980;
  78.    month =  ((unsigned int) date >> 5) & 0x0f;
  79.    day   =  date        & 0x1f;
  80.  
  81.    hour =  ((unsigned int) time >> 11)& 0x1f;
  82.    min   =  ((unsigned int) time >> 5) & 0x3f;
  83.    sec   =  ((unsigned int) time & 0x1f) * 2;
  84.  
  85. /*
  86. DEBUG and leap year fixes thanks to Mark Alexander
  87. <uunet!amdahl!drivax!alexande>
  88. */
  89. #ifdef DEBUG
  90.    printf ("mstonix:  year=%d  month=%d  day=%d  hour=%d  min=%d  sec=%d\n",
  91.            year, month, day, hour, min, sec);
  92. #endif
  93.  
  94.    /* Calculate days since 1970/01/01 */
  95.    daycount = 365 * (year - 1970) +    /* days due to whole years */
  96.                (year - 1969) / 4 +     /* days due to leap years */
  97.                dsboy[month-1] +        /* days since beginning of this year */
  98.                day-1;                  /* days since beginning of month */
  99.  
  100.    if (year % 4 == 0 &&
  101.        year % 400 != 0 && month >= 3)  /* if this is a leap year and month */
  102.       daycount++;                      /* is March or later, add a day */
  103.  
  104.    /* Knowing the days, we can find seconds */
  105.    longtime = daycount * 24L * 60L * 60L    +
  106.           hour * 60L * 60L   +   min * 60   +    sec;
  107.     return (longtime);
  108. }
  109.