home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / dtoxtime.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  7KB  |  230 lines

  1. /***
  2. *dtoxtime.c - convert OS local time to time_t
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines __loctotime_t() - convert OS local time to internal format
  8. *       (time_t).
  9. *
  10. *******************************************************************************/
  11.  
  12. #ifdef _WIN32
  13.  
  14.  
  15.  
  16. #include <cruntime.h>
  17. #include <time.h>
  18. #include <ctime.h>
  19. #include <internal.h>
  20.  
  21. /***
  22. *time_t __loctotime_t(yr, mo, dy, hr, mn, sc, dstflag) - converts OS local
  23. *       time to internal time format (i.e., a time_t value)
  24. *
  25. *Purpose:
  26. *       Converts a local time value, obtained in a broken down format from
  27. *       the host OS, to time_t format (i.e., the number elapsed seconds since
  28. *       01-01-70, 00:00:00, UTC).
  29. *
  30. *Entry:
  31. *       int yr, mo, dy -    date
  32. *       int hr, mn, sc -    time
  33. *       int dstflag    -    1 if Daylight Time, 0 if Standard Time, -1 if
  34. *                           not specified.
  35. *
  36. *Exit:
  37. *       Returns calendar time value.
  38. *
  39. *Exceptions:
  40. *
  41. *******************************************************************************/
  42.  
  43. time_t __cdecl __loctotime_t (
  44.         int yr,         /* 0 based */
  45.         int mo,         /* 1 based */
  46.         int dy,         /* 1 based */
  47.         int hr,
  48.         int mn,
  49.         int sc,
  50.         int dstflag )
  51. {
  52.         int tmpdays;
  53.         long tmptim;
  54.         struct tm tb;
  55.  
  56.         /*
  57.          * Do a quick range check on the year and convert it to a delta
  58.          * off of 1900.
  59.          */
  60.         if ( ((long)(yr -= 1900) < _BASE_YEAR) || ((long)yr > _MAX_YEAR) )
  61.                 return (time_t)(-1);
  62.  
  63.         /*
  64.          * Compute the number of elapsed days in the current year. Note the
  65.          * test for a leap year would fail in the year 2100, if this was in
  66.          * range (which it isn't).
  67.          */
  68.         tmpdays = dy + _days[mo - 1];
  69.         if ( !(yr & 3) && (mo > 2) )
  70.                 tmpdays++;
  71.  
  72.         /*
  73.          * Compute the number of elapsed seconds since the Epoch. Note the
  74.          * computation of elapsed leap years would break down after 2100
  75.          * if such values were in range (fortunately, they aren't).
  76.          */
  77.         tmptim = /* 365 days for each year */
  78.                  (((long)yr - _BASE_YEAR) * 365L
  79.  
  80.                  /* one day for each elapsed leap year */
  81.                  + (long)((yr - 1) >> 2) - _LEAP_YEAR_ADJUST
  82.  
  83.                  /* number of elapsed days in yr */
  84.                  + tmpdays)
  85.  
  86.                  /* convert to hours and add in hr */
  87.                  * 24L + hr;
  88.  
  89.         tmptim = /* convert to minutes and add in mn */
  90.                  (tmptim * 60L + mn)
  91.  
  92.                  /* convert to seconds and add in sec */
  93.                  * 60L + sc;
  94.         /*
  95.          * Account for time zone.
  96.          */
  97.         __tzset();
  98.         tmptim += _timezone;
  99.  
  100.         /*
  101.          * Fill in enough fields of tb for _isindst(), then call it to
  102.          * determine DST.
  103.          */
  104.         tb.tm_yday = tmpdays;
  105.         tb.tm_year = yr;
  106.         tb.tm_mon  = mo - 1;
  107.         tb.tm_hour = hr;
  108.         if ( (dstflag == 1) || ((dstflag == -1) && _daylight &&
  109.                                 _isindst(&tb)) )
  110.                 tmptim += _dstbias;
  111.         return(tmptim);
  112. }
  113.  
  114.  
  115.  
  116.  
  117.  
  118. #else  /* _WIN32 */
  119.  
  120. #if defined (_M_MPPC) || defined (_M_M68K)
  121.  
  122.  
  123. #include <cruntime.h>
  124. #include <time.h>
  125. #include <ctime.h>
  126. #include <internal.h>
  127.  
  128. /***
  129. *time_t _gmtotime_t(yr, mo, dy, hr, mn, sc) - convert broken down time (UTC)
  130. *   to time_t
  131. *
  132. *Purpose:
  133. *       Converts a broken down UTC (GMT) time to time_t. This is similar to
  134. *       _mkgmtime() except there is minimal overflow checking and no updating
  135. *       of the input values (i.e., the fields of tm structure).
  136. *
  137. *Entry:
  138. *       int yr, mo, dy -        date
  139. *       int hr, mn, sc -        time
  140. *
  141. *Exit:
  142. *       returns time_t value
  143. *
  144. *Exceptions:
  145. *
  146. *******************************************************************************/
  147.  
  148. time_t __cdecl _gmtotime_t (
  149.         int yr,     /* 0 based */
  150.         int mo,     /* 1 based */
  151.         int dy,     /* 1 based */
  152.         int hr,
  153.         int mn,
  154.         int sc
  155.         )
  156. {
  157.         int tmpdays;
  158.         long tmptim;
  159.         struct tm tb;
  160.  
  161.         /*
  162.          * Do a quick range check on the year and convert it to a delta
  163.          * off of 1900.
  164.          */
  165.         if ( ((long)(yr -= 1900) < _BASE_YEAR) || ((long)yr > _MAX_YEAR) )
  166.                 return (time_t)(-1);
  167.  
  168.         /*
  169.          * Compute the number of elapsed days in the current year minus
  170.          * one. Note the test for leap year and the would fail in the year 2100
  171.          * if this was in range (which it isn't).
  172.          */
  173.         tmpdays = dy + _days[mo - 1];
  174.         if ( !(yr & 3) && (mo > 2) )
  175.                 /*
  176.                  * in a leap year, after Feb. add one day for elapsed
  177.                  * Feb 29.
  178.                  */
  179.                 tmpdays++;
  180.  
  181.         /*
  182.          * Compute the number of elapsed seconds since the Epoch. Note the
  183.          * computation of elapsed leap years would break down after 2100
  184.          * if such values were in range (fortunately, they aren't).
  185.          */
  186.  
  187.         tmptim = (long)yr - _BASE_YEAR;
  188.  
  189.         tmptim = /* 365 days for each year */
  190.                  ( ( ( ( tmptim ) * 365L
  191.  
  192.                  /* one day for each elapsed leap year */
  193.                  + ((long)(yr - 1) >> 2) - (long)_LEAP_YEAR_ADJUST
  194.  
  195.                  /* number of elapsed days in yr */
  196.                  + (long)tmpdays )
  197.  
  198.                  /* convert to hours and add in hr */
  199.                  * 24L + (long)hr )
  200.  
  201.                  /* convert to minutes and add in mn */
  202.                  * 60L + (long)mn )
  203.  
  204.                  /* convert to seconds and add in sec */
  205.                  * 60L + (long)sc;
  206.  
  207.         _tzset();
  208.  
  209.         tmptim += _timezone;        //timezone adjustment
  210.  
  211.  
  212.         /*
  213.          * Fill in enough fields of tb struct for _isindst(), then call it to
  214.          * determine DST.
  215.          */
  216.         tb.tm_yday = tmpdays;
  217.         tb.tm_year = yr;
  218.         tb.tm_mon = mo - 1;
  219.         tb.tm_hour = hr;
  220.         if (_daylight && _isindst(&tb))
  221.                 tmptim -= 3600L;
  222.  
  223.         return (tmptim >= 0) ? (time_t)tmptim : (time_t)(-1);
  224. }
  225.  
  226.  
  227. #endif  /* defined (_M_MPPC) || defined (_M_M68K) */
  228.  
  229. #endif  /* _WIN32 */
  230.