gmtime.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. ********************************************************************************
  3. **
  4. ** \file ./boot/startup/src/gmtime.c
  5. **
  6. ** \version $Id: gmtime.c 6304 2016-03-18 03:39:06Z chenke $
  7. **
  8. ** \brief ARM1176 RealView libc function retargeting.
  9. **
  10. ** This files retargets the time-specific function gmtime().
  11. **
  12. ** \attention THIS SAMPLE CODE IS PROVIDED AS IS. FUJITSU MICROELECTRONICS
  13. ** ACCEPTS NO RESPONSIBILITY OR LIABILITY FOR ANY ERRORS OR
  14. ** OMMISSIONS.
  15. **
  16. ** (C) Copyright 2006-2010 by Fujitsu Microelectronics Europe GmbH
  17. ** (C) Copyright 2010 by Fujitsu Semiconductor Europe GmbH
  18. **
  19. ********************************************************************************
  20. **
  21. ** \note The following LINT statements have been suppressed:
  22. **
  23. ** - Warning 586: function 'mktime' is deprecated. [MISRA 2004 Rule 20.12]
  24. **
  25. *****************************************************************************
  26. */
  27. //**************************************************************************
  28. //**************************************************************************
  29. //** Standard includes
  30. //**************************************************************************
  31. //**************************************************************************
  32. #include <stdint.h>
  33. //#include <errno_ext.h>
  34. #include <errno.h>
  35. #include <string.h>
  36. //#include <time_ext.h>
  37. #include <time.h>
  38. #include <internal.h>
  39. //**************************************************************************
  40. //**************************************************************************
  41. //** FAPI includes
  42. //**************************************************************************
  43. //**************************************************************************
  44. //**************************************************************************
  45. //**************************************************************************
  46. //** Defines and Macros
  47. //**************************************************************************
  48. //**************************************************************************
  49. /*lint -save -e(961) */
  50. #define IS_HIGH_BIT_SET(val) (((1LL << ((sizeof(val) * 8) - 1)) & val) != 0)
  51. /*lint -restore */
  52. //**************************************************************************
  53. //**************************************************************************
  54. //** Global Data
  55. //**************************************************************************
  56. //**************************************************************************
  57. static struct tm _gmtime_tm;
  58. //******************************************************************************
  59. //******************************************************************************
  60. //** Local Functions Declaration
  61. //******************************************************************************
  62. //******************************************************************************
  63. //******************************************************************************
  64. //******************************************************************************
  65. //** API Functions
  66. //******************************************************************************
  67. //******************************************************************************
  68. /*
  69. ********************************************************************************
  70. ** \brief Retarget implementation of the stdc library function.
  71. **
  72. ** This is the standard C library gmtime() function from time.h.
  73. **
  74. ** The gmtime() function shall convert the time in seconds since the epoch
  75. ** pointed to by timer into a broken-down time, expressed as coordinated
  76. ** universal time (UTC).
  77. **
  78. ** The relationship between a time in seconds since the Epoch used as an
  79. ** argument to gmtime() and the tm structure (defined in the <time.h> header) is
  80. ** that the result shall be as specified in the expression given in the
  81. ** definition of seconds since the Epoch (see the Base Definitions volume of
  82. ** IEEE Std 1003.1-2001, Section 4.14, Seconds Since the Epoch), where the names
  83. ** in the structure and in the expression correspond.
  84. **
  85. ** The asctime(), ctime(), gmtime(), and localtime() functions shall return
  86. ** values in one of two static objects: a broken-down time structure and an
  87. ** array of type char. Execution of any of the functions may overwrite the
  88. ** information returned in either of these objects by any of the other
  89. ** functions.
  90. **
  91. ** \param timer Pointer to time in seconds since the epoch (00:00:00 UTC on
  92. ** 1 January 1970)
  93. **
  94. ** \return Upon successful completion, the gmtime() function shall return a
  95. ** pointer to a struct tm. If an error is detected, gmtime() shall return a null
  96. ** pointer and set errno to indicate the error.
  97. ** ERRORS:
  98. ** - EOVERFLOW The result cannot be represented.
  99. ********************************************************************************
  100. */
  101. struct tm * gmtime(const time_t *timer)
  102. {
  103. struct tm *tm = &_gmtime_tm;
  104. uint32_t cpu_flags;
  105. /* check parameter validity */
  106. if (timer == NULL) {
  107. return (NULL);
  108. }
  109. /* irrespective of values sign representation, high bit set in time_t leads
  110. to an invalid (overflow) result */
  111. if (IS_HIGH_BIT_SET(*timer)) {
  112. /*lint -e{48,63} */
  113. errno = EOVERFLOW;
  114. return (NULL);
  115. }
  116. //////////////////////////////////
  117. cpu_flags = st_enter_crit_func();
  118. //////////////////////////////////
  119. /* reset local time structure */
  120. memset(tm, 0x0, sizeof(struct tm));
  121. /* fill up time based on epoch using given seconds */
  122. tm->tm_year = 70;
  123. tm->tm_mday = 1;
  124. tm->tm_sec = (int) *timer;
  125. /* normalize data */
  126. /*lint -e(586) */
  127. mktime(tm);
  128. //////////////////////////////
  129. st_exit_crit_func(cpu_flags);
  130. //////////////////////////////
  131. return (tm);
  132. }