你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

GPS,UTC和本地时间的显示器

[复制链接]
XinLiYF 发布时间:2018-3-7 20:34
GPS,UTC和本地时间的显示器

转载来源:GPS,UTC和本地时间的显示器

GitHub仓库:http://github.com/XinLiGitHub/GpsUtcAndLocalTime
PS:博文不再更新,后续更新会在GitHub仓库进行。

      GPS,UTC和本地时间的显示器。程序中涉及到朱利安日期的转换,详细介绍见维基百科[Julian day](http://en.wikipedia.org/wiki/Julian_day)。

1,开发环境
      1,操作系统:Windows 10 专业版
      2,IDE:Visual Studio 2015 专业版

2,程序源码
      DateTime.h文件

  1. /****************************************************************
  2. * Copyright (C) 2017, XinLi, all right reserved.
  3. * File name:    DateTime.h
  4. * Date:         2017.10.17
  5. * Description:  Date and time module header file.
  6. *****************************************************************/

  7. #ifndef __DATETIME_H
  8. #define __DATETIME_H

  9. /****************************************************************
  10. *                        Header include
  11. *****************************************************************/


  12. /****************************************************************
  13. *                       Macro definition
  14. *****************************************************************/


  15. /****************************************************************
  16. *                       Type definition
  17. *****************************************************************/


  18. /****************************************************************
  19. *                     Structure definition
  20. *****************************************************************/
  21. typedef struct
  22. {
  23.   int year;
  24.   int month;
  25.   int day;
  26.   int hour;
  27.   int minute;
  28.   int second;
  29. }DateTime;

  30. typedef struct
  31. {
  32.   int week;
  33.   int second;
  34. }GpsWeekSecond;


  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif  /* __cplusplus */

  38. /****************************************************************
  39. *                     Variable declaration
  40. *****************************************************************/


  41. /****************************************************************
  42. *                     Function declaration
  43. *****************************************************************/
  44. DateTime GregorianCalendarDateAddYear(DateTime time, int year);
  45. DateTime GregorianCalendarDateAddMonth(DateTime time, int month);
  46. DateTime GregorianCalendarDateAddWeek(DateTime time, int week);
  47. DateTime GregorianCalendarDateAddDay(DateTime time, int day);
  48. DateTime GregorianCalendarDateAddHour(DateTime time, int hour);
  49. DateTime GregorianCalendarDateAddMinute(DateTime time, int minute);
  50. DateTime GregorianCalendarDateAddSecond(DateTime time, int second);
  51. GpsWeekSecond GregorianCalendarDateToGpsWeekSecond(DateTime time);
  52. double GregorianCalendarDateToJulianDate(DateTime time);
  53. double GregorianCalendarDateToModifiedJulianDate(DateTime time);
  54. GpsWeekSecond GpsWeekSecondAddYear(GpsWeekSecond time, int year);
  55. GpsWeekSecond GpsWeekSecondAddMonth(GpsWeekSecond time, int month);
  56. GpsWeekSecond GpsWeekSecondAddWeek(GpsWeekSecond time, int week);
  57. GpsWeekSecond GpsWeekSecondAddDay(GpsWeekSecond time, int day);
  58. GpsWeekSecond GpsWeekSecondAddHour(GpsWeekSecond time, int hour);
  59. GpsWeekSecond GpsWeekSecondAddMinute(GpsWeekSecond time, int minute);
  60. GpsWeekSecond GpsWeekSecondAddSecond(GpsWeekSecond time, int second);
  61. DateTime GpsWeekSecondToGregorianCalendarDate(GpsWeekSecond time);
  62. double GpsWeekSecondToJulianDate(GpsWeekSecond time);
  63. double GpsWeekSecondToModifiedJulianDate(GpsWeekSecond time);
  64. double JulianDateAddYear(double jd, int year);
  65. double JulianDateAddMonth(double jd, int month);
  66. double JulianDateAddWeek(double jd, int week);
  67. double JulianDateAddDay(double jd, int day);
  68. double JulianDateAddHour(double jd, int hour);
  69. double JulianDateAddMinute(double jd, int minute);
  70. double JulianDateAddSecond(double jd, int second);
  71. DateTime JulianDateToGregorianCalendarDate(double jd);
  72. GpsWeekSecond JulianDateToGpsWeekSecond(double jd);
  73. double JulianDateToModifiedJulianDate(double jd);
  74. double ModifiedJulianDateAddYear(double mjd, int year);
  75. double ModifiedJulianDateAddMonth(double mjd, int month);
  76. double ModifiedJulianDateAddWeek(double mjd, int week);
  77. double ModifiedJulianDateAddDay(double mjd, int day);
  78. double ModifiedJulianDateAddHour(double mjd, int hour);
  79. double ModifiedJulianDateAddMinute(double mjd, int minute);
  80. double ModifiedJulianDateAddSecond(double mjd, int second);
  81. DateTime ModifiedJulianDateToGregorianCalendarDate(double mjd);
  82. GpsWeekSecond ModifiedJulianDateToGpsWeekSecond(double mjd);
  83. double ModifiedJulianDateToJulianDate(double mjd);

  84. #ifdef __cplusplus
  85. }
  86. #endif  /* __cplusplus */

  87. #endif  /* __DATETIME_H */
