AceTime  0.6.1
Date and time classes for Arduino that support timezones from the TZ Database, and a system clock that can synchronize from an NTP server or an RTC chip.
util.h
1 /*
2  * MIT License
3  * Copyright (c) 2018 Brian T. Park
4  */
5 
6 #ifndef ACE_TIME_COMMON_UTIL_H
7 #define ACE_TIME_COMMON_UTIL_H
8 
9 #include <stdint.h>
10 #include <Print.h>
11 
12 namespace ace_time {
13 namespace common {
14 
19 template<typename T>
20 void incrementMod(T& d, T m) {
21  d++;
22  if (d >= m) d = 0;
23 }
24 
29 template<typename T>
30 void incrementMod(T& d, T m, T offset) {
31  d -= offset;
32  d++;
33  if (d >= m) d = 0;
34  d += offset;
35 }
36 
38 inline uint8_t decToBcd(uint8_t val) {
39  return (val/10*16) + (val%10);
40 }
41 
43 inline uint8_t bcdToDec(uint8_t val) {
44  return (val/16*10) + (val%16);
45 }
46 
48 inline void printPad2(Print& printer, uint8_t value, char padChar = '0') {
49  if (value < 10) printer.print(padChar);
50  printer.print(value);
51 }
52 
54 inline void printPad3(Print& printer, uint16_t val, char padChar = '0') {
55  if (val < 100) printer.print(padChar);
56  if (val < 10) printer.print(padChar);
57  printer.print(val);
58 }
59 
60 }
61 }
62 
63 #endif