summaryrefslogtreecommitdiffstats
path: root/firmware/datetime.h
blob: f39e49335a14d12daeb8fdc5e88d90741e94c3af (plain)
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
#ifndef DATETIME_H_
#define DATETIME_H_

#include "util.h"

#include <stdint.h>


/* RTC representation of date and time. */
struct rtc_time {
	/* The second. 0-59 */
	uint8_t second;
	/* The minute. 0-59 */
	uint8_t minute;
	/* The hour. 0-23 */
	uint8_t hour;
	/* The day. 0-30 */
	uint8_t day;
	/* The month. 0-11 */
	uint8_t month;
	/* The year. 0-99 */
	uint8_t year;
	/* The day of the week. 0-6 */
	uint8_t day_of_week;
};

/* Type representing the time of the day, in 2-second resolution.
 * This is a count of double-seconds since midnight.
 */
typedef uint16_t time_of_day_t;

/* Representation of a time range in a day.
 * It features a start time and an end time.
 */
struct time_of_day_range {
	/* The start time of the range. */
	time_of_day_t from;
	/* The end time of the range. */
	time_of_day_t to;
};

/* Timestamp type with second granularity.
 * For easy convertability from struct rtc_time, this type
 * stores most of the rtc_time fields in a packed format
 * instead of using a count of seconds since some point in time (epoch).
 *
 * The bit-allocation of the 32-bit timestamp_t type is:
 *
 * Bit 0 - 5:	Seconds
 *		Value range: 0-59
 * Bit 6 - 11:	Minutes
 *		Value range: 0-59
 * Bit 12 - 16:	Hours
 *		Value range: 0-23
 * Bit 17 - 21:	Days
 *		Value range: 0-30
 * Bit 22 - 25:	Months
 *		Value range: 0-11
 * Bit 26 - 31:	Years
 *		Value range: 0-63 (= 2000-2063)
 */
typedef uint32_t timestamp_t;


void rtc_time_to_bcd(struct rtc_time *bcd_time,
		     const struct rtc_time *time);

void rtc_time_from_bcd(struct rtc_time *time,
		       const struct rtc_time *bcd_time);

time_of_day_t rtc_get_time_of_day(const struct rtc_time *time);

/* Return 1, if time_a is after time_b.
 * Return 0 otherwise.
 */
static inline bool time_of_day_after(time_of_day_t time_a,
				     time_of_day_t time_b)
{
	return time_a > time_b;
}

/* Return 1, if time_a is before time_b.
 * Return 0 otherwise.
 */
static inline bool time_of_day_before(time_of_day_t time_a,
				      time_of_day_t time_b)
{
	return time_a < time_b;
}

timestamp_t rtc_get_timestamp(const struct rtc_time *time);

#endif /* DATETIME_H_ */
bues.ch cgit interface