Developer World
Spresense Arduino Library v1.4.2-7e8b4bd
RtcTime.h
1 /*
2  * RtcTime.h - Spresense Arduino RTC library
3  * Copyright 2018 Sony Semiconductor Solutions Corporation
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #ifndef __RTCTIME_H__
21 #define __RTCTIME_H__
22 
34 class RtcTime
35 {
36 public:
40  RtcTime(uint32_t sec = 0, long nsec = 0);
41  RtcTime(int year, int month, int day, int hour = 0, int minute = 0, int second = 0);
42  RtcTime(int year, int month, int day, int hour = 0, int minute = 0, int second = 0, long nsec = 0);
43  RtcTime(const char* date, const char* time);
44 
48  uint32_t unixtime() const { return _sec; }
49  long nsec() const { return _nsec; }
50  int year() const { return _year; }
51  int month() const { return _month; }
52  int day() const { return _day; }
53  int hour() const { return _hour; }
54  int minute() const { return _minute; }
55  int second() const { return _second; }
56 
60  void unixtime(uint32_t sec) { _sec = sec; }
61  void nsec(long nsec) { _nsec = nsec; }
62  void year(int year) { _year = year; }
63  void month(int month) { _month = month; }
64  void day(int day) { _day = day; }
65  void hour(int hour) { _hour = hour; }
66  void minute(int minute) { _minute = minute; }
67  void second(int second) { _second = second; }
68 
72  bool operator == (const RtcTime& other) const {
73  return (_sec == other._sec);
74  }
75 
76  bool operator != (const RtcTime& other) const {
77  return !(*this == other);
78  }
79 
80  void operator += (uint32_t seconds) {
81  RtcTime after = RtcTime(unixtime() + seconds);
82  *this = after;
83  }
84 
85  void operator -= (uint32_t seconds) {
86  RtcTime before = RtcTime(unixtime() - seconds);
87  *this = before;
88  }
89 
90  operator uint32_t() const {
91  return unixtime();
92  }
93 
94 private:
95  uint32_t _sec; /* UNIX time in Seconds (time_t) */
96  long _nsec; /* Nanoseconds */
97  int _year; /* Years */
98  int _month; /* Month (1-12) */
99  int _day; /* Day of the month (1-31) */
100  int _hour; /* Hours (0-23) */
101  int _minute; /* Minutes (0-59) */
102  int _second; /* Seconds (0-61, allows for leap seconds) */
103 };
104 
107 #endif // __RTCETIME_H__
RtcTime(uint32_t sec=0, long nsec=0)
Create RtcTime object.
void unixtime(uint32_t sec)
Setter APIs of RtcTime.
Definition: RtcTime.h:60
uint32_t unixtime() const
Getter APIs of RtcTime.
Definition: RtcTime.h:48
RTC time definitions.
Definition: RtcTime.h:34
bool operator==(const RtcTime &other) const
operator APIs to compare and calculate with RtcTime
Definition: RtcTime.h:72