time.c 4.7 KB

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