|
| 1 | +/* |
| 2 | + MIT License |
| 3 | +
|
| 4 | + Copyright (c) 2021 Felix Biego |
| 5 | +
|
| 6 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + of this software and associated documentation files (the "Software"), to deal |
| 8 | + in the Software without restriction, including without limitation the rights |
| 9 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + copies of the Software, and to permit persons to whom the Software is |
| 11 | + furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | + The above copyright notice and this permission notice shall be included in all |
| 14 | + copies or substantial portions of the Software. |
| 15 | +
|
| 16 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + SOFTWARE. |
| 23 | +*/ |
| 24 | + |
| 25 | +#include <ESP32Time.h> |
| 26 | + |
| 27 | +ESP32Time rtc; |
| 28 | +ESP32Time rtc1(-3600); // offset GMT-1 |
| 29 | +ESP32Time rtc2(7200); // offset GMT+2 |
| 30 | + |
| 31 | +void setup() { |
| 32 | + Serial.begin(115200); |
| 33 | + rtc.setTime(30, 24, 15, 17, 1, 2021); // 17th Jan 2021 15:24:30 |
| 34 | + //rtc1.setTime(1609459200); // 1st Jan 2021 00:00:00 |
| 35 | + // time can be set on one instance |
| 36 | + // no need for rtc1.setTime() or rtc2.setTime() |
| 37 | + |
| 38 | + |
| 39 | +} |
| 40 | + |
| 41 | +void loop() { |
| 42 | +// Serial.println(rtc.getTime()); // (String) 15:24:38 |
| 43 | +// Serial.println(rtc.getDate()); // (String) Sun, Jan 17 2021 |
| 44 | +// Serial.println(rtc.getDate(true)); // (String) Sunday, January 17 2021 |
| 45 | +// Serial.println(rtc.getDateTime()); // (String) Sun, Jan 17 2021 15:24:38 |
| 46 | +// Serial.println(rtc.getDateTime(true)); // (String) Sunday, January 17 2021 15:24:38 |
| 47 | +// Serial.println(rtc.getTimeDate()); // (String) 15:24:38 Sun, Jan 17 2021 |
| 48 | +// Serial.println(rtc.getTimeDate(true)); // (String) 15:24:38 Sunday, January 17 2021 |
| 49 | +// |
| 50 | +// Serial.println(rtc.getMicros()); // (long) 723546 |
| 51 | +// Serial.println(rtc.getMillis()); // (long) 723 |
| 52 | +// Serial.println(rtc.getEpoch()); // (long) 1609459200 |
| 53 | +// Serial.println(rtc.getSecond()); // (int) 38 (0-59) |
| 54 | +// Serial.println(rtc.getMinute()); // (int) 24 (0-59) |
| 55 | +// Serial.println(rtc.getHour()); // (int) 3 (0-12) |
| 56 | +// Serial.println(rtc.getHour(true)); // (int) 15 (0-23) |
| 57 | +// Serial.println(rtc.getAmPm()); // (String) pm |
| 58 | +// Serial.println(rtc.getAmPm(true)); // (String) PM |
| 59 | +// Serial.println(rtc.getDay()); // (int) 17 (1-31) |
| 60 | +// Serial.println(rtc.getDayofWeek()); // (int) 0 (0-6) |
| 61 | +// Serial.println(rtc.getDayofYear()); // (int) 16 (0-365) |
| 62 | +// Serial.println(rtc.getMonth()); // (int) 0 (0-11) |
| 63 | +// Serial.println(rtc.getYear()); // (int) 2021 |
| 64 | + |
| 65 | + Serial.println(rtc.getTime("RTC0: %A, %B %d %Y %H:%M:%S")); // (String) returns time with specified format |
| 66 | + Serial.println(rtc1.getTime("RTC1: %A, %B %d %Y %H:%M:%S")); // (String) returns time with specified format |
| 67 | + Serial.println(rtc2.getTime("RTC2: %A, %B %d %Y %H:%M:%S")); // (String) returns time with specified format |
| 68 | + |
| 69 | + // formating options http://www.cplusplus.com/reference/ctime/strftime/ |
| 70 | + |
| 71 | + Serial.println(rtc.getEpoch()); // (long) |
| 72 | + Serial.println(rtc1.getEpoch()); // (long) |
| 73 | + Serial.println(rtc2.getEpoch()); // (long) |
| 74 | + |
| 75 | + Serial.println(rtc.getLocalEpoch()); // (long) epoch without offset, same for all instances |
| 76 | + delay(1000); |
| 77 | +} |
0 commit comments