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