复制代码

      DateTime.c文件
  1. /****************************************************************
  2. * Copyright (C) 2017, XinLi, all right reserved.
  3. * File name:    DateTime.c
  4. * Date:         2017.10.17
  5. * Description:  Date and time module source file.
  6. *****************************************************************/

  7. /****************************************************************
  8. *                        Header include
  9. *****************************************************************/
  10. #include "DateTime.h"

  11. /****************************************************************
  12. *                       Global variables
  13. *****************************************************************/


  14. /****************************************************************
  15. *                     Function declaration
  16. *****************************************************************/


  17. /****************************************************************
  18. *                     Function definition
  19. *****************************************************************/

  20. /****************************************************************
  21. * Function:    GregorianCalendarDateAddYear
  22. * Description: Gregorian calendar date add year.
  23. * Input:       time: Gregorian calendar date.
  24. *              year: The number of year to add.
  25. * Output:
  26. * Return:      Gregorian calendar date.
  27. *****************************************************************/
  28. DateTime GregorianCalendarDateAddYear(DateTime time, int year)
  29. {
  30.   time.year += year;

  31.   if(time.month == 2)
  32.   {
  33.     int mday = 0;

  34.     if((((time.year % 4) == 0) && ((time.year % 100) != 0)) || ((time.year % 400) == 0))
  35.     {
  36.       mday = 29;
  37.     }
  38.     else
  39.     {
  40.       mday = 28;
  41.     }

  42.     if(time.day > mday)
  43.     {
  44.       time.month += 1;
  45.       time.day   -= mday;
  46.     }
  47.   }

  48.   return time;
  49. }

  50. /****************************************************************
  51. * Function:    GregorianCalendarDateAddMonth
  52. * Description: Gregorian calendar date add month.
  53. * Input:       time:  Gregorian calendar date.
  54. *              month: The number of month to add.
  55. * Output:
  56. * Return:      Gregorian calendar date.
  57. *****************************************************************/
  58. DateTime GregorianCalendarDateAddMonth(DateTime time, int month)
  59. {
  60.   time.year  += month / 12;
  61.   time.month += month % 12;

  62.   if(time.month > 12)
  63.   {
  64.     time.year  += 1;
  65.     time.month -= 12;
  66.   }

  67.   int mday = 0;

  68.   if((time.month == 1) || (time.month == 3) || (time.month == 5) || (time.month == 7) ||
  69.      (time.month == 8) || (time.month == 10) || (time.month == 12))
  70.   {
  71.     mday = 31;
  72.   }
  73.   else if((time.month == 4) || (time.month == 6) || (time.month == 9) || (time.month == 11))
  74.   {
  75.     mday = 30;
  76.   }
  77.   else
  78.   {
  79.     if((((time.year % 4) == 0) && ((time.year % 100) != 0)) || ((time.year % 400) == 0))
  80.     {
  81.       mday = 29;
  82.     }
  83.     else
  84.     {
  85.       mday = 28;
  86.     }
  87.   }

  88.   if(time.day > mday)
  89.   {
  90.     time.month += 1;
  91.     time.day   -= mday;

  92.     if(time.month > 12)
  93.     {
  94.       time.year  += 1;
  95.       time.month -= 12;
  96.     }
  97.   }

  98.   return time;
  99. }

  100. /****************************************************************
  101. * Function:    GregorianCalendarDateAddWeek
  102. * Description: Gregorian calendar date add week.
  103. * Input:       time: Gregorian calendar date.
  104. *              week: The number of week to add.
  105. * Output:
  106. * Return:      Gregorian calendar date.
  107. *****************************************************************/
  108. DateTime GregorianCalendarDateAddWeek(DateTime time, int week)
  109. {
  110.   double jd = GregorianCalendarDateToJulianDate(time) + week * 7.0;

  111.   return JulianDateToGregorianCalendarDate(jd);
  112. }

  113. /****************************************************************
  114. * Function:    GregorianCalendarDateAddDay
  115. * Description: Gregorian calendar date add day.
  116. * Input:       time: Gregorian calendar date.
  117. *              day:  The number of day to add.
  118. * Output:
  119. * Return:      Gregorian calendar date.
  120. *****************************************************************/
  121. DateTime GregorianCalendarDateAddDay(DateTime time, int day)
  122. {
  123.   double jd = GregorianCalendarDateToJulianDate(time) + day;

  124.   return JulianDateToGregorianCalendarDate(jd);
  125. }

  126. /****************************************************************
  127. * Function:    GregorianCalendarDateAddHour
  128. * Description: Gregorian calendar date add hour.
  129. * Input:       time: Gregorian calendar date.
  130. *              hour: The number of hour to add.
  131. * Output:
  132. * Return:      Gregorian calendar date.
  133. *****************************************************************/
  134. DateTime GregorianCalendarDateAddHour(DateTime time, int hour)
  135. {
  136.   time.hour += hour;

  137.   double jd = GregorianCalendarDateToJulianDate(time);

  138.   return JulianDateToGregorianCalendarDate(jd);
  139. }

  140. /****************************************************************
  141. * Function:    GregorianCalendarDateAddMinute
  142. * Description: Gregorian calendar date add minute.
  143. * Input:       time:   Gregorian calendar date.
  144. *              minute: The number of minute to add.
  145. * Output:
  146. * Return:      Gregorian calendar date.
  147. *****************************************************************/
  148. DateTime GregorianCalendarDateAddMinute(DateTime time, int minute)
  149. {
  150.   time.minute += minute;

  151.   double jd = GregorianCalendarDateToJulianDate(time);

  152.   return JulianDateToGregorianCalendarDate(jd);
  153. }

  154. /****************************************************************
  155. * Function:    GregorianCalendarDateAddSecond
  156. * Description: Gregorian calendar date add second.
  157. * Input:       time:   Gregorian calendar date.
  158. *              second: The number of seconds to add.
  159. * Output:
  160. * Return:      Gregorian calendar date.
  161. *****************************************************************/
  162. DateTime GregorianCalendarDateAddSecond(DateTime time, int second)
  163. {
  164.   time.second += second;

  165.   double jd = GregorianCalendarDateToJulianDate(time);

  166.   return JulianDateToGregorianCalendarDate(jd);
  167. }

  168. /****************************************************************
  169. * Function:    GregorianCalendarDateToGpsWeekSecond
  170. * Description: Gregorian calendar date to gps week and second.
  171. * Input:       time: Gregorian calendar date.
  172. * Output:
  173. * Return:      Gps week and second.
  174. *****************************************************************/
  175. GpsWeekSecond GregorianCalendarDateToGpsWeekSecond(DateTime time)
  176. {
  177.   double jd = GregorianCalendarDateToJulianDate(time);

  178.   return JulianDateToGpsWeekSecond(jd);
  179. }

  180. /****************************************************************
  181. * Function:    GregorianCalendarDateToJulianDate
  182. * Description: Gregorian calendar date to julian date.
  183. * Input:       time: Gregorian calendar date.
  184. * Output:
  185. * Return:      Julian date.
  186. *****************************************************************/
  187. double GregorianCalendarDateToJulianDate(DateTime time)
  188. {
  189.   int jdn = (1461 * (time.year + 4800 + (time.month - 14) / 12)) / 4
  190.           + (367 * (time.month - 2 - 12 * ((time.month - 14) / 12))) / 12
  191.           - (3 * ((time.year + 4900 + (time.month - 14) / 12) / 100)) / 4
  192.           + time.day - 32075;

  193.   double jd = jdn + ((time.hour - 12) * 3600.0 + time.minute * 60.0 + time.second) / 86400.0;

  194.   return jd;
  195. }

  196. /****************************************************************
  197. * Function:    GregorianCalendarDateToModifiedJulianDate
  198. * Description: Gregorian calendar date to modified julian date.
  199. * Input:       time: Gregorian calendar date.
  200. * Output:
  201. * Return:      Modified julian date.
  202. *****************************************************************/
  203. double GregorianCalendarDateToModifiedJulianDate(DateTime time)
  204. {
  205.   return GregorianCalendarDateToJulianDate(time) - 2400000.5;
  206. }

  207. /****************************************************************
  208. * Function:    GpsWeekSecondAddYear
  209. * Description: Gps week and second add year.
  210. * Input:       time: Gps week and second.
  211. *              year: The number of year to add.
  212. * Output:
  213. * Return:      Gps week and second.
  214. *****************************************************************/
  215. GpsWeekSecond GpsWeekSecondAddYear(GpsWeekSecond time, int year)
  216. {
  217.   DateTime date = GpsWeekSecondToGregorianCalendarDate(time);

  218.   date = GregorianCalendarDateAddYear(date, year);

  219.   return GregorianCalendarDateToGpsWeekSecond(date);
  220. }

  221. /****************************************************************
  222. * Function:    GpsWeekSecondAddMonth
  223. * Description: Gps week and second add month.
  224. * Input:       time:  Gps week and second.
  225. *              month: The number of month to add.
  226. * Output:
  227. * Return:      Gps week and second.
  228. *****************************************************************/
  229. GpsWeekSecond GpsWeekSecondAddMonth(GpsWeekSecond time, int month)
  230. {
  231.   DateTime date = GpsWeekSecondToGregorianCalendarDate(time);

  232.   date = GregorianCalendarDateAddMonth(date, month);

  233.   return GregorianCalendarDateToGpsWeekSecond(date);
  234. }

  235. /****************************************************************
  236. * Function:    GpsWeekSecondAddWeek
  237. * Description: Gps week and second add week.
  238. * Input:       time: Gps week and second.
  239. *              week: The number of week to add.
  240. * Output:
  241. * Return:      Gps week and second.
  242. *****************************************************************/
  243. GpsWeekSecond GpsWeekSecondAddWeek(GpsWeekSecond time, int week)
  244. {
  245.   time.week += week;

  246.   return time;
  247. }

  248. /****************************************************************
  249. * Function:    GpsWeekSecondAddDay
  250. * Description: Gps week and second add day.
  251. * Input:       time: Gps week and second.
  252. *              day:  The number of day to add.
  253. * Output:
  254. * Return:      Gps week and second.
  255. *****************************************************************/
  256. GpsWeekSecond GpsWeekSecondAddDay(GpsWeekSecond time, int day)
  257. {
  258.   time.week   += day / 7;
  259.   time.second += day % 7 * 86400;

  260.   if(time.second > 604799)
  261.   {
  262.     time.week   += 1;
  263.     time.second -= 604800;
  264.   }

  265.   return time;
  266. }

  267. /****************************************************************
  268. * Function:    GpsWeekSecondAddHour
  269. * Description: Gps week and second add hour.
  270. * Input:       time: Gps week and second.
  271. *              hour: The number of hour to add.
  272. * Output:
  273. * Return:      Gps week and second.
  274. *****************************************************************/
  275. GpsWeekSecond GpsWeekSecondAddHour(GpsWeekSecond time, int hour)
  276. {
  277.   time.week   += hour / 168;
  278.   time.second += hour % 168 * 3600;

  279.   if(time.second > 604799)
  280.   {
  281.     time.week   += 1;
  282.     time.second -= 604800;
  283.   }
  284.   
  285.   return time;
  286. }

  287. /****************************************************************
  288. * Function:    GpsWeekSecondAddMinute
  289. * Description: Gps week and second add minute.
  290. * Input:       time:   Gps week and second.
  291. *              minute: The number of minute to add.
  292. * Output:
  293. * Return:      Gps week and second.
  294. *****************************************************************/
  295. GpsWeekSecond GpsWeekSecondAddMinute(GpsWeekSecond time, int minute)
  296. {
  297.   time.week   += minute / 10080;
  298.   time.second += minute % 10080 * 60;
  299.   
  300.   if(time.second > 604799)
  301.   {
  302.     time.week   += 1;
  303.     time.second -= 604800;
  304.   }
  305.   
  306.   return time;
  307. }

  308. /****************************************************************
  309. * Function:    GpsWeekSecondAddSecond
  310. * Description: Gps week and second add second.
  311. * Input:       time:   Gps week and second.
  312. *              second: The number of second to add.
  313. * Output:
  314. * Return:      Gps week and second.
  315. *****************************************************************/
  316. GpsWeekSecond GpsWeekSecondAddSecond(GpsWeekSecond time, int second)
  317. {
  318.   time.week   += second / 604800;
  319.   time.second += second % 604800;

  320.   if(time.second > 604799)
  321.   {
  322.     time.week   += 1;
  323.     time.second -= 604800;
  324.   }
  325.   
  326.   return time;
  327. }

  328. /****************************************************************
  329. * Function:    GpsWeekSecondToGregorianCalendarDate
  330. * Description: Gps week and second to gregorian calendar date.
  331. * Input:       time: Gps week and second.
  332. * Output:
  333. * Return:      Gregorian calendar date.
  334. *****************************************************************/
  335. DateTime GpsWeekSecondToGregorianCalendarDate(GpsWeekSecond time)
  336. {
  337.   double jd = GpsWeekSecondToJulianDate(time);

  338.   return JulianDateToGregorianCalendarDate(jd);
  339. }

  340. /****************************************************************
  341. * Function:    GpsWeekSecondToJulianDate
  342. * Description: Gps week and second to julian date.
  343. * Input:       time: Gps week and second.
  344. * Output:
  345. * Return:      Julian date.
  346. *****************************************************************/
  347. double GpsWeekSecondToJulianDate(GpsWeekSecond time)
  348. {
  349.   double jd = 2444244.5 + time.week * 7.0 + time.second / 86400.0;

  350.   return jd;
  351. }

  352. /****************************************************************
  353. * Function:    GpsWeekSecondToModifiedJulianDate
  354. * Description: Gps week and second to modified julian date.
  355. * Input:       time: Gps week and second.
  356. * Output:
  357. * Return:      Modified julian date.
  358. *****************************************************************/
  359. double GpsWeekSecondToModifiedJulianDate(GpsWeekSecond time)
  360. {
  361.   return GpsWeekSecondToJulianDate(time) - 2400000.5;
  362. }

  363. /****************************************************************
  364. * Function:    JulianDateAddYear
  365. * Description: Julian date add year.
  366. * Input:       jd:   Julian date.
  367. *              year: The number of year to add.
  368. * Output:
  369. * Return:      Julian date.
  370. *****************************************************************/
  371. double JulianDateAddYear(double jd, int year)
  372. {
  373.   DateTime date = JulianDateToGregorianCalendarDate(jd);

  374.   date = GregorianCalendarDateAddYear(date, year);

  375.   return GregorianCalendarDateToJulianDate(date);
  376. }

  377. /****************************************************************
  378. * Function:    JulianDateAddMonth
  379. * Description: Julian date add month.
  380. * Input:       jd:    Julian date.
  381. *              month: The number of month to add.
  382. * Output:
  383. * Return:      Julian date.
  384. *****************************************************************/
  385. double JulianDateAddMonth(double jd, int month)
  386. {
  387.   DateTime date = JulianDateToGregorianCalendarDate(jd);

  388.   date = GregorianCalendarDateAddMonth(date, month);

  389.   return GregorianCalendarDateToJulianDate(date);
  390. }

  391. /****************************************************************
  392. * Function:    JulianDateAddWeek
  393. * Description: Julian date add week.
  394. * Input:       jd:   Julian date.
  395. *              week: The number of week to add.
  396. * Output:
  397. * Return:      Julian date.
  398. *****************************************************************/
  399. double JulianDateAddWeek(double jd, int week)
  400. {
  401.   jd += week * 7.0;

  402.   return jd;
  403. }

  404. /****************************************************************
  405. * Function:    JulianDateAddDay
  406. * Description: Julian date add day.
  407. * Input:       jd:  Julian date.
  408. *              day: The number of day to add.
  409. * Output:
  410. * Return:      Julian date.
  411. *****************************************************************/
  412. double JulianDateAddDay(double jd, int day)
  413. {
  414.   jd += day;

  415.   return jd;
  416. }

  417. /****************************************************************
  418. * Function:    JulianDateAddHour
  419. * Description: Julian date add hour.
  420. * Input:       jd:   Julian date.
  421. *              hour: The number of hour to add.
  422. * Output:
  423. * Return:      Julian date.
  424. *****************************************************************/
  425. double JulianDateAddHour(double jd, int hour)
  426. {
  427.   jd += hour / 24.0;

  428.   return jd;
  429. }

  430. /****************************************************************
  431. * Function:    JulianDateAddMinute
  432. * Description: Julian date add minute.
  433. * Input:       jd:     Julian date.
  434. *              minute: The number of minute to add.
  435. * Output:
  436. * Return:      Julian date.
  437. *****************************************************************/
  438. double JulianDateAddMinute(double jd, int minute)
  439. {
  440.   jd += minute / 1440.0;

  441.   return jd;
  442. }

  443. /****************************************************************
  444. * Function:    JulianDateAddSecond
  445. * Description: Julian date add second.
  446. * Input:       jd:     Julian date.
  447. *              second: The number of second to add.
  448. * Output:
  449. * Return:      Julian date.
  450. *****************************************************************/
  451. double JulianDateAddSecond(double jd, int second)
  452. {
  453.   jd += second / 86400.0;

  454.   return jd;
  455. }

  456. /****************************************************************
  457. * Function:    JulianDateToGregorianCalendarDate
  458. * Description: Julian date to gregorian calendar date.
  459. * Input:       jd: Julian date.
  460. * Output:
  461. * Return:      Gregorian calendar date.
  462. *****************************************************************/
  463. DateTime JulianDateToGregorianCalendarDate(double jd)
  464. {
  465.   int y = 4716;
  466.   int j = 1401;
  467.   int m = 2;
  468.   int n = 12;
  469.   int r = 4;
  470.   int p = 1461;
  471.   int v = 3;
  472.   int u = 5;
  473.   int s = 153;
  474.   int w = 2;
  475.   int b = 274277;
  476.   int c = -38;

  477.   int jdn = (int)(jd + 0.5);
  478.   int f   = jdn + j + (((4 * jdn + b) / 146097) * 3) / 4 + c;
  479.   int e   = r * f + v;
  480.   int g   = (e % p) / r;
  481.   int h   = u * g + w;

  482.   DateTime time = {0};

  483.   time.day    = (h % s) / u + 1;
  484.   time.month  = (h / s + m) % n + 1;
  485.   time.year   = e / p - y + (n + m - time.month) / n;
  486.   time.hour   = (int)((jd + 0.5 - jdn) * 86400.5) / 3600;
  487.   time.minute = (int)((jd + 0.5 - jdn) * 86400.5 - time.hour * 3600) / 60;
  488.   time.second = (int)((jd + 0.5 - jdn) * 86400.5 - time.hour * 3600 - time.minute * 60);

  489.   return time;
  490. }

  491. /****************************************************************
  492. * Function:    JulianDateToGpsWeekSecond
  493. * Description: Julian date to gps week and second.
  494. * Input:       jd: Julian date.
  495. * Output:
  496. * Return:      Gps week and second.
  497. *****************************************************************/
  498. GpsWeekSecond JulianDateToGpsWeekSecond(double jd)
  499. {
  500.   GpsWeekSecond time = {0};
  501.   DateTime      date = JulianDateToGregorianCalendarDate(jd);

  502.   time.week   = (int)(jd - 2444244.5) / 7;
  503.   time.second = ((int)(jd - 2444244.5) - time.week * 7) * 86400 + date.hour * 3600 + date.minute * 60 + date.second;

  504.   return time;
  505. }

  506. /****************************************************************
  507. * Function:    JulianDateToModifiedJulianDate
  508. * Description: Julian date to modified julian date.
  509. * Input:       jd: Julian date.
  510. * Output:
  511. * Return:      Modified julian date.
  512. *****************************************************************/
  513. double JulianDateToModifiedJulianDate(double jd)
  514. {
  515.   double mjd = jd - 2400000.5;

  516.   return mjd;
  517. }

  518. /****************************************************************
  519. * Function:    ModifiedJulianDateAddYear
  520. * Description: Modified julian date add year.
  521. * Input:       mjd:  Modified julian date.
  522. *              year: The number of year to add.
  523. * Output:
  524. * Return:      Modified julian date.
  525. *****************************************************************/
  526. double ModifiedJulianDateAddYear(double mjd, int year)
  527. {
  528.   DateTime date = ModifiedJulianDateToGregorianCalendarDate(mjd);

  529.   date = GregorianCalendarDateAddYear(date, year);

  530.   return GregorianCalendarDateToModifiedJulianDate(date);
  531. }

  532. /****************************************************************
  533. * Function:    ModifiedJulianDateAddMonth
  534. * Description: Modified julian date add month.
  535. * Input:       mjd:   Modified julian date.
  536. *              month: The number of month to add.
  537. * Output:
  538. * Return:      Modified julian date.
  539. *****************************************************************/
  540. double ModifiedJulianDateAddMonth(double mjd, int month)
  541. {
  542.   DateTime date = ModifiedJulianDateToGregorianCalendarDate(mjd);

  543.   date = GregorianCalendarDateAddMonth(date, month);

  544.   return GregorianCalendarDateToModifiedJulianDate(date);
  545. }

  546. /****************************************************************
  547. * Function:    ModifiedJulianDateAddWeek
  548. * Description: Modified julian date add week.
  549. * Input:       mjd:  Modified julian date.
  550. *              week: The number of week to add.
  551. * Output:
  552. * Return:      Modified julian date.
  553. *****************************************************************/
  554. double ModifiedJulianDateAddWeek(double mjd, int week)
  555. {
  556.   mjd += week * 7.0;

  557.   return mjd;
  558. }

  559. /****************************************************************
  560. * Function:    ModifiedJulianDateAddDay
  561. * Description: Modified julian date add day.
  562. * Input:       mjd: Modified julian date.
  563. *              day: The number of day to add.
  564. * Output:
  565. * Return:      Modified julian date.
  566. *****************************************************************/
  567. double ModifiedJulianDateAddDay(double mjd, int day)
  568. {
  569.   mjd += day;

  570.   return mjd;
  571. }

  572. /****************************************************************
  573. * Function:    ModifiedJulianDateAddHour
  574. * Description: Modified julian date add hour.
  575. * Input:       mjd:  Modified julian date.
  576. *              hour: The number of hour to add.
  577. * Output:
  578. * Return:      Modified julian date.
  579. *****************************************************************/
  580. double ModifiedJulianDateAddHour(double mjd, int hour)
  581. {
  582.   mjd += hour / 24.0;

  583.   return mjd;
  584. }

  585. /****************************************************************
  586. * Function:    ModifiedJulianDateAddMinute
  587. * Description: Modified julian date add minute.
  588. * Input:       mjd:    Modified julian date.
  589. *              minute: The number of minute to add.
  590. * Output:
  591. * Return:      Modified julian date.
  592. *****************************************************************/
  593. double ModifiedJulianDateAddMinute(double mjd, int minute)
  594. {
  595.   mjd += minute / 1440.0;

  596.   return mjd;
  597. }

  598. /****************************************************************
  599. * Function:    ModifiedJulianDateAddSecond
  600. * Description: Modified julian date add second.
  601. * Input:       mjd:    Modified julian date.
  602. *              second: The number of second to add.
  603. * Output:
  604. * Return:      Modified julian date.
  605. *****************************************************************/
  606. double ModifiedJulianDateAddSecond(double mjd, int second)
  607. {
  608.   mjd += second / 86400.0;

  609.   return mjd;
  610. }

  611. /****************************************************************
  612. * Function:    ModifiedJulianDateToGregorianCalendarDate
  613. * Description: Modified julian date to gregorian calendar date.
  614. * Input:       mjd: Modified julian date.
  615. * Output:
  616. * Return:      Gregorian calendar date.
  617. *****************************************************************/
  618. DateTime ModifiedJulianDateToGregorianCalendarDate(double mjd)
  619. {
  620.   return JulianDateToGregorianCalendarDate(mjd + 2400000.5);
  621. }

  622. /****************************************************************
  623. * Function:    ModifiedJulianDateToGpsWeekSecond
  624. * Description: Modified julian date to gps week and second.
  625. * Input:       mjd: Modified julian date.
  626. * Output:
  627. * Return:      Gps week and second.
  628. *****************************************************************/
  629. GpsWeekSecond ModifiedJulianDateToGpsWeekSecond(double mjd)
  630. {
  631.   return JulianDateToGpsWeekSecond(mjd + 2400000.5);
  632. }

  633. /****************************************************************
  634. * Function:    ModifiedJulianDateToJulianDate
  635. * Description: Modified julian date to julian date.
  636. * Input:       mjd: Modified julian date.
  637. * Output:
  638. * Return:      Julian date.
  639. *****************************************************************/
  640. double ModifiedJulianDateToJulianDate(double mjd)
  641. {
  642.   double jd = mjd + 2400000.5;

  643.   return jd;
  644. }
