|
| 1 | +/** |
| 2 | + * @ingroup tests |
| 3 | + * @{ |
| 4 | + * @file |
| 5 | + * @brief Test the `struct tm` helpers in "tm.h" of the module "timex". |
| 6 | + * @author René Kijewski <[email protected]> |
| 7 | + * @} |
| 8 | + */ |
| 9 | + |
| 10 | +#include <stdio.h> |
| 11 | + |
| 12 | +#include "shell.h" |
| 13 | +#include "posix_io.h" |
| 14 | +#include "board_uart0.h" |
| 15 | +#include "tm.h" |
| 16 | + |
| 17 | +#define SHELL_BUFSIZE (UART0_BUFSIZE) |
| 18 | + |
| 19 | +static const char MON_NAMES[12][3] = { |
| 20 | + "JAN", "FEB", "MAR", "APR", |
| 21 | + "MAY", "JUN", "JUL", "AUG", |
| 22 | + "SEP", "OCT", "NOV", "DEC", |
| 23 | +}; |
| 24 | +static const char DAY_NAMES[7][3] = { |
| 25 | + "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" |
| 26 | +}; |
| 27 | +static const char BOOL_NAMES[2][3] = { "NO", "YES" }; |
| 28 | + |
| 29 | +static void cmd_days_in(int argc, char **argv) |
| 30 | +{ |
| 31 | + int mon; |
| 32 | + if ((argc != 2) || (sscanf(argv[1], "%d", &mon) != 1) || (mon < 1) || (mon > 12)) { |
| 33 | + printf("Usage: %s <Month[1..12]>\n", argv[0]); |
| 34 | + } |
| 35 | + else { |
| 36 | + printf("There are %d days in %.3s in common years.\n", |
| 37 | + TM_MON_DAYS[mon - 1], MON_NAMES[mon - 1]); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +static void cmd_leap_year(int argc, char **argv) |
| 42 | +{ |
| 43 | + int year; |
| 44 | + if ((argc != 2) || (sscanf(argv[1], "%d", &year) != 1)) { |
| 45 | + printf("Usage: %s <Year>\n", argv[0]); |
| 46 | + } |
| 47 | + else { |
| 48 | + printf("Was %d a leap year? %.3s.\n", |
| 49 | + year, BOOL_NAMES[tm_is_leap_year(year)]); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +static void cmd_doomsday(int argc, char **argv) |
| 54 | +{ |
| 55 | + int year; |
| 56 | + if ((argc != 2) || (sscanf(argv[1], "%d", &year) != 1)) { |
| 57 | + printf("Usage: %s <Year>\n", argv[0]); |
| 58 | + } |
| 59 | + else { |
| 60 | + printf("What weekday was MAR 0 of %d? %.3s.\n", |
| 61 | + year, DAY_NAMES[tm_doomsday(year) % 7]); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +static void cmd_day(int argc, char **argv) |
| 66 | +{ |
| 67 | + int year, mon, day; |
| 68 | + if ((argc != 4) || (sscanf(argv[1], "%d", &year) != 1) |
| 69 | + || (sscanf(argv[2], "%d", &mon) != 1) |
| 70 | + || (sscanf(argv[3], "%d", &day) != 1)) { |
| 71 | + printf("Usage: %s <Year> <Month[1..12]> <Day[1..31]>\n", argv[0]); |
| 72 | + } |
| 73 | + else { |
| 74 | + if (!tm_is_valid_date(year, mon - 1, day)) { |
| 75 | + puts("The supplied date is invalid, but no error should occur."); |
| 76 | + } |
| 77 | + |
| 78 | + int wday, yday; |
| 79 | + tm_get_wyday(year, mon - 1, day, &wday, &yday); |
| 80 | + printf("What weekday was %04d-%02d-%02d? The %d(th) day of the year was a %.3s.\n", |
| 81 | + year, mon, day, yday + 1, DAY_NAMES[wday]); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +static const shell_command_t shell_commands[] = { |
| 86 | + { "days_in", "Tells you the number of days in a month.", cmd_days_in }, |
| 87 | + { "leap_year", "Tells you if a supplied year is a leap year.", cmd_leap_year }, |
| 88 | + { "doomsday", "Tells you the wday Doomsday of the supplied year.", cmd_doomsday }, |
| 89 | + { "day", "Tells you the day of the supplied date.", cmd_day }, |
| 90 | + { NULL, NULL, NULL } |
| 91 | +}; |
| 92 | + |
| 93 | +static int shell_readc(void) |
| 94 | +{ |
| 95 | + char c; |
| 96 | + int result = posix_read(uart0_handler_pid, &c, 1); |
| 97 | + if (result != 1) { |
| 98 | + return -1; |
| 99 | + } |
| 100 | + return (unsigned char) c; |
| 101 | +} |
| 102 | + |
| 103 | +static void shell_putchar(int c) |
| 104 | +{ |
| 105 | + putchar(c); |
| 106 | +} |
| 107 | + |
| 108 | +int main(void) |
| 109 | +{ |
| 110 | + board_uart0_init(); |
| 111 | + posix_open(uart0_handler_pid, 0); |
| 112 | + |
| 113 | + shell_t shell; |
| 114 | + shell_init(&shell, shell_commands, SHELL_BUFSIZE, shell_readc, shell_putchar); |
| 115 | + |
| 116 | + puts("`struct tm` utility shell."); |
| 117 | + shell_run(&shell); |
| 118 | + |
| 119 | + return 0; |
| 120 | +} |
0 commit comments