caltime.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*!
  2. * Copyright (C) 2001-2005 by egnite Software GmbH
  3. * Copyright (C) 2009-2010 by egnite GmbH
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holders nor the names of
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * For additional information see http://www.ethernut.de/
  34. */
  35. /*!
  36. * $Id: caltime.c 5156 2013-05-15 17:28:20Z u_bonnes $
  37. */
  38. #include <dev/board.h>
  39. #include <dev/debug.h>
  40. #include <sys/version.h>
  41. #include <sys/timer.h>
  42. #include <stdlib.h>
  43. #include <stdio.h>
  44. #include <io.h>
  45. #include <string.h>
  46. #include <time.h>
  47. #ifdef USE_BUILD_TIME
  48. #include <pro/rfctime.h>
  49. #endif
  50. #ifdef USE_LINE_EDITOR
  51. #include <gorp/edline.h>
  52. #define LINE_MAXLENGTH 80
  53. static char edbuff[LINE_MAXLENGTH];
  54. static EDLINE *edline;
  55. #endif
  56. /*!
  57. * \example caltime/caltime.c
  58. *
  59. * Demonstrates Nut/OS date and time functions, which had been contributed
  60. * by Oliver Schulz.
  61. *
  62. * Check the Makefile for additional options.
  63. */
  64. static char *version = "2.1";
  65. /* Used for ASCII Art Animation. */
  66. static char *rotor = "|/-\\";
  67. static char *weekday_name[7] = {
  68. "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
  69. };
  70. /*
  71. * Print content of tm structure.
  72. */
  73. static void PrintDateTime(const struct _tm *stm)
  74. {
  75. printf("%s, %04d/%02d/%02d, %02d:%02d:%02d%s"
  76. , weekday_name[stm->tm_wday]
  77. , stm->tm_year + 1900, stm->tm_mon + 1, stm->tm_mday
  78. , stm->tm_hour, stm->tm_min, stm->tm_sec
  79. , stm->tm_isdst ? " DST" : ""
  80. );
  81. }
  82. /*
  83. * Query calendar date from user.
  84. *
  85. * Returns 0 on success, -1 otherwise.
  86. */
  87. static int EnterDate(struct _tm *stm)
  88. {
  89. int year = stm->tm_year + 1900;
  90. int month = stm->tm_mon + 1;
  91. int day = stm->tm_mday;
  92. printf("Enter date, use format YYYY/MM/DD");
  93. #ifdef USE_LINE_EDITOR
  94. printf(" : ");
  95. sprintf(edbuff, "%04d/%02d/%02d", year, month, day);
  96. EdLineRead(edline, edbuff, LINE_MAXLENGTH);
  97. sscanf(edbuff, "%d/%d/%d", &year, &month, &day);
  98. #else
  99. printf(" (%04d/%02d/%02d): ", year, month, day);
  100. scanf("%d/%d/%d", &year, &month, &day);
  101. putchar('\n');
  102. #endif
  103. if (year < 1970 || year > 2038) {
  104. printf("Bad year: %d is not within range 1970..2038\n", year);
  105. return -1;
  106. }
  107. if (month < 1 || month > 12) {
  108. printf("Bad month: %d is not within range 1..12\n", month);
  109. return -1;
  110. }
  111. if (day < 1 || day > 31) {
  112. printf("Bad day: %d is not within range 1..31\n", day);
  113. return -1;
  114. }
  115. stm->tm_year = year - 1900;
  116. stm->tm_mon = month - 1;
  117. stm->tm_mday = day;
  118. return 0;
  119. }
  120. /*
  121. * Query time of day from user.
  122. *
  123. * Returns 0 on success, -1 otherwise.
  124. */
  125. static int EnterTime(struct _tm *stm)
  126. {
  127. int hour = stm->tm_hour;
  128. int minute = stm->tm_min;
  129. int second = stm->tm_sec;
  130. printf("Enter time, use 24h format HH:MM:SS");
  131. #ifdef USE_LINE_EDITOR
  132. printf(": ");
  133. sprintf(edbuff, "%02d:%02d:%02d", hour, minute, second);
  134. EdLineRead(edline, edbuff, LINE_MAXLENGTH);
  135. sscanf(edbuff, "%d:%d:%d", &hour, &minute, &second);
  136. #else
  137. printf(" (%02d:%02d:%02d): ", hour, minute, second);
  138. scanf("%d:%d:%d", &hour, &minute, &second);
  139. putchar('\n');
  140. #endif
  141. if (hour < 0 || hour > 23) {
  142. printf("Bad hour: %d is not within range 0..23\n", hour);
  143. return -1;
  144. }
  145. if (minute < 0 || minute > 59) {
  146. printf("Bad minute: %d is not within range 0..59\n", minute);
  147. return -1;
  148. }
  149. if (second < 0 || second > 59) {
  150. printf("Bad second: %d is not within range 0..59\n", second);
  151. return -1;
  152. }
  153. stm->tm_hour = hour;
  154. stm->tm_min = minute;
  155. stm->tm_sec = second;
  156. return 0;
  157. }
  158. /*
  159. * Query time difference from user, specified in hours and minutes.
  160. *
  161. * 'init' specifies the default number of seconds.
  162. *
  163. * Returns the number of seconds.
  164. */
  165. static long EnterTimeDiff(long init)
  166. {
  167. long hours;
  168. long minutes;
  169. long seconds;
  170. seconds = init;
  171. minutes = seconds / 60L;
  172. hours = minutes / 60L;
  173. minutes = abs(minutes % 60L);
  174. printf("Enter time difference in format HH:MM");
  175. #ifdef USE_LINE_EDITOR
  176. printf(": ");
  177. sprintf(edbuff, "%+03ld:%02ld", hours, minutes);
  178. EdLineRead(edline, edbuff, LINE_MAXLENGTH);
  179. sscanf(edbuff, "%ld:%ld", &hours, &minutes);
  180. #else
  181. printf(" (%+03ld:%02ld): ", hours, minutes);
  182. scanf("%ld:%ld", &hours, &minutes);
  183. putchar('\n');
  184. #endif
  185. if (hours < -12 || hours > 12) {
  186. printf("\nBad hours: %ld is not within range -12..+12\n", hours);
  187. return init;
  188. }
  189. if (minutes < 0 || minutes > 59) {
  190. printf("\nBad minutes: %ld is not within range 0..59\n", minutes);
  191. return init;
  192. }
  193. return (hours * 60L + minutes) * 60L;
  194. }
  195. /*
  196. * Display the elapsed seconds of the epoch.
  197. *
  198. * The value is constantly updated until the user presses a key.
  199. */
  200. static void DisplaySeconds(void)
  201. {
  202. uint_fast8_t i = 0;
  203. while (!kbhit()) {
  204. printf(" [%c] Seconds since epoch: %ld\r"
  205. , rotor[++i & 3]
  206. , (long) time(NULL));
  207. NutSleep(200);
  208. }
  209. putchar('\n');
  210. }
  211. /*
  212. * Display the coordinated universal time.
  213. *
  214. * The value is constantly updated until the user presses a key.
  215. */
  216. static void DisplayZuluTime(void)
  217. {
  218. time_t secs;
  219. struct _tm *gmt;
  220. uint_fast8_t i = 0;
  221. while (!kbhit()) {
  222. secs = time(NULL);
  223. gmt = gmtime(&secs);
  224. printf(" [%c] Universal time: ", rotor[++i & 3]);
  225. PrintDateTime(gmt);
  226. printf(" \r");
  227. NutSleep(500);
  228. }
  229. putchar('\n');
  230. }
  231. /*
  232. * Display the local time.
  233. *
  234. * The value is constantly updated until the user presses a key.
  235. */
  236. static void DisplayLocalTime(void)
  237. {
  238. time_t tt;
  239. struct _tm *ltm;
  240. uint_fast8_t i = 0;
  241. while (!kbhit()) {
  242. /* Get local time and print it. */
  243. tt = time(NULL);
  244. ltm = localtime(&tt);
  245. printf(" [%c] Local time: ", rotor[++i & 3]);
  246. PrintDateTime(ltm);
  247. /* Calculate the offset from UTC in minutes. */
  248. tt = _timezone;
  249. if (ltm->tm_isdst > 0) {
  250. tt += _dstbias;
  251. }
  252. tt /= -60L;
  253. /* Print UTC offset in format HH:MM. */
  254. printf(" UTC%+03ld:%02ld \r", tt / 60L, abs(tt) % 60L);
  255. NutSleep(200);
  256. }
  257. putchar('\n');
  258. }
  259. /*
  260. * Display system up time.
  261. *
  262. * The value is constantly updated until the user presses a key.
  263. */
  264. static void DisplayUpTime(void)
  265. {
  266. uint32_t hours;
  267. uint32_t minutes;
  268. uint32_t seconds;
  269. uint_fast8_t i = 0;
  270. while (!kbhit()) {
  271. seconds = NutGetSeconds();
  272. minutes = seconds / 60UL;
  273. hours = minutes / 60UL;
  274. minutes %= 60UL;
  275. seconds %= 60UL;
  276. printf(" [%c] System is running %lu hours"
  277. ", %lu minutes and %lu seconds \r"
  278. , rotor[++i & 3], hours, minutes, seconds);
  279. NutSleep(500);
  280. }
  281. putchar('\n');
  282. }
  283. /*
  284. * Display the week day of a queried calendar date.
  285. *
  286. * mktime() updates the structure members tm_yday and tm_wday.
  287. * This can be used to determine the week day name of any given
  288. * date.
  289. */
  290. static void CalcWeekDay(void)
  291. {
  292. struct _tm date;
  293. time_t secs;
  294. /* Use current local time as default. */
  295. time(&secs);
  296. memcpy(&date, localtime(&secs), sizeof(date));
  297. /* Query date and print week day name if we got a valid entry. */
  298. if (EnterDate(&date) == 0) {
  299. mktime(&date);
  300. puts(weekday_name[date.tm_wday]);
  301. }
  302. }
  303. /*
  304. * Query user for a new time zone offset.
  305. */
  306. static void SetTimeZone(void)
  307. {
  308. /* Nut/OS uses a global variable to store the current TZ offset. */
  309. _timezone = EnterTimeDiff(_timezone);
  310. }
  311. /*
  312. * Query user for a new system time.
  313. */
  314. static void SetLocalTime(void)
  315. {
  316. struct _tm ltm;
  317. time_t now;
  318. /* Use current local time as default. */
  319. time(&now);
  320. memcpy(&ltm, localtime(&now), sizeof(ltm));
  321. /* Query date and time. */
  322. if (EnterDate(&ltm) == 0 && EnterTime(&ltm) == 0) {
  323. /* Let mktime determine whether DST is in effect. */
  324. ltm.tm_isdst = -1;
  325. /* mktime expects local time and returns seconds since the epoch. */
  326. now = mktime(&ltm);
  327. /* stime expects seconds since the epoch. */
  328. stime(&now);
  329. }
  330. }
  331. /*
  332. * Application entry.
  333. */
  334. int main(void)
  335. {
  336. uint32_t baud = 115200;
  337. int cmd;
  338. /* Use UART device for stdin and stdout. */
  339. NutRegisterDevice(&DEV_CONSOLE, 0, 0);
  340. freopen(DEV_CONSOLE.dev_name, "w", stdout);
  341. freopen(DEV_CONSOLE.dev_name, "r", stdin);
  342. _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
  343. printf("\n\nCalendar Time %s running on Nut/OS %s\n"
  344. , version, NutVersionString());
  345. #ifdef USE_LINE_EDITOR
  346. /* Open line editor, if configured. */
  347. printf("Opening line editor...");
  348. edline = EdLineOpen(EDIT_MODE_ECHO);
  349. if (edline) {
  350. puts("OK");
  351. } else {
  352. puts("failed");
  353. }
  354. #else
  355. puts("Note: Enable local echo!");
  356. #endif
  357. #if USE_TIME_ZONE
  358. _timezone = USE_TIME_ZONE;
  359. #endif
  360. #ifdef RTC_CHIP
  361. /* Register and query hardware RTC, if available. */
  362. printf("Registering RTC hardware...");
  363. if (NutRegisterRtc(&RTC_CHIP)) {
  364. puts("failed");
  365. } else {
  366. uint32_t rtc_stat;
  367. NutRtcGetStatus(&rtc_stat);
  368. if (rtc_stat & RTC_STATUS_PF) {
  369. #if defined(USE_BUILD_TIME)
  370. puts("power failure, Setting Time from Build date");
  371. /* Initially use the compile date and time. */
  372. time_t now = RfcTimeParse("Unk, " __DATE__ " " __TIME__);
  373. stime(&now);
  374. puts("Built " __DATE__ " " __TIME__);
  375. #else
  376. puts("power failure");
  377. #endif
  378. } else {
  379. puts("OK");
  380. }
  381. }
  382. #elif USE_BUILD_TIME
  383. {
  384. /* Initially use the compile date and time. */
  385. time_t now = RfcTimeParse("Unk, " __DATE__ " " __TIME__);
  386. stime(&now);
  387. puts("Built " __DATE__ " " __TIME__);
  388. }
  389. #endif
  390. for (;;) {
  391. /* Print command menu. */
  392. puts("\n 0 - Display seconds counter");
  393. puts(" 1 - Display universal time");
  394. puts(" 2 - Display local time");
  395. puts(" 3 - Display system uptime");
  396. puts(" C - Calculate weekday");
  397. puts(" S - Set local time");
  398. puts(" Y - Toggle DST calculation");
  399. puts(" Z - Set timezone");
  400. printf("What is thy bidding, my master? ");
  401. /* Flush input buffer. */
  402. while (kbhit()) {
  403. cmd = getchar();
  404. }
  405. /* Get the next command. */
  406. cmd = getchar();
  407. putchar('\n');
  408. /* Process the command. */
  409. switch (cmd) {
  410. case '0':
  411. DisplaySeconds();
  412. break;
  413. case '1':
  414. DisplayZuluTime();
  415. break;
  416. case '2':
  417. DisplayLocalTime();
  418. break;
  419. case '3':
  420. DisplayUpTime();
  421. break;
  422. case 'C':
  423. case 'c':
  424. CalcWeekDay();
  425. break;
  426. case 'S':
  427. case 's':
  428. SetLocalTime();
  429. break;
  430. case 'Y':
  431. case 'y':
  432. /* Nut/OS uses a global variable to enable/disable DST.
  433. Toggle the current status and display the result. */
  434. _daylight = _daylight == 0;
  435. printf("DST calculation %sabled\n", _daylight ? "en" : "dis");
  436. break;
  437. case 'Z':
  438. case 'z':
  439. SetTimeZone();
  440. break;
  441. }
  442. }
  443. return 0;
  444. }