复制代码

      main.c文件
  1. /****************************************************************
  2. * Copyright (C) 2017, XinLi, all right reserved.
  3. * File name:    main.c
  4. * Date:         2017.10.17
  5. * Description:  GPS, UTC and local time displays.
  6. *****************************************************************/

  7. /****************************************************************
  8. *                        Header include
  9. *****************************************************************/
  10. #include "DateTime.h"
  11. #include <stdint.h>
  12. #include <stdio.h>
  13. #include <time.h>
  14. #include <windows.h>

  15. /****************************************************************
  16. *                       Global variables
  17. *****************************************************************/


  18. /****************************************************************
  19. *                     Function declaration
  20. *****************************************************************/
  21. static void gotoxy(int x, int y);

  22. /****************************************************************
  23. *                     Function definition
  24. *****************************************************************/

  25. /****************************************************************
  26. * Function:    main
  27. * Description: Program entry.
  28. * Input:
  29. * Output:
  30. * Return:
  31. *****************************************************************/
  32. int main(void)
  33. {
  34.   for(;;)
  35.   {
  36.     time_t        times     = 0;
  37.     double        mjd       = 0.0;
  38.     DateTime      utctime   = {.year = 1970, .month = 1, .day = 1, .hour = 0, .minute = 0, .second = 0};
  39.     DateTime      localtime = {.year = 1970, .month = 1, .day = 1, .hour = 8, .minute = 0, .second = 0};
  40.     DateTime      gpstime   = {.year = 1970, .month = 1, .day = 1, .hour = 0, .minute = 0, .second = 0};
  41.     GpsWeekSecond gpstimews = {0};

  42.     time(×);

  43.     if(times > INT32_MAX)
  44.     {
  45.       utctime = GregorianCalendarDateAddSecond(utctime, INT32_MAX);
  46.       utctime = GregorianCalendarDateAddSecond(utctime, (int)(times - INT32_MAX));
  47.     }
  48.     else
  49.     {
  50.       utctime = GregorianCalendarDateAddSecond(utctime, (int)times);
  51.     }

  52.     mjd       = GregorianCalendarDateToModifiedJulianDate(utctime);
  53.     localtime = GregorianCalendarDateAddHour(utctime, 8);
  54.     gpstime   = GregorianCalendarDateAddSecond(utctime, 18);
  55.     gpstimews = GregorianCalendarDateToGpsWeekSecond(gpstime);

  56.     gotoxy(0, 0);

  57.     printf("Local | %d-%.2d-%.2d %.2d:%.2d:%.2d | timezone UTC+8\n",
  58.            localtime.year, localtime.month, localtime.day,
  59.            localtime.hour, localtime.minute, localtime.second);

  60.     printf("UTC   | %d-%.2d-%.2d %.2d:%.2d:%.2d | MJD %.5f\n",
  61.            utctime.year, utctime.month, utctime.day,
  62.            utctime.hour, utctime.minute, utctime.second,
  63.            mjd);

  64.     printf("GPS   | %d-%.2d-%.2d %.2d:%.2d:%.2d | week %d %d s\n",
  65.            gpstime.year, gpstime.month, gpstime.day,
  66.            gpstime.hour, gpstime.minute, gpstime.second,
  67.            gpstimews.week, gpstimews.second);

  68.     Sleep(100);
  69.   }
  70. }

  71. /****************************************************************
  72. * Function:    gotoxy
  73. * Description: Move the cursor to the specified position on the text screen.
  74. * Input:       x: X axis coordinates.
  75. *              y: Y axis coordinates.
  76. * Output:
  77. * Return:
  78. *****************************************************************/
  79. static void gotoxy(int x, int y)
  80. {
  81.   COORD  pos  = {x, y};
  82.   HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  83.   SetConsoleCursorPosition(hOut, pos);
  84. }
