time.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (C) 2001-2007 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. */
  33. /*
  34. * $Log$
  35. * Revision 1.7 2009/02/13 14:52:05 haraldkipp
  36. * Include memdebug.h for heap management debugging support.
  37. *
  38. * Revision 1.6 2008/08/11 06:59:41 haraldkipp
  39. * BSD types replaced by stdint types (feature request #1282721).
  40. *
  41. * Revision 1.5 2007/06/03 08:49:56 haraldkipp
  42. * Bug #1724015 fixed. Simplified code in time().
  43. *
  44. * Revision 1.4 2006/10/05 17:20:13 haraldkipp
  45. * Standard C functions now support hardware RTC.
  46. * Ugly type conversion replaced.
  47. *
  48. * Revision 1.3 2003/12/19 22:26:38 drsung
  49. * Dox written.
  50. *
  51. * Revision 1.2 2003/11/26 11:14:32 haraldkipp
  52. * Portability issues
  53. *
  54. * Revision 1.1 2003/11/24 18:07:37 drsung
  55. * first release
  56. *
  57. *
  58. */
  59. #include <cfg/os.h>
  60. #include <stdint.h>
  61. #include <time.h>
  62. #include <sys/time.h>
  63. #include <sys/timer.h>
  64. #include <sys/atom.h>
  65. #include <stdlib.h>
  66. #include <memdebug.h>
  67. #include <dev/rtc.h>
  68. struct timeval epo_offs = {0, 0};
  69. /*!
  70. * \addtogroup xgCrtTime
  71. *
  72. */
  73. /*@{*/
  74. /*!
  75. * \brief Get the system time.
  76. *
  77. * The \b time function returns the number of seconds elapsed since midnight (00:00:00), January 1, 1970,
  78. * coordinated universal time (UTC), according to the system clock. The return value is stored in the location
  79. * given by \e timer. This parameter may be \b NULL, in which case the return value is not stored.
  80. *
  81. * \param timer Pointer to the storage location for time.
  82. * \return Return the time in elapsed seconds. There is no error return.
  83. *
  84. */
  85. time_t time(time_t * timer)
  86. {
  87. /* Initially use internal seconds counter. */
  88. time_t r = epo_offs.tv_sec + NutGetSeconds();
  89. #ifdef NUT_USE_OLD_TIME_API
  90. struct _tm tm;
  91. /* Try to get time from hardware clock. */
  92. tm.tm_isdst = -1;
  93. if (NutRtcGetTime(&tm) == 0) {
  94. /* Success. */
  95. r = _mkgmtime(&tm);
  96. }
  97. #endif
  98. /* Return result. */
  99. if (timer) {
  100. *timer = r;
  101. }
  102. return r;
  103. }
  104. /*!
  105. * \brief Set the system time.
  106. *
  107. * \param timer Pointer to the storage location for time.
  108. * \return This function always returns 0.
  109. *
  110. */
  111. int stime(time_t * timer)
  112. {
  113. #ifdef NUT_USE_OLD_TIME_API
  114. /* Try to set hardware clock. */
  115. NutRtcSetTime(gmtime(timer));
  116. #endif
  117. /* Set internal seconds counter. */
  118. epo_offs.tv_sec = (uint32_t)(*timer) - NutGetSeconds();
  119. epo_offs.tv_usec = 0;
  120. return 0;
  121. }
  122. /*@}*/