• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #ifndef HDF_RTC_BASE_H
10 #define HDF_RTC_BASE_H
11 
12 #include "rtc_if.h"
13 
14 #ifdef __cplusplus
15 #if __cplusplus
16 extern "C" {
17 #endif
18 #endif /* __cplusplus */
19 
20 #define IS_LEAP_YEAR(year)     (((((year) % 4) == 0) && (((year) % 100) != 0)) || (((year) % 400) == 0))
21 #define RTC_YEAR_DAYS(year)    (IS_LEAP_YEAR(year) ? 366 : 365)
22 
23 #define RTC_DAY_SECONDS        86400ULL /* 24 hours * 60 minutes * 60 seconds */
24 #define RTC_DAY_HOURS          24       /* 24 hours */
25 #define RTC_HOUR_SECONDS       3600UL   /* 60 minutes * 60 seconds */
26 #define RTC_MAX_MONTH          12
27 #define RTC_MAX_WEEKDAY        7
28 #define RTC_GREAT_MONTH_DAY    31
29 #define RTC_SMALL_MONTH_DAY    30
30 #define RTC_TWO_MONTH_DAY      28
31 #define RTC_MAX_HOUR           24
32 #define RTC_MAX_MINUTE         60UL
33 #define RTC_MAX_SECOND         60UL
34 #define RTC_MAX_MS             1000
35 #define RTC_UNIT_DIFF          1
36 #define RTC_ODD_MONTH_MASK     0x1
37 #define RTC_TIME_UNIT          60
38 #define RTC_FALSE              0
39 #define RTC_TRUE               1
40 
41 #define RTC_BEGIN_YEAR         1970 /* UTC started at 00:00:00 January 1, 1970 */
42 #define RTC_BEGIN_WEEKDAY      0x04 /* Thursday January 1, 1970 */
43 
44 #define IS_INVALID_YEAR(year)    ((year) < RTC_BEGIN_YEAR)
45 #define IS_INVALID_MONTH(m)      (((m) < RTC_JANUARY) || ((m) > RTC_MAX_MONTH))
46 #define IS_INVALID_WEEKDAY(wd)   (((wd) < 1) || ((wd) > RTC_MAX_WEEKDAY))
47 #define IS_INVALID_HOUR(hour)    ((hour) >= RTC_MAX_HOUR)
48 #define IS_INVALID_MIN(min)      ((min) >= RTC_MAX_MINUTE)
49 #define IS_INVALID_SECOND(s)     ((s) >= RTC_MAX_SECOND)
50 #define IS_INVALID_MS(ms)        ((ms) >= RTC_MAX_MS)
51 
52 enum RtcMonth {
53     RTC_JANUARY = 1,
54     RTC_FEBRUARY,
55     RTC_MARCH,
56     RTC_APRIL,
57     RTC_MAY,
58     RTC_JUNE,
59     RTC_JULY,
60     RTC_AUGUST,
61     RTC_SEPTEMBER,
62     RTC_OCTOBER,
63     RTC_NOVEMBER,
64     RTC_DECEMBER,
65 };
66 
67 uint8_t RtcGetMonthDays(const uint8_t isLeapYear, const uint8_t month);
68 uint8_t RtcIsInvalidDay(const uint8_t day, const uint8_t month, const uint16_t year);
69 uint8_t RtcIsInvalid(const struct RtcTime *time);
70 uint8_t RtcGetWeekDay(const struct RtcTime *time);
71 #ifndef __KERNEL__
72 uint64_t RtcTimeToTimestamp(const struct RtcTime *time);
73 void TimestampToRtcTime(struct RtcTime *time, const uint64_t seconds);
74 #endif
75 
76 #ifdef __cplusplus
77 #if __cplusplus
78 }
79 #endif
80 #endif /* __cplusplus */
81 
82 #endif /* HDF_RTC_BASE_H */
83