Skip to content

Commit 9cde1e5

Browse files
Merge pull request #1146 from Kijewski/struct-tm-utils
sys: add utility functions for `struct tm`
2 parents a2fb115 + bd2fbd1 commit 9cde1e5

File tree

4 files changed

+331
-0
lines changed

4 files changed

+331
-0
lines changed

sys/include/tm.h

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* @addtogroup sys_timex
3+
* @{
4+
* @brief Utility library for `struct tm`.
5+
*/
6+
7+
#ifndef __SYS__TIMEX__TM__H
8+
#define __SYS__TIMEX__TM__H
9+
10+
#include <time.h>
11+
#include <sys/time.h>
12+
#include <stdint.h>
13+
14+
#include "attributes.h"
15+
16+
#define TM_WDAY_SUN (0) /**< Sunday in `struct tm::tm_wday`. */
17+
#define TM_WDAY_MON (1) /**< Monday in `struct tm::tm_wday`. */
18+
#define TM_WDAY_TUE (2) /**< Tuesday in `struct tm::tm_wday`. */
19+
#define TM_WDAY_WED (3) /**< Wednesday in `struct tm::tm_wday`. */
20+
#define TM_WDAY_THU (4) /**< Thursday in `struct tm::tm_wday`. */
21+
#define TM_WDAY_FRI (5) /**< Friday in `struct tm::tm_wday`. */
22+
#define TM_WDAY_SAT (6) /**< Saturday in `struct tm::tm_wday`. */
23+
24+
#define TM_MON_JAN ( 0) /**< January in `struct tm::tm_mon` */
25+
#define TM_MON_FEB ( 1) /**< February in `struct tm::tm_mon` */
26+
#define TM_MON_MAR ( 2) /**< March in `struct tm::tm_mon` */
27+
#define TM_MON_APR ( 3) /**< April in `struct tm::tm_mon` */
28+
#define TM_MON_MAY ( 4) /**< May in `struct tm::tm_mon` */
29+
#define TM_MON_JUN ( 5) /**< June in `struct tm::tm_mon` */
30+
#define TM_MON_JUL ( 6) /**< July in `struct tm::tm_mon` */
31+
#define TM_MON_AUG ( 7) /**< August in `struct tm::tm_mon` */
32+
#define TM_MON_SEP ( 8) /**< September in `struct tm::tm_mon` */
33+
#define TM_MON_OCT ( 9) /**< October in `struct tm::tm_mon` */
34+
#define TM_MON_NOV (10) /**< November in `struct tm::tm_mon` */
35+
#define TM_MON_DEC (11) /**< December in `struct tm::tm_mon` */
36+
37+
/**
38+
* @brief The number of days in common years.
39+
* @see http://oeis.org/A008685
40+
*/
41+
extern const int8_t TM_MON_DAYS[12];
42+
43+
/**
44+
* @brief The prefixsum of the number of days in common years.
45+
* @see http://oeis.org/A061251
46+
*/
47+
extern const int16_t TM_MON_DAYS_ACCU[12];
48+
49+
/**
50+
* @brief Tells if a given year is a leap year in the Gregorian calendar.
51+
* @param[in] year The year. Probably should be ≥ 1582, but needs to be ≥ 1.
52+
* @returns `1` if it is a leap year, `0` if it is a common year.
53+
*/
54+
int tm_is_leap_year(unsigned year) CONST;
55+
56+
/**
57+
* @brief Returns the congruent weekday of the Doomsday (March 0).
58+
* @details Only applies for years in the Gregorian calendar.
59+
* @param[in] year The year. Probably should be ≥ 1582, but needs to be ≥ 1.
60+
* @returns The result `% 7` is the weekday of the Doomsday of the given year.
61+
*/
62+
int tm_doomsday(int year) CONST;
63+
64+
/**
65+
* @brief Calculates the day of the year and the weekday of a given date.
66+
* @details Illegal dates are not catched.
67+
* @param[in] year The year. Probably should be ≥ 1582, but needs to be ≥ 1.
68+
* @param[in] mon The month, TM_MON_JAN to TM_MON_DEC.
69+
* @param[in] day The day in the month, 1 to 31.
70+
* @param[out] wday Returns the day of the week.
71+
* @param[out] yday Returns the day of the year (Jan 1st is 0).
72+
*/
73+
void tm_get_wyday(int year, int mon, int mday, int *wday, int *yday);
74+
75+
/**
76+
* @brief Fills in `struct tm::tm_wday` and `struct tm::tm_yday` given a date.
77+
* @details `struct tm::tm_year`, `struct tm::tm_mon`, and `struct tm::tm_mday`
78+
* need to be set before you call this function.
79+
* @param[in,out] The datum to operate on.
80+
*/
81+
void tm_fill_derived_values(struct tm *tm);
82+
83+
/**
84+
* @brief Tests if a date is valid.
85+
* @details Dates before 1582-10-15 are invalid.
86+
* @param[in] year The year.
87+
* @param[in] mon The month.
88+
* @param[in] day The day in the month.
89+
* @returns 0 iff the date is invalid.
90+
*/
91+
int tm_is_valid_date(int year, int mon, int mday) CONST;
92+
93+
/**
94+
* @brief Shallow test if a time is valid.
95+
* @details This function accepts leap seconds at any given time, because the timezone is unknown.
96+
* @param[in] hour The hour.
97+
* @param[in] min The minutes.
98+
* @param[in] sec The seconds.
99+
* @returns 0 iff the time is invalid.
100+
*/
101+
int tm_is_valid_time(int hour, int min, int sec) CONST;
102+
103+
#endif

sys/timex/tm.c

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright (C) 2014 René Kijewski <[email protected]>
3+
*
4+
* This library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* This library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with this library; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
/**
20+
* @file
21+
* @author René Kijewski <[email protected]>
22+
*/
23+
24+
#include "tm.h"
25+
26+
#include <stdint.h>
27+
28+
const int8_t TM_MON_DAYS[12] = {
29+
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
30+
};
31+
32+
const int16_t TM_MON_DAYS_ACCU[12] = {
33+
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
34+
};
35+
36+
int tm_is_leap_year(unsigned year)
37+
{
38+
return ((year & 3) == 0) && ((year % 400 == 0) || (year % 100 != 0));
39+
}
40+
41+
int tm_doomsday(int year)
42+
{
43+
int result;
44+
result = TM_WDAY_TUE;
45+
result += year;
46+
result += year >>= 2;
47+
result -= year /= 25;
48+
result += year >>= 2;
49+
return result;
50+
}
51+
52+
void tm_get_wyday(int year, int mon, int mday, int *wday, int *yday)
53+
{
54+
int is_leap_year = tm_is_leap_year(year);
55+
*yday = TM_MON_DAYS_ACCU[mon] + mday + (mon <= TM_MON_FEB ? 0 : is_leap_year) - 1;
56+
int jan1 = tm_doomsday(year) - 2 - is_leap_year;
57+
*wday = (jan1 + *yday) % 7;
58+
}
59+
60+
void tm_fill_derived_values(struct tm *tm)
61+
{
62+
tm_get_wyday(tm->tm_year + 1900, tm->tm_mon, tm->tm_mday, &tm->tm_wday, &tm->tm_yday);
63+
}
64+
65+
int tm_is_valid_date(int year, int mon, int mday)
66+
{
67+
if ((mon < TM_MON_JAN) || (mon > TM_MON_DEC)) {
68+
return 0;
69+
}
70+
if ((mday <= 0) || (mday > TM_MON_DAYS[mon])) {
71+
if ((mday != 29) || (mon != TM_MON_FEB) || !tm_is_leap_year(year)) {
72+
return 0;
73+
}
74+
}
75+
if (year <= 1582) {
76+
if (year < 1582) {
77+
return 0;
78+
}
79+
if ((mon < TM_MON_OCT) || ((mon == TM_MON_OCT) && (mday < 15))) {
80+
return 0;
81+
}
82+
}
83+
84+
return 1;
85+
}
86+
87+
int tm_is_valid_time(int hour, int min, int sec)
88+
{
89+
return (hour >= 0) && (hour < 24) &&
90+
(min >= 0) && (min < 60) &&
91+
(sec >= 0) && (sec <= 60);
92+
}

tests/test_struct_tm_utility/Makefile

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
PROJECT = test_struct_tm_utility
2+
include ../Makefile.tests_common
3+
4+
DISABLE_MODULE += auto_init
5+
6+
USEMODULE += shell
7+
USEMODULE += posix
8+
USEMODULE += timex
9+
10+
# The MSP-430 toolchain lacks sscanf:
11+
BOARD_BLACKLIST := chronos msb-430 msb-430h telosb wsn430-v1_3b wsn430-v1_4 z1
12+
13+
# Too little RAM
14+
BOARD_BLACKLIST += redbee-econotag
15+
16+
include $(RIOTBASE)/Makefile.include

tests/test_struct_tm_utility/main.c

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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

Comments
 (0)