/wcelibcex-1.0/src/wce_mktime.c

https://bitbucket.org/raviyellani/opencv · C · 155 lines · 55 code · 25 blank · 75 comment · 11 complexity · a2af623bcf1eaadc59041fc2a77f51cb MD5 · raw file

  1. /*
  2. * $Id: wce_mktime.c,v 1.2 2006/04/09 16:48:18 mloskot Exp $
  3. *
  4. * Defines functions to convert struct tm to time_t value.
  5. *
  6. * Created by Mateusz Loskot (mateusz@loskot.net)
  7. *
  8. * Copyright (c) 2006 Taxus SI Ltd.
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining
  11. * a copy of this software and associated documentation files (the "Software"),
  12. * to deal in the Software without restriction, including without limitation
  13. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  14. * and/or sell copies of the Software, and to permit persons to whom
  15. * the Software is furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included
  18. * in all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  23. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  24. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  25. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
  26. * THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. *
  28. * MIT License:
  29. * http://opensource.org/licenses/mit-license.php
  30. *
  31. * Contact:
  32. * Taxus SI Ltd.
  33. * http://www.taxussi.com.pl
  34. *
  35. */
  36. #include <wce_time.h>
  37. #include <windows.h>
  38. /* Function used intenally to convert struct tm to a time_t value. */
  39. static time_t __wceex_mktime_internal(struct tm *tmbuff, time_t _loctime_offset);
  40. /*******************************************************************************
  41. * wceex_mktime - Convert local time to calendar value in seconds since epoch.
  42. *******************************************************************************/
  43. time_t wceex_mktime(struct tm *tmbuff)
  44. {
  45. time_t offset;
  46. TIME_ZONE_INFORMATION tzi;
  47. offset = 0;
  48. // Retrive timezone offset in seconds
  49. if (GetTimeZoneInformation(&tzi) != 0xFFFFFFFF)
  50. {
  51. offset += (tzi.Bias * 60);
  52. if (tzi.StandardDate.wMonth != 0)
  53. {
  54. offset += (tzi.StandardBias * 60);
  55. }
  56. }
  57. return __wceex_mktime_internal(tmbuff, offset);
  58. }
  59. /*******************************************************************************
  60. * wceex_gmmktime - Get Unix timestamp for a GMT date
  61. *
  62. * Description:
  63. * Given a struct tm representing a calendar time in UTC, convert it to
  64. * seconds since epoch.
  65. * Note that this function does not canonicalize the provided
  66. * struct tm, nor does it allow out of range values or years before 1970.
  67. * The tm struct values of tm_wday and tm_yday are ignored.
  68. *
  69. * Return:
  70. * Value of time if success, otherwise (time_t)-1 is returned.
  71. *
  72. *******************************************************************************/
  73. time_t wceex_gmmktime(struct tm *tmbuff)
  74. {
  75. return __wceex_mktime_internal(tmbuff, 0);
  76. }
  77. /*******************************************************************************
  78. * __wceex_mktime_internal - Convert struct tm to a time_t value.
  79. *
  80. * Description:
  81. * Use offset as difference in seconds between local time and UTC time.
  82. *
  83. /*******************************************************************************/
  84. /* The number of days in each month. */
  85. #define MONTHS_NUMBER 12
  86. static const int MONTHDAYS[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  87. static time_t __wceex_mktime_internal(struct tm *tmbuff, time_t _loctime_offset)
  88. {
  89. time_t tres;
  90. int doy;
  91. int i;
  92. /* We do allow some ill-formed dates, but we don't do anything special
  93. with them and our callers really shouldn't pass them to us. Do
  94. explicitly disallow the ones that would cause invalid array accesses
  95. or other algorithm problems. */
  96. if (tmbuff->tm_mon < 0 || tmbuff->tm_mon > 11 || tmbuff->tm_year < (EPOCH_YEAR - TM_YEAR_BASE))
  97. {
  98. return (time_t) -1;
  99. }
  100. /* Convert calender time to a time_t value. */
  101. tres = 0;
  102. /* Sum total amount of days from the Epoch with respect to leap years. */
  103. for (i = EPOCH_YEAR; i < tmbuff->tm_year + TM_YEAR_BASE; i++)
  104. {
  105. tres += 365 + IS_LEAP_YEAR(i);
  106. }
  107. /* Add days of months before current month. */
  108. doy = 0;
  109. for (i = 0; i < tmbuff->tm_mon; i++)
  110. {
  111. doy += MONTHDAYS[i];
  112. }
  113. tres += doy;
  114. /* Day of year */
  115. tmbuff->tm_yday = doy + tmbuff->tm_mday;
  116. if (tmbuff->tm_mon > 1 && IS_LEAP_YEAR(tmbuff->tm_year + TM_YEAR_BASE))
  117. {
  118. tres++;
  119. }
  120. /* Add days of current month and convert to total to hours. */
  121. tres = 24 * (tres + tmbuff->tm_mday - 1) + tmbuff->tm_hour;
  122. /* Add minutes part and convert total to minutes. */
  123. tres = 60 * tres + tmbuff->tm_min;
  124. /* Add seconds part and convert total to seconds. */
  125. tres = 60 * tres + tmbuff->tm_sec;
  126. /* For offset > 0 adjust time value for timezone
  127. given as local to UTC time difference in seconds). */
  128. tres += _loctime_offset;
  129. return tres;
  130. }