Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
2.9 KiB

  1. /***
  2. *dtoxtime.c - convert broken-down UTC time to time_t
  3. *
  4. * Copyright (c) 1985-1993, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines __gmtotime_t() - convert broken-down UTC time to internal
  8. * format (time_t).
  9. *
  10. *Revision History:
  11. * 03-??-84 RLB written
  12. * 11-18-87 SKS change tzset() to __tzset(), change source file name
  13. * make _dtoxtime a near procedure
  14. * 01-26-88 SKS _dtoxtime is no longer a near procedure (for QC)
  15. * 03-20-90 GJF Made calling type _CALLTYPE1, added #include
  16. * <cruntime.h>, removed #include <register.h> and
  17. * fixed the copyright. Also, cleaned up the formatting
  18. * a bit.
  19. * 10-04-90 GJF New-style function declarator.
  20. * 01-21-91 GJF ANSI naming.
  21. * 05-19-92 DJM ifndef for POSIX build.
  22. * 03-30-93 GJF Revised. Old _dtoxtime is replaced by __gmtotime_t,
  23. * which is more useful on Win32.
  24. * 04-06-93 GJF Rewrote computation to avoid compiler warnings.
  25. *
  26. *******************************************************************************/
  27. #ifndef _POSIX_
  28. #include <cruntime.h>
  29. #include <time.h>
  30. #include <ctime.h>
  31. #include <internal.h>
  32. /***
  33. *time_t __gmtotime_t(yr, mo, dy, hr, mn, sc) - convert broken down time (UTC)
  34. * to time_t
  35. *
  36. *Purpose:
  37. * Converts a broken down UTC (GMT) time to time_t. This is similar to
  38. * _mkgmtime() except there is minimal overflow checking and no updating
  39. * of the input values (i.e., the fields of tm structure).
  40. *
  41. *Entry:
  42. * int yr, mo, dy - date
  43. * int hr, mn, sc - time
  44. *
  45. *Exit:
  46. * returns time_t value
  47. *
  48. *Exceptions:
  49. *
  50. *******************************************************************************/
  51. time_t __cdecl __gmtotime_t (
  52. int yr, /* 0 based */
  53. int mo, /* 1 based */
  54. int dy, /* 1 based */
  55. int hr,
  56. int mn,
  57. int sc
  58. )
  59. {
  60. int tmpdays;
  61. long tmptim;
  62. /*
  63. * Do a quick range check on the year and convert it to a delta
  64. * off of 1900.
  65. */
  66. if ( ((long)(yr -= 1900) < _BASE_YEAR) || ((long)yr > _MAX_YEAR) )
  67. return (time_t)(-1);
  68. /*
  69. * Compute the number of elapsed days in the current year minus
  70. * one. Note the test for leap year and the would fail in the year 2100
  71. * if this was in range (which it isn't).
  72. */
  73. tmpdays = dy + _days[mo - 1];
  74. if ( !(yr & 3) && (mo > 2) )
  75. /*
  76. * in a leap year, after Feb. add one day for elapsed
  77. * Feb 29.
  78. */
  79. tmpdays++;
  80. /*
  81. * Compute the number of elapsed seconds since the Epoch. Note the
  82. * computation of elapsed leap years would break down after 2100
  83. * if such values were in range (fortunately, they aren't).
  84. */
  85. tmptim = /* 365 days for each year */
  86. (((long)yr - _BASE_YEAR) * 365L
  87. /* one day for each elapsed leap year */
  88. + (long)((yr - 1) >> 2) - _LEAP_YEAR_ADJUST
  89. /* number of elapsed days in yr */
  90. + tmpdays)
  91. /* convert to hours and add in hr */
  92. * 24L + hr;
  93. tmptim = /* convert to minutes and add in mn */
  94. (tmptim * 60L + mn)
  95. /* convert to seconds and add in sec */
  96. * 60L + sc;
  97. return (tmptim >= 0) ? (time_t)tmptim : (time_t)(-1);
  98. }
  99. #endif /* _POSIX_ */