home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 5.ddi / CLIBSRC2.ZIP / TIMECVT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  4.1 KB  |  127 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - timecvt.c
  3.  *
  4.  * function(s)
  5.  *        dostounix - converts date and time to UNIX time format
  6.  *    unixtodos - converts from UNIX-format time
  7.  *-----------------------------------------------------------------------*/
  8.  
  9. /*
  10.  *      C/C++ Run Time Library - Version 5.0
  11.  *
  12.  *      Copyright (c) 1987, 1992 by Borland International
  13.  *      All Rights Reserved.
  14.  *
  15.  */
  16.  
  17.  
  18. #include <_io.h>
  19. #include <dos.h>
  20. #include <time.h>
  21.  
  22. static char Days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  23.  
  24. /*---------------------------------------------------------------------*
  25.  
  26. Name            dostounix - converts date and time to UNIX time format
  27.  
  28. Usage           long dostounix(struct date *d, struct time *t);
  29.  
  30. Prototype in    time.h
  31.  
  32. Description     Converts a date and time (as returned from getdate and
  33.                 gettime) into UNIX-format time.
  34.  
  35. Return value    UNIX version of the given date and time.
  36.  
  37. *---------------------------------------------------------------------*/
  38. long _FARFUNC dostounix(struct date *d, struct time *t)
  39. {
  40.         long            x;
  41.         register int    i;
  42.         register int    days;
  43.         int             hours;
  44.  
  45.         tzset();                                /* get timezone info */
  46.  
  47.         x = 24L * 60L * 60L * 3652L + timezone; /* Convert from 1980 to
  48.                                                    1970 base date GMT */
  49.         i = d->da_year - 1980;
  50.         x += (i >> 2) * (1461L * 24L * 60L * 60L);
  51.         x += (i & 3) * (24L * 60L * 60L * 365L);
  52.         if (i & 3)
  53.                 x += 24L * 3600L;
  54.         days = 0;
  55.         i = d->da_mon - 1;                      /* Add in months */
  56.         while (i > 0)
  57.         {
  58.                 i--;
  59.                 days += Days[i];
  60.         }
  61.         days += d->da_day - 1;
  62.         if (d->da_mon > 2 && (d->da_year & 3) == 0)
  63.                 days++;                         /* Currently in leap year */
  64.         hours = days * 24 + t->ti_hour;         /* Find hours */
  65.         if (daylight && __isDST( t->ti_hour, days, 0, d->da_year-1970))
  66.                 hours--;
  67.         x += hours * 3600L;
  68.         x += 60L * t->ti_min + t->ti_sec;
  69.         return (x);
  70. }
  71.  
  72.  
  73. /*---------------------------------------------------------------------*
  74.  
  75. Name            unixtodos - converts from UNIX-format time
  76.  
  77. Usage           void unixtodos(long time, struct date *d, struct time *t);
  78.  
  79. Prototype in    time.h
  80.  
  81. Description     Converts a UNIX-format time into date and time.
  82.  
  83. Return value    There is no return value.
  84.  
  85. *---------------------------------------------------------------------*/
  86. void _FARFUNC unixtodos(long time, struct date *d, struct time *t)
  87. {
  88.         tzset();                                /* get timezone info */
  89.  
  90.         time -= 24L * 60L * 60L * 3652L + timezone;
  91.         t->ti_hund = 0;
  92.         t->ti_sec = time % 60;
  93.         time /= 60;                             /* Time in minutes */
  94.         t->ti_min = time % 60;
  95.         time /= 60;                             /* Time in hours */
  96.         d->da_year = 1980 + (int)((time / (1461L * 24L)) << 2);
  97.         time %= 1461L * 24L;
  98.         if (time >= 366 * 24)
  99.         {
  100.                 time -= 366 * 24;
  101.                 d->da_year++;
  102.                 d->da_year += (int)(time / (365 * 24));
  103.                 time %= 365 * 24;
  104.         }
  105.         if (daylight && __isDST( (int)(time % 24), (int)(time / 24), 0, d->da_year-1970 ))
  106.                 time++;
  107.         t->ti_hour = time % 24;
  108.         time /= 24;                             /* Time in days */
  109.         time++;
  110.         if ((d->da_year & 3) == 0)
  111.         {
  112.                 if (time > 60)
  113.                         time--;
  114.                 else
  115.                 if (time == 60)
  116.                 {
  117.                         d->da_mon = 2;
  118.                         d->da_day = 29;
  119.                         return;
  120.                 }
  121.         }
  122.         for (d->da_mon = 0; Days[d->da_mon] < time; d->da_mon++)
  123.                 time -= Days[d->da_mon];
  124.         d->da_mon++;
  125.         d->da_day = time;
  126. }
  127.