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
3.2 KiB

  1. /***
  2. *dtoxtm64.c - convert OS local time to __time64_t
  3. *
  4. * Copyright (c) 1998-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines __loctotime64_t() - convert OS local time to internal format
  8. * (__time64_t).
  9. *
  10. *Revision History:
  11. * 05-21-98 GJF Created.
  12. * 10-19-98 GJF Fill in tm_min and tm_sec before calling _isindst
  13. *
  14. *******************************************************************************/
  15. #include <cruntime.h>
  16. #include <time.h>
  17. #include <ctime.h>
  18. #include <internal.h>
  19. /***
  20. *__time64_t __loctotime64_t(yr, mo, dy, hr, mn, sc, dstflag) - converts OS
  21. * local time to internal time format (i.e., a __time64_t value)
  22. *
  23. *Purpose:
  24. * Converts a local time value, obtained in a broken down format from
  25. * the host OS, to __time64_t format (i.e., the number elapsed seconds
  26. * since 01-01-70, 00:00:00, UTC).
  27. *
  28. *Entry:
  29. * int yr, mo, dy - date
  30. * int hr, mn, sc - time
  31. * int dstflag - 1 if Daylight Time, 0 if Standard Time, -1 if
  32. * not specified.
  33. *
  34. *Exit:
  35. * Returns calendar time value.
  36. *
  37. *Exceptions:
  38. *
  39. *******************************************************************************/
  40. __time64_t __cdecl __loctotime64_t (
  41. int yr, /* 0 based */
  42. int mo, /* 1 based */
  43. int dy, /* 1 based */
  44. int hr,
  45. int mn,
  46. int sc,
  47. int dstflag )
  48. {
  49. int tmpdays;
  50. __time64_t tmptim;
  51. struct tm tb;
  52. /*
  53. * Do a quick range check on the year and convert it to a delta
  54. * off of 1900.
  55. */
  56. if ( ((long)(yr -= 1900) < _BASE_YEAR) || ((long)yr > _MAX_YEAR64) )
  57. return (__time64_t)(-1);
  58. /*
  59. * Compute the number of elapsed days in the current year.
  60. */
  61. tmpdays = dy + _days[mo - 1];
  62. if ( _IS_LEAP_YEAR(yr) && (mo > 2) )
  63. tmpdays++;
  64. /*
  65. * Compute the number of elapsed seconds since the Epoch. Note the
  66. * computation of elapsed leap years would break down after 2100
  67. * if such values were in range (fortunately, they aren't).
  68. */
  69. tmptim = /* 365 days for each year */
  70. (((__time64_t)yr - _BASE_YEAR) * 365
  71. /* one day for each elapsed leap year */
  72. + (__time64_t)_ELAPSED_LEAP_YEARS(yr)
  73. /* number of elapsed days in yr */
  74. + tmpdays)
  75. /* convert to hours and add in hr */
  76. * 24 + hr;
  77. tmptim = /* convert to minutes and add in mn */
  78. (tmptim * 60 + mn)
  79. /* convert to seconds and add in sec */
  80. * 60 + sc;
  81. /*
  82. * Account for time zone.
  83. */
  84. __tzset();
  85. tmptim += _timezone;
  86. /*
  87. * Fill in enough fields of tb for _isindst(), then call it to
  88. * determine DST.
  89. */
  90. tb.tm_yday = tmpdays;
  91. tb.tm_year = yr;
  92. tb.tm_mon = mo - 1;
  93. tb.tm_hour = hr;
  94. tb.tm_min = mn;
  95. tb.tm_sec = sc;
  96. if ( (dstflag == 1) || ((dstflag == -1) && _daylight &&
  97. _isindst(&tb)) )
  98. tmptim += _dstbias;
  99. return(tmptim);
  100. }