offtime.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /* Copyright (C) 1991, 1993, 1997, 1998, 2002 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, write to the Free
  13. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  14. 02111-1307 USA. */
  15. /******************************************************************************
  16. 版权所有:
  17. 文件名称: offtime.c
  18. 文件版本: 01.01
  19. 创建作者: sunxi
  20. 创建日期: 2010-05-06
  21. 功能说明: 时间格式转化。
  22. 其它说明: 原始文件来自gcc\glibc-2.6\time\offtime.c
  23. 修改记录:
  24. */
  25. /*------------------------------- 头文件 --------------------------------------
  26. */
  27. // #include <linux/time.h>
  28. #include "head.h"
  29. /*------------------------------- 宏定义 --------------------------------------
  30. */
  31. /* Nonzero if YEAR is a leap year (every 4 years,
  32. except every 100th isn't, and every 400th is). */
  33. #define __isleap(year) ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
  34. #define SECS_PER_HOUR (60 * 60)
  35. #define SECS_PER_DAY (SECS_PER_HOUR * 24)
  36. /*------------------------------ 类型结构 -------------------------------------
  37. */
  38. /*------------------------------ 全局变量 -------------------------------------
  39. */
  40. /* How many days come before each month (0-12). */
  41. const unsigned short int __mon_yday[2][13] =
  42. {
  43. /* Normal years. */
  44. {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
  45. /* Leap years. */
  46. {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}};
  47. /*------------------------------ 函数声明 -------------------------------------
  48. */
  49. uint32_t
  50. _mktime(const unsigned int year0, const unsigned int mon0,
  51. const unsigned int day, const unsigned int hour,
  52. const unsigned int min, const unsigned int sec);
  53. /*------------------------------ 外部函数 -------------------------------------
  54. 外部函数供其它实体文件引用,必须仔细检查传入参数的合法性.
  55. */
  56. /******************************************************************************
  57. 函数名称: rtc_to_timespec
  58. 函数版本: v1.00
  59. 创建作者: 杨海涛
  60. 创建日期: 2011-2-22
  61. 函数说明: 将rtc_time_t结构的数据转换为timespec格式
  62. 参数说明:
  63. p_ts: 输出的timespec结构的时间
  64. p_rtc: 输入的rtc_time_t结构的时间
  65. 返回值:
  66. 0: 成功
  67. -1: 失败
  68. 修改记录:
  69. */
  70. int rtc_to_timespec(struct rtc_time_t *p_rtc, struct timespec *p_ts)
  71. {
  72. #if 1
  73. if (check_rtc(p_rtc) != true)
  74. {
  75. return -1;
  76. }
  77. #else
  78. if (p_rtc->month > 12 || p_rtc->day > 31 || p_rtc->hour > 24 || p_rtc->min > 60 || p_rtc->ms > 60000)
  79. {
  80. return -1;
  81. }
  82. #endif
  83. // 将系统时间转换为timespec格式
  84. p_ts->tv_sec = _mktime(p_rtc->year + 2000, p_rtc->month, p_rtc->day, p_rtc->hour, p_rtc->min, (p_rtc->ms) / 1000);
  85. p_ts->tv_nsec = p_rtc->ms % 1000 * 1000000;
  86. // RTC和GPS中的时间是北京时间(东8区),我们需要将其调整为UTC时间。
  87. // p_ts->tv_sec -= 8*60*60;
  88. return 0;
  89. }
  90. /******************************************************************************
  91. 函数名称: timespec_to_rtc
  92. 函数版本: 01.01
  93. 创建作者: sunxi
  94. 创建日期: 2008-11-04
  95. 函数说明: 将timespec结构的数据转换为rtc_time_t格式
  96. 参数说明:
  97. ts: 输入的timespec结构的时间
  98. p_rtc: 输出的rtc_time_t结构的时间
  99. is_round: 是否需要四舍五入到ms
  100. 返回值:
  101. 0: 成功
  102. 其它: 失败
  103. 修改记录:
  104. 2015-5-14: sunxi 增加is_round参数。当只需要ms精度时,应该设置为1,使ms的精度更高;
  105. 如果需要此函数的结果和ts中ns组成更高精度的时间记录,应设置为0,且使用ns时,
  106. 也不应该进行四舍五入操作,否则将导致组合出来的时间不正确。(是否难以理解?直接问我好了!)
  107. */
  108. int timespec_to_rtc(struct timespec ts, struct rtc_time_t *p_rtc, int is_round)
  109. {
  110. long int days, rem, y;
  111. const unsigned short int *ip;
  112. if (p_rtc == 0)
  113. {
  114. return -1;
  115. }
  116. if ((uint32_t)ts.tv_nsec >= NSEC_PER_SEC)
  117. {
  118. memset(p_rtc, 0, sizeof(*p_rtc));
  119. return -2;
  120. }
  121. // 根据是否需要四舍五入对ms进行处理
  122. if (is_round)
  123. {
  124. ts.tv_nsec += NSEC_PER_MSEC / 2;
  125. if (ts.tv_nsec >= NSEC_PER_SEC)
  126. {
  127. ts.tv_nsec -= NSEC_PER_SEC;
  128. ts.tv_sec++;
  129. }
  130. }
  131. // 以日期为界,分开算
  132. days = ts.tv_sec / SECS_PER_DAY;
  133. rem = ts.tv_sec % SECS_PER_DAY;
  134. // 得到时分秒
  135. p_rtc->hour = rem / SECS_PER_HOUR;
  136. rem %= SECS_PER_HOUR;
  137. p_rtc->min = rem / 60;
  138. // ms,前面已作四舍五入处理
  139. p_rtc->ms = rem % 60 * MSEC_PER_SEC + ts.tv_nsec / NSEC_PER_MSEC;
  140. #define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
  141. #define LEAPS_THRU_END_OF(y) (DIV(y, 4) - DIV(y, 100) + DIV(y, 400))
  142. // 算年
  143. // 这个算法有点意思,一下子没有看懂,下次再看
  144. y = 1970; // UTC
  145. while (days < 0 || days >= (__isleap(y) ? 366 : 365))
  146. {
  147. /* Guess a corrected year, assuming 365 days per year. */
  148. long int yg = y + days / 365 - (days % 365 < 0);
  149. /* Adjust DAYS and Y to match the guessed year. */
  150. days -= ((yg - y) * 365 + LEAPS_THRU_END_OF(yg - 1) - LEAPS_THRU_END_OF(y - 1));
  151. y = yg;
  152. }
  153. // rtc_time_t中的年从2000年起
  154. p_rtc->year = y - 2000;
  155. if (p_rtc->year != y - 2000)
  156. {
  157. /* The year cannot be represented due to overflow. */
  158. memset(p_rtc, 0, sizeof(*p_rtc));
  159. return -3;
  160. }
  161. // 得到月日
  162. ip = __mon_yday[__isleap(y)];
  163. for (y = 11; days < (long int)ip[y]; --y)
  164. continue;
  165. days -= ip[y];
  166. // rtc_time_t中month是1~12
  167. // p_rtc->month = y;
  168. p_rtc->month = y + 1;
  169. p_rtc->day = days + 1;
  170. return 0;
  171. }
  172. int timespec_to_rtchms(struct timespec ts, struct rtc_time_t *p_rtc, int is_round)
  173. {
  174. long int rem;
  175. if (p_rtc == 0)
  176. {
  177. return -1;
  178. }
  179. if ((uint32_t)ts.tv_nsec >= NSEC_PER_SEC)
  180. {
  181. memset(p_rtc, 0, sizeof(*p_rtc));
  182. return -2;
  183. }
  184. // 根据是否需要四舍五入对ms进行处理
  185. if (is_round)
  186. {
  187. ts.tv_nsec += NSEC_PER_MSEC / 2;
  188. if (ts.tv_nsec >= NSEC_PER_SEC)
  189. {
  190. ts.tv_nsec -= NSEC_PER_SEC;
  191. ts.tv_sec++;
  192. }
  193. }
  194. // 以日期为界,分开算
  195. rem = ts.tv_sec % SECS_PER_DAY;
  196. // 得到时分秒
  197. p_rtc->hour = rem / SECS_PER_HOUR;
  198. rem %= SECS_PER_HOUR;
  199. p_rtc->min = rem / 60;
  200. // ms,前面已作四舍五入处理
  201. p_rtc->ms = rem % 60 * MSEC_PER_SEC + ts.tv_nsec / NSEC_PER_MSEC;
  202. return 0;
  203. }
  204. uint32_t mktimehms(const unsigned int hour,
  205. const unsigned int min, const unsigned int sec)
  206. {
  207. return (uint32_t)((hour * 60 + min) * 60 + sec); /* finally seconds */
  208. }
  209. int check_rtc(struct rtc_time_t *pct)
  210. {
  211. // 时间有效检查
  212. if (pct->year > 99)
  213. return false;
  214. if (pct->month > 12 || pct->month < 1)
  215. return false;
  216. if (pct->day > 31 || pct->day < 1)
  217. return false;
  218. if (pct->hour > 23)
  219. return false;
  220. if (pct->min > 59)
  221. return false;
  222. if (pct->ms >= 60000)
  223. return false;
  224. if (pct->month == 2) // 2月特殊处理
  225. {
  226. unsigned short year = pct->year + 2000;
  227. if (year % 4 == 0) // 闰年
  228. {
  229. if (pct->day > 29)
  230. return false;
  231. }
  232. else
  233. {
  234. if (pct->day > 28)
  235. return false;
  236. }
  237. }
  238. if (pct->month == 4 || pct->month == 6 || pct->month == 9 || pct->month == 11)
  239. {
  240. if (pct->day > 30)
  241. {
  242. return false;
  243. }
  244. }
  245. return true;
  246. }
  247. /*------------------------------ 内部函数 -------------------------------------
  248. 内部函数以下划线‘_’开头,不需要检查参数的合法性.
  249. */
  250. /* Converts Gregorian date to seconds since 1970-01-01 00:00:00.
  251. * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
  252. * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
  253. *
  254. * [For the Julian calendar (which was used in Russia before 1917,
  255. * Britain & colonies before 1752, anywhere else before 1582,
  256. * and is still in use by some communities) leave out the
  257. * -year/100+year/400 terms, and add 10.]
  258. *
  259. * This algorithm was first published by Gauss (I think).
  260. *
  261. * WARNING: this function will overflow on 2106-02-07 06:28:16 on
  262. * machines where long is 32-bit! (However, as time_t is signed, we
  263. * will already get problems at other places on 2038-01-19 03:14:08)
  264. */
  265. // 本函数来自Linux内核
  266. uint32_t
  267. _mktime(const unsigned int year0, const unsigned int mon0,
  268. const unsigned int day, const unsigned int hour,
  269. const unsigned int min, const unsigned int sec)
  270. {
  271. unsigned int mon = mon0, year = year0;
  272. /* 1..12 -> 11,12,1..10 */
  273. if (0 >= (int)(mon -= 2))
  274. {
  275. mon += 12; /* Puts Feb last since it has leap day */
  276. year -= 1;
  277. }
  278. return ((((uint32_t)(year / 4 - year / 100 + year / 400 + 367 * mon / 12 + day) +
  279. year * 365 - 719499) *
  280. 24 +
  281. hour /* now have hours */
  282. ) * 60 +
  283. min /* now have minutes */
  284. ) * 60 +
  285. sec; /* finally seconds */
  286. }
  287. /*------------------------------ 测试函数 -------------------------------------
  288. 一个实体文件必须带一个本模块的测试函数来进行单元测试,如果的确不方便在本模块中
  289. 进行单元测试,必须在此注明实际的测试位置(例如在哪个实体文件中使用哪个测试函数).
  290. */
  291. /*------------------------------ 文件结束 -------------------------------------
  292. */