gmtime.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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.8 2008/08/11 06:59:40 haraldkipp
  38. * BSD types replaced by stdint types (feature request #1282721).
  39. *
  40. * Revision 1.7 2005/08/02 17:46:47 haraldkipp
  41. * Major API documentation update.
  42. *
  43. * Revision 1.6 2004/10/14 16:43:00 drsung
  44. * Fixed compiler warning "comparison between signed and unsigned"
  45. *
  46. * Revision 1.5 2003/12/19 22:26:37 drsung
  47. * Dox written.
  48. *
  49. * Revision 1.4 2003/11/27 09:17:18 drsung
  50. * Wrong comment fixed
  51. *
  52. * Revision 1.3 2003/11/26 12:45:20 drsung
  53. * Portability issues ... again
  54. *
  55. * Revision 1.2 2003/11/26 11:13:17 haraldkipp
  56. * Portability issues
  57. *
  58. * Revision 1.1 2003/11/24 18:07:37 drsung
  59. * first release
  60. *
  61. *
  62. */
  63. #include <stdint.h>
  64. #include <time.h>
  65. #include "ctime.h"
  66. #define __need_NULL
  67. #include <stddef.h>
  68. tm _tb;
  69. /* static arrays used by gmtime to determine date and time
  70. * values. Shows days from being of year.
  71. ***************************************************************/
  72. int _lpdays[] = {
  73. -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365
  74. };
  75. int _days[] = {
  76. -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364
  77. };
  78. /*!
  79. * \addtogroup xgCrtTime
  80. * @{
  81. */
  82. /*!
  83. * \brief Convert a time value to a structure.
  84. *
  85. * Thread safe version of \b gmtime. See ::gmtime for more information.
  86. *
  87. * \param timer Pointer to stored time. The time is represented as seconds elapsed
  88. * since midnight (00:00:00), January 1, 1970, coordinated universal time (UTC).
  89. * \param ptm Pointer to structure ::tm where the converted time is stored.
  90. * \return Returns nonzero value if any error occured.
  91. *
  92. */
  93. int gmtime_r(const time_t * timer, tm * ptm)
  94. {
  95. time_t ctimer = *timer; /* var to calculate with */
  96. uint8_t isleapyear = 0; /* current year is leap year */
  97. uint32_t tmptimer;
  98. int *mdays; /* pointer to _numdayslp or _numdays */
  99. if (ptm == NULL) /* check pointer */
  100. return -1;
  101. /*
  102. First calculate the number of four-year-interval, so calculation
  103. of leap year will be simple. Btw, because 2000 IS a leap year and
  104. 2100 is out of range, this formula is so simple.
  105. */
  106. tmptimer = (uint32_t) (ctimer / _FOUR_YEAR_SEC);
  107. ctimer -= ((time_t) tmptimer * _FOUR_YEAR_SEC);
  108. /* Determine the correct year within the interval */
  109. tmptimer = (tmptimer * 4) + 70; /* 1970, 1974, 1978,... */
  110. if (ctimer >= (time_t)_YEAR_SEC) {
  111. tmptimer++; /* 1971, 1975, 1979,... */
  112. ctimer -= _YEAR_SEC;
  113. if (ctimer >= (time_t)_YEAR_SEC) {
  114. tmptimer++; /* 1972, 1976, 1980,... (all leap years!) */
  115. ctimer -= _YEAR_SEC;
  116. /* A leap year has 366 days, so compare to _YEAR_SEC + _DAY_SEC */
  117. if (ctimer >= (time_t)(_YEAR_SEC + _DAY_SEC)) {
  118. tmptimer++; /* 1973, 1977, 1981,... */
  119. ctimer -= (_YEAR_SEC + _DAY_SEC);
  120. } else
  121. isleapyear = 1; /*If leap year, set the flag */
  122. }
  123. }
  124. /*
  125. tmptimer now has the value for tm_year. ctimer now holds the
  126. number of elapsed seconds since the beginning of that year.
  127. */
  128. ptm->tm_year = tmptimer;
  129. /*
  130. Calculate days since January 1st and store it to tm_yday.
  131. Leave ctimer with number of elapsed seconds in that day.
  132. */
  133. ptm->tm_yday = (int) (ctimer / _DAY_SEC);
  134. ctimer -= (time_t) (ptm->tm_yday) * _DAY_SEC;
  135. /*
  136. Determine months since January (Note, range is 0 - 11)
  137. and day of month (range: 1 - 31)
  138. */
  139. if (isleapyear)
  140. mdays = _lpdays;
  141. else
  142. mdays = _days;
  143. for (tmptimer = 1; mdays[tmptimer] < ptm->tm_yday; tmptimer++);
  144. ptm->tm_mon = --tmptimer;
  145. ptm->tm_mday = ptm->tm_yday - mdays[tmptimer];
  146. /* Calculate day of week. Sunday is 0 */
  147. ptm->tm_wday = ((int) (*timer / _DAY_SEC) + _BASE_DOW) % 7;
  148. /* Calculate the time of day from the remaining seconds */
  149. ptm->tm_hour = (int) (ctimer / 3600);
  150. ctimer -= (time_t) ptm->tm_hour * 3600L;
  151. ptm->tm_min = (int) (ctimer / 60);
  152. ptm->tm_sec = (int) (ctimer - (ptm->tm_min) * 60);
  153. ptm->tm_isdst = 0;
  154. return 0;
  155. }
  156. /*!
  157. * \brief Convert a time value to a structure.
  158. *
  159. * The \b gmtime function breaks down the \e timer value and stores it in a statically
  160. * allocated structure of type ::tm, defined in time.h. The value of \e timer is usually
  161. * obtained from a call to the ::time function.
  162. *
  163. * \param timer Pointer to stored time. The time is represented as seconds elapsed
  164. * since midnight (00:00:00), January 1, 1970, coordinated universal time (UTC).
  165. * \return Returns a pointer to a structure of type ::tm. The fields of the returned
  166. * structure hold the evaluated value of the timer argument in UTC rather than in local time.
  167. *
  168. * \note This function is not thread safe, because it uses a static variable
  169. * to store the calculated values. To be safe, you must surround the call to \b gmtime
  170. * and the usage of the returned pointer with ::NutEnterCritical() and ::NutExitCritical()!
  171. *
  172. */
  173. tm *gmtime(const time_t * timer)
  174. {
  175. if (gmtime_r(timer, &_tb))
  176. return NULL;
  177. else
  178. return &_tb;
  179. }
  180. /*@}*/