home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / FTIME.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  3KB  |  146 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  Public domain by Jeff Dunlop & Bob Stout
  5. */
  6.  
  7. #include <time.h>
  8. #include "ftime.h"
  9.  
  10. #if !defined(__TURBOC__) && !defined(__SC__) && !defined(__POWERC)
  11.  
  12. #ifdef __ZTC__
  13.  #pragma ZTC align 1
  14.  #define DOS_GETFTIME dos_getftime
  15.  #define DOS_SETFTIME dos_setftime
  16.  typedef unsigned FTIME_T_;
  17. #else
  18.  #pragma pack(1)
  19.  #define DOS_GETFTIME _dos_getftime
  20.  #define DOS_SETFTIME _dos_setftime
  21.  #ifdef __WATCOMC__
  22.   typedef unsigned short FTIME_T_;
  23.  #else
  24.   typedef unsigned FTIME_T_;
  25.  #endif
  26. #endif
  27.  
  28. int getftime (int handle, struct ftime *ftimep)
  29. {
  30.       int retval = 0;
  31.       union
  32.       {
  33.             struct
  34.             {
  35.                   unsigned time;
  36.                   unsigned date;
  37.             } msc_time;
  38.             struct ftime bc_time;
  39.       } FTIME;
  40.  
  41.       if (0 == (retval = DOS_GETFTIME(handle,
  42.             (FTIME_T_ *)&FTIME.msc_time.date,
  43.             (FTIME_T_ *)&FTIME.msc_time.time)))
  44.       {
  45.             *ftimep = FTIME.bc_time;
  46.       }
  47.       return retval;
  48. }
  49.  
  50. int setftime (int handle, struct ftime *ftimep)
  51. {
  52.       union
  53.       {
  54.             struct
  55.             {
  56.                   unsigned time;
  57.                   unsigned date;
  58.             } msc_time;
  59.             struct ftime bc_time;
  60.       } FTIME;
  61.  
  62.       FTIME.bc_time = *ftimep;
  63.  
  64.       return DOS_SETFTIME(handle, FTIME.msc_time.date, FTIME.msc_time.time);
  65. }
  66.  
  67. #endif
  68.  
  69. static void _ftimecnvrt(struct ftime *ft, struct tm *time, time_t *tt)
  70. {
  71.       time->tm_sec  = ft->ft_tsec * 2;
  72.       time->tm_min  = ft->ft_min;
  73.       time->tm_hour = ft->ft_hour;
  74.       time->tm_mday = ft->ft_day;
  75.       time->tm_mon  = ft->ft_month - 1;
  76.       time->tm_year = ft->ft_year + 80;
  77.       *tt = mktime(time);                 /* Fill in rest of the struct */
  78. }
  79.  
  80. void ftime2tm(struct ftime *ft, struct tm *time)
  81. {
  82.       time_t tt;
  83.       
  84.       _ftimecnvrt(ft, time, &tt);
  85. }
  86.  
  87. time_t ftime2time(struct ftime *ft)
  88. {
  89.       struct tm time;
  90.       time_t tt;
  91.       
  92.       _ftimecnvrt(ft, &time, &tt);
  93.       return tt;
  94. }
  95.  
  96. #ifdef TEST
  97.  
  98. #include <stdio.h>
  99. #include <stdlib.h>
  100. #if defined(MSDOS) || defined(__MSDOS__)
  101.  #include "unistd.h"
  102. #else
  103.  #include <unistd.h>
  104. #endif
  105. #include "errors.h"
  106. #include "sniptype.h"
  107.  
  108. #ifdef __WATCOMC__
  109.  #pragma off (unreferenced);
  110. #endif
  111. #ifdef __TURBOC__
  112.  #pragma argsused
  113. #endif
  114.  
  115. main(int argc, char *argv[])
  116. {
  117.       struct ftime ft;
  118.       struct tm time;
  119.       FILE *fp;
  120.       char timeline[80];
  121.  
  122.       fp = cant(argv[0], "r");
  123.       if (Success_ != getftime(fileno(fp), &ft))
  124.             ErrExit("getftime() failed");
  125.       ftime2tm(&ft, &time);
  126.       printf("ft_tsec = %d\n", ft.ft_tsec);
  127.       printf("ft_min  = %d\n", ft.ft_min);
  128.       printf("ft_hour = %d\n", ft.ft_hour);
  129.       printf("ft_day  = %d\n", ft.ft_day);
  130.       printf("ft_month= %d\n", ft.ft_month);
  131.       printf("ft_year = %d\n", ft.ft_year);
  132.       puts("");
  133.       printf("tm_sec  = %d\n", time.tm_sec);
  134.       printf("tm_min  = %d\n", time.tm_min);
  135.       printf("tm_hour = %d\n", time.tm_hour);
  136.       printf("tm_mday = %d\n", time.tm_mday);
  137.       printf("tm_mon  = %d\n", time.tm_mon);
  138.       printf("tm_year = %d\n", time.tm_year);
  139.  
  140.       strftime(timeline, 80, "\n%c\n", &time);
  141.       puts(timeline);
  142.       return EXIT_SUCCESS;
  143. }
  144.  
  145. #endif /* TEST */
  146.