localtime.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (C) 2001-2003 by egnite Software GmbH. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. *
  32. * Portions of the following functions are derived from material which is
  33. * Copyright (c) 1985 by Microsoft Corporation. All rights are reserved.
  34. */
  35. /*
  36. * $Log$
  37. * Revision 1.6 2009/01/17 15:37:52 haraldkipp
  38. * Added some NUTASSERT macros to check function parameters.
  39. *
  40. * Revision 1.5 2008/08/11 06:59:40 haraldkipp
  41. * BSD types replaced by stdint types (feature request #1282721).
  42. *
  43. * Revision 1.4 2004/10/14 16:43:00 drsung
  44. * Fixed compiler warning "comparison between signed and unsigned"
  45. *
  46. * Revision 1.3 2003/12/19 22:26:37 drsung
  47. * Dox written.
  48. *
  49. * Revision 1.2 2003/11/26 11:14:32 haraldkipp
  50. * Portability issues
  51. *
  52. * Revision 1.1 2003/11/24 18:07:37 drsung
  53. * first release
  54. *
  55. *
  56. */
  57. #include <stdint.h>
  58. #include <time.h>
  59. #include "ctime.h"
  60. #include <sys/nutdebug.h>
  61. #define __need_NULL
  62. #include <stddef.h>
  63. #define LONG_MAX 2147483647L
  64. /*!
  65. * \addtogroup xgCrtTime
  66. * @{
  67. */
  68. /*!
  69. * \brief Convert a time value and correct for the local time zone.
  70. *
  71. * Thread safe version of \b localtime. See ::localtime for more information.
  72. *
  73. * \param timer Pointer to stored time.
  74. * \param ptm Pointer to structure ::tm where the converted time is stored.
  75. * \return Always return 0.
  76. *
  77. */
  78. int localtime_r(const time_t * timer, tm * ptm)
  79. {
  80. long ltime;
  81. NUTASSERT(timer != NULL);
  82. if ((*timer > (time_t)(3 * _DAY_SEC)) && (*timer < (time_t)(LONG_MAX - 3 * _DAY_SEC))) {
  83. /*
  84. * The date does not fall within the first three, or last
  85. * three, representable days of the Epoch. Therefore, there
  86. * is no possibility of overflowing or underflowing the
  87. * time_t representation as we compensate for timezone and
  88. * Daylight Savings Time.
  89. */
  90. ltime = (long) *timer - _timezone;
  91. gmtime_r((time_t *) & ltime, ptm);
  92. /*
  93. * Check and adjust for Daylight Saving Time.
  94. */
  95. if (_daylight && _isindst(ptm)) {
  96. ltime -= _dstbias;
  97. gmtime_r((time_t *) & ltime, ptm);
  98. ptm->tm_isdst = 1;
  99. }
  100. } else {
  101. gmtime_r(timer, ptm);
  102. /*
  103. * The date falls with the first three, or last three days
  104. * of the Epoch. It is possible the time_t representation
  105. * would overflow or underflow while compensating for
  106. * timezone and Daylight Savings Time. Therefore, make the
  107. * timezone and Daylight Savings Time adjustments directly
  108. * in the tm structure. The beginning of the Epoch is
  109. * 00:00:00, 01-01-70 (UCT) and the last representable second
  110. * in the Epoch is 03:14:07, 01-19-2038 (UCT). This will be
  111. * used in the calculations below.
  112. *
  113. * First, adjust for the timezone.
  114. */
  115. if (_isindst(ptm))
  116. ltime = (long) ptm->tm_sec - (_timezone + _dstbias);
  117. else
  118. ltime = (long) ptm->tm_sec - _timezone;
  119. ptm->tm_sec = (int) (ltime % 60);
  120. if (ptm->tm_sec < 0) {
  121. ptm->tm_sec += 60;
  122. ltime -= 60;
  123. }
  124. ltime = (long) ptm->tm_min + ltime / 60;
  125. ptm->tm_min = (int) (ltime % 60);
  126. if (ptm->tm_min < 0) {
  127. ptm->tm_min += 60;
  128. ltime -= 60;
  129. }
  130. ltime = (long) ptm->tm_hour + ltime / 60;
  131. ptm->tm_hour = (int) (ltime % 24);
  132. if (ptm->tm_hour < 0) {
  133. ptm->tm_hour += 24;
  134. ltime -= 24;
  135. }
  136. ltime /= 24;
  137. if (ltime > 0L) {
  138. /*
  139. * There is no possibility of overflowing the tm_mday
  140. * and tm_yday fields since the date can be no later
  141. * than January 19.
  142. */
  143. ptm->tm_wday = (ptm->tm_wday + ltime) % 7;
  144. ptm->tm_mday += ltime;
  145. ptm->tm_yday += ltime;
  146. } else if (ltime < 0L) {
  147. /*
  148. * It is possible to underflow the tm_mday and tm_yday
  149. * fields. If this happens, then adjusted date must
  150. * lie in December 1969.
  151. */
  152. ptm->tm_wday = (ptm->tm_wday + 7 + ltime) % 7;
  153. if ((ptm->tm_mday += ltime) <= 0) {
  154. ptm->tm_mday += 31;
  155. ptm->tm_yday = 364;
  156. ptm->tm_mon = 11;
  157. ptm->tm_year--;
  158. } else {
  159. ptm->tm_yday += ltime;
  160. }
  161. }
  162. }
  163. return 0;
  164. }
  165. /*!
  166. * \brief Convert a time value and correct for the local time zone.
  167. *
  168. * The localtime function converts a time stored as a time_t value and stores the
  169. * result in a structure of type ::tm. The long value \e timer represents the seconds
  170. * elapsed since midnight (00:00:00), January 1, 1970, UTC. This value is usually
  171. * obtained from the ::time function.
  172. *
  173. * ::gmtime, ::mktime, and \b localtime all use a single statically allocated ::tm structure
  174. * for the conversion. Each call to one of these routines destroys the result of the
  175. * previous call.
  176. *
  177. * \b localtime corrects for the local time zone if the user first sets the global
  178. * variable #_timezone.
  179. *
  180. * \param timer Pointer to stored time.
  181. * \return Return a pointer to the structure result. If the value in \e timer
  182. * represents a date before midnight, January 1, 1970, return \b NULL.
  183. *
  184. */
  185. /*
  186. Note: This function is *not* thread safe, because it uses a static variable
  187. to store the calculated values. To be safe, you must surround the call to localtime
  188. _and_ the usage of the returned pointer with NutEnterCritical() and NutExitCritical()!
  189. Provided for compatibility to std c lib.
  190. */
  191. tm *localtime(const time_t * timer)
  192. {
  193. if (localtime_r(timer, &_tb))
  194. return NULL;
  195. else
  196. return &_tb;
  197. }
  198. /*@}*/