复制代码

3,运行效果



收藏 1 评论7 发布时间:2018-3-7 20:34

举报

7个回答
XinLiYF 回答时间:2018-3-7 20:35:11
可以直接移植到嵌入式端,非常方便。
xuanyuan1993 回答时间:2018-3-7 21:57:55
牛逼牛逼,回复拿分
maxtch 回答时间:2018-3-7 22:19:13
有必要如此折腾吗?不论是 C# 还是传统 C,标准库都有完整的时间与时区支持,不必自己劳心费神再写一遍了。
XinLiYF 回答时间:2018-3-7 22:50:51
maxtch 发表于 2018-3-7 22:19
有必要如此折腾吗?不论是 C# 还是传统 C,标准库都有完整的时间与时区支持,不必自己劳心费神再写一遍了。 ...

朱利安日期,GPS周和周内秒,一般的库都没有吧,除非是专门做导航的库才会有这些。写这个是为了方便的在嵌入式端使用。
jjbboox 回答时间:2018-3-8 10:23:39
本帖最后由 jjbboox 于 2018-3-8 10:25 编辑

直接 #include <time.h>不就OK了?
C51才需要自己实现,stm32的环境,何须如此麻烦。
以上。

maxtch 回答时间:2018-3-8 11:38:10
XinLiYF 发表于 2018-3-7 22:50
朱利安日期,GPS周和周内秒,一般的库都没有吧,除非是专门做导航的库才会有这些。写这个是为了方便的在 ...

儒略历和格里历的转换一般支持时区转换的库都有。(那个不叫朱利安日期……)至于 GPS 周和周内秒,如果用现有库的话,也只要可以和 UNIX 时间戳互转就可以了,没那么复杂。
XinLiYF 回答时间:2018-3-8 13:31:08
maxtch 发表于 2018-3-8 11:38
儒略历和格里历的转换一般支持时区转换的库都有。(那个不叫朱利安日期……)至于 GPS 周和周内秒,如果 ...

朱利安日期有这样叫的,是 Julian Date 音译过来的。

所属标签

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版