Blame view

RIOT/tests/struct_tm_utility/main.c 3.83 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
  /*
   * Copyright (C) 2014  René Kijewski  <rene.kijewski@fu-berlin.de>
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public
   * License as published by the Free Software Foundation; either
   * version 2.1 of the License, or (at your option) any later version.
   *
   * This library is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this library; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
   */
  
  /**
   * @ingroup  tests
   * @{
   * @file
   * @brief    Test the `struct tm` helpers in "tm.h" of the module "timex".
   * @author   René Kijewski <rene.kijewski@fu-berlin.de>
   * @}
   */
  
  #include <stdio.h>
  #include <stdlib.h>
  #include <stdbool.h>
  
  #include "shell.h"
  #include "tm.h"
  
  static const char MON_NAMES[12][3] = {
      "JAN", "FEB", "MAR", "APR",
      "MAY", "JUN", "JUL", "AUG",
      "SEP", "OCT", "NOV", "DEC",
  };
  static const char DAY_NAMES[7][3] = {
      "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"
  };
  static const char BOOL_NAMES[2][3] = { "NO", "YES" };
  
  bool proper_atoi(const char *a, int *i)
  {
      char *end;
      *i = strtol(a, &end, 0);
      return (a != end) && (*end == '\0');
  }
  
  
  static int cmd_days_in(int argc, char **argv)
  {
      int mon;
      if ((argc != 2) || (proper_atoi(argv[1], &mon) != 1) || (mon < 1) || (mon > 12)) {
          printf("Usage: %s <Month[1..12]>\n", argv[0]);
  
          return 1;
      }
      else {
          printf("There are %d days in %.3s in common years.\n",
                 TM_MON_DAYS[mon - 1], MON_NAMES[mon - 1]);
  
          return 0;
      }
  }
  
  static int cmd_leap_year(int argc, char **argv)
  {
      int year;
      if ((argc != 2) || (proper_atoi(argv[1], &year) != 1)) {
          printf("Usage: %s <Year>\n", argv[0]);
  
          return 1;
      }
      else {
          printf("Was %d a leap year? %.3s.\n",
                 year, BOOL_NAMES[tm_is_leap_year(year)]);
  
          return 0;
      }
  }
  
  static int cmd_doomsday(int argc, char **argv)
  {
      int year;
      if ((argc != 2) || (proper_atoi(argv[1], &year) != 1)) {
          printf("Usage: %s <Year>\n", argv[0]);
  
          return 1;
      }
      else {
          printf("What weekday was MAR 0 of %d? %.3s.\n",
                 year, DAY_NAMES[tm_doomsday(year) % 7]);
  
          return 0;
      }
  }
  
  static int cmd_day(int argc, char **argv)
  {
      int year, mon, day;
      if ((argc != 4) || (proper_atoi(argv[1], &year) != 1)
                      || (proper_atoi(argv[2], &mon) != 1)
                      || (proper_atoi(argv[3], &day) != 1)) {
          printf("Usage: %s <Year> <Month[1..12]> <Day[1..31]>\n", argv[0]);
  
          return 1;
      }
      else {
          if (!tm_is_valid_date(year, mon - 1, day)) {
              puts("The supplied date is invalid, but no error should occur.");
          }
  
          int wday, yday;
          tm_get_wyday(year, mon - 1, day, &wday, &yday);
          printf("What weekday was %04d-%02d-%02d? The %d(th) day of the year was a %.3s.\n",
                 year, mon, day, yday + 1, DAY_NAMES[wday]);
  
          return 0;
      }
  }
  
  static const shell_command_t shell_commands[] = {
      { "days_in", "Tells you the number of days in a month.", cmd_days_in },
      { "leap_year", "Tells you if a supplied year is a leap year.", cmd_leap_year },
      { "doomsday", "Tells you the wday Doomsday of the supplied year.", cmd_doomsday },
      { "day", "Tells you the day of the supplied date.", cmd_day },
      { NULL, NULL, NULL }
  };
  
  int main(void)
  {
      puts("`struct tm` utility shell.");
  
      char line_buf[SHELL_DEFAULT_BUFSIZE];
      shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);
  
      return 0;
  }