AceTime  0.1
Date and time classes for Arduino that supports the TZ DAtabase, and a system clock synchronized from an NTP server or an RTC chip.
TimeOffset.cpp
1 #include <string.h> // strlen()
2 #include "common/util.h"
3 #include "common/DateStrings.h"
4 #include "TimeOffset.h"
5 
6 namespace ace_time {
7 
8 using common::printPad2;
9 
10 void TimeOffset::printTo(Print& printer) const {
11  int8_t hour;
12  uint8_t minute;
13  toHourMinute(hour, minute);
14 
15  printer.print((hour < 0) ? '-' : '+');
16  if (hour < 0) {
17  hour = -hour;
18  }
19  common::printPad2(printer, hour);
20  printer.print(':');
21  common::printPad2(printer, minute);
22 }
23 
24 TimeOffset TimeOffset::forOffsetString(const char* offsetString) {
25  // verify exact ISO 8601 string length
26  if (strlen(offsetString) != kTimeOffsetStringLength) {
27  return forError();
28  }
29 
30  return forOffsetStringChainable(offsetString);
31 }
32 
33 TimeOffset TimeOffset::forOffsetStringChainable(const char*& offsetString) {
34  const char* s = offsetString;
35 
36  // '+' or '-'
37  char utcSign = *s++;
38  if (utcSign != '-' && utcSign != '+') {
39  return forError();
40  }
41 
42  // hour
43  uint8_t hour = (*s++ - '0');
44  hour = 10 * hour + (*s++ - '0');
45  s++;
46 
47  // minute
48  uint8_t minute = (*s++ - '0');
49  minute = 10 * minute + (*s++ - '0');
50  s++;
51 
52  offsetString = s;
53  return TimeOffset::forHourMinute((utcSign == '+') ? hour : -hour, minute);
54 }
55 
56 }
static TimeOffset forError()
Return an error indicator.
Definition: TimeOffset.h:90
void toHourMinute(int8_t &hour, uint8_t &minute) const
Extract hour and minute representation of the offset.
Definition: TimeOffset.h:125
static TimeOffset forOffsetString(const char *offsetString)
Create from an offset string ("-07:00" or "+01:00").
Definition: TimeOffset.cpp:24
static TimeOffset forHourMinute(int8_t hour, uint8_t minute)
Create TimeOffset from (hour, minute) offset, where the sign of hour determines the sign of the offse...
Definition: TimeOffset.h:69
A thin wrapper that represents a time offset from a reference point, usually 00:00 at UTC...
Definition: TimeOffset.h:53
void printTo(Print &printer) const
Print the human readable string.
Definition: TimeOffset.cpp:10