time.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #include <time.h>
  2. #include <rtthread.h>
  3. /* days per month -- nonleap! */
  4. const short __spm[13] =
  5. { 0,
  6. (31),
  7. (31+28),
  8. (31+28+31),
  9. (31+28+31+30),
  10. (31+28+31+30+31),
  11. (31+28+31+30+31+30),
  12. (31+28+31+30+31+30+31),
  13. (31+28+31+30+31+30+31+31),
  14. (31+28+31+30+31+30+31+31+30),
  15. (31+28+31+30+31+30+31+31+30+31),
  16. (31+28+31+30+31+30+31+31+30+31+30),
  17. (31+28+31+30+31+30+31+31+30+31+30+31),
  18. };
  19. static long int timezone;
  20. static const char days[] = "Sun Mon Tue Wed Thu Fri Sat ";
  21. static const char months[] = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ";
  22. /* seconds per day */
  23. #define SPD 24*60*60
  24. int __isleap(int year)
  25. {
  26. /* every fourth year is a leap year except for century years that are
  27. * not divisible by 400. */
  28. /* return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); */
  29. return (!(year % 4) && ((year % 100) || !(year % 400)));
  30. }
  31. struct tm *gmtime_r(const time_t *timep, struct tm *r)
  32. {
  33. time_t i;
  34. register time_t work = *timep % (SPD);
  35. r->tm_sec = work % 60;
  36. work /= 60;
  37. r->tm_min = work % 60;
  38. r->tm_hour = work / 60;
  39. work = *timep / (SPD);
  40. r->tm_wday = (4 + work) % 7;
  41. for (i = 1970;; ++i)
  42. {
  43. register time_t k = __isleap(i) ? 366 : 365;
  44. if (work >= k)
  45. work -= k;
  46. else
  47. break;
  48. }
  49. r->tm_year = i - 1900;
  50. r->tm_yday = work;
  51. r->tm_mday = 1;
  52. if (__isleap(i) && (work > 58))
  53. {
  54. if (work == 59)
  55. r->tm_mday = 2; /* 29.2. */
  56. work -= 1;
  57. }
  58. for (i = 11; i && (__spm[i] > work); --i)
  59. ;
  60. r->tm_mon = i;
  61. r->tm_mday += work - __spm[i];
  62. return r;
  63. }
  64. struct tm* localtime_r(const time_t* t, struct tm* r)
  65. {
  66. time_t tmp;
  67. struct timezone tz = {0};
  68. gettimeofday(0, &tz);
  69. timezone = tz.tz_minuteswest * 60L;
  70. tmp = *t + timezone;
  71. return gmtime_r(&tmp, r);
  72. }
  73. struct tm* localtime(const time_t* t)
  74. {
  75. static struct tm tmp;
  76. return localtime_r(t, &tmp);
  77. }
  78. time_t mktime(struct tm * const t)
  79. {
  80. register time_t day;
  81. register time_t i;
  82. register time_t years = t->tm_year - 70;
  83. if (t->tm_sec > 60)
  84. {
  85. t->tm_min += t->tm_sec / 60;
  86. t->tm_sec %= 60;
  87. }
  88. if (t->tm_min > 60)
  89. {
  90. t->tm_hour += t->tm_min / 60;
  91. t->tm_min %= 60;
  92. }
  93. if (t->tm_hour > 24)
  94. {
  95. t->tm_mday += t->tm_hour / 24;
  96. t->tm_hour %= 24;
  97. }
  98. if (t->tm_mon > 12)
  99. {
  100. t->tm_year += t->tm_mon / 12;
  101. t->tm_mon %= 12;
  102. }
  103. while (t->tm_mday > __spm[1 + t->tm_mon])
  104. {
  105. if (t->tm_mon == 1 && __isleap(t->tm_year + 1900))
  106. {
  107. --t->tm_mday;
  108. }
  109. t->tm_mday -= __spm[t->tm_mon];
  110. ++t->tm_mon;
  111. if (t->tm_mon > 11)
  112. {
  113. t->tm_mon = 0;
  114. ++t->tm_year;
  115. }
  116. }
  117. if (t->tm_year < 70)
  118. return (time_t) -1;
  119. /* Days since 1970 is 365 * number of years + number of leap years since 1970 */
  120. day = years * 365 + (years + 1) / 4;
  121. /* After 2100 we have to substract 3 leap years for every 400 years
  122. This is not intuitive. Most mktime implementations do not support
  123. dates after 2059, anyway, so we might leave this out for it's
  124. bloat. */
  125. if (years >= 131)
  126. {
  127. years -= 131;
  128. years /= 100;
  129. day -= (years >> 2) * 3 + 1;
  130. if ((years &= 3) == 3)
  131. years--;
  132. day -= years;
  133. }
  134. day += t->tm_yday = __spm[t->tm_mon] + t->tm_mday - 1 +
  135. (__isleap(t->tm_year + 1900) & (t->tm_mon > 1));
  136. /* day is now the number of days since 'Jan 1 1970' */
  137. i = 7;
  138. t->tm_wday = (day + 4) % i; /* Sunday=0, Monday=1, ..., Saturday=6 */
  139. i = 24;
  140. day *= i;
  141. i = 60;
  142. return ((day + t->tm_hour) * i + t->tm_min) * i + t->tm_sec;
  143. }
  144. static void num2str(char *c, int i)
  145. {
  146. c[0] = i / 10 + '0';
  147. c[1] = i % 10 + '0';
  148. }
  149. char *asctime_r(const struct tm *t, char *buf)
  150. {
  151. /* "Wed Jun 30 21:49:08 1993\n" */
  152. *(int*) buf = *(int*) (days + (t->tm_wday << 2));
  153. *(int*) (buf + 4) = *(int*) (months + (t->tm_mon << 2));
  154. num2str(buf + 8, t->tm_mday);
  155. if (buf[8] == '0')
  156. buf[8] = ' ';
  157. buf[10] = ' ';
  158. num2str(buf + 11, t->tm_hour);
  159. buf[13] = ':';
  160. num2str(buf + 14, t->tm_min);
  161. buf[16] = ':';
  162. num2str(buf + 17, t->tm_sec);
  163. buf[19] = ' ';
  164. num2str(buf + 20, (t->tm_year + 1900) / 100);
  165. num2str(buf + 22, (t->tm_year + 1900) % 100);
  166. buf[24] = '\n';
  167. return buf;
  168. }
  169. char *asctime(const struct tm *timeptr)
  170. {
  171. static char buf[25];
  172. return asctime_r(timeptr, buf);
  173. }
  174. char *ctime(const time_t *timep)
  175. {
  176. return asctime(localtime(timep));
  177. }
  178. #ifdef RT_USING_DEVICE
  179. int gettimeofday(struct timeval *tp, void *ignore)
  180. {
  181. time_t time;
  182. rt_device_t device;
  183. device = rt_device_find("rtc");
  184. if (device != RT_NULL)
  185. {
  186. rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time);
  187. if (tp != RT_NULL)
  188. {
  189. tp->tv_sec = time;
  190. tp->tv_usec = 0;
  191. }
  192. return time;
  193. }
  194. return 0;
  195. }
  196. #endif
  197. #ifndef _gettimeofday
  198. /* Dummy function when hardware do not have RTC */
  199. int _gettimeofday( struct timeval *tv, void *ignore)
  200. {
  201. tv->tv_sec = 0; // convert to seconds
  202. tv->tv_usec = 0; // get remaining microseconds
  203. return 0; // return non-zero for error
  204. }
  205. #endif
  206. /**
  207. * Returns the current time.
  208. *
  209. * @param time_t * t the timestamp pointer, if not used, keep NULL.
  210. *
  211. * @return time_t return timestamp current.
  212. *
  213. */
  214. /* for IAR 6.2 later Compiler */
  215. #if defined (__IAR_SYSTEMS_ICC__) && (__VER__) >= 6020000
  216. #pragma module_name = "?time"
  217. time_t (__time32)(time_t *t) /* Only supports 32-bit timestamp */
  218. #else
  219. time_t time(time_t *t)
  220. #endif
  221. {
  222. time_t time_now = 0;
  223. #ifdef RT_USING_RTC
  224. static rt_device_t device = RT_NULL;
  225. /* optimization: find rtc device only first. */
  226. if (device == RT_NULL)
  227. {
  228. device = rt_device_find("rtc");
  229. }
  230. /* read timestamp from RTC device. */
  231. if (device != RT_NULL)
  232. {
  233. if (rt_device_open(device, 0) == RT_EOK)
  234. {
  235. rt_device_control(device, RT_DEVICE_CTRL_RTC_GET_TIME, &time_now);
  236. rt_device_close(device);
  237. }
  238. }
  239. #endif /* RT_USING_RTC */
  240. /* if t is not NULL, write timestamp to *t */
  241. if (t != RT_NULL)
  242. {
  243. *t = time_now;
  244. }
  245. return time_now;
  246. }
  247. RT_WEAK clock_t clock(void)
  248. {
  249. return rt_tick_get();
  250. }