AceTime  0.3
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.
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  int8_t minute;
13  toHourMinute(hour, minute);
14 
15  if (mOffsetCode < 0) {
16  printer.print('-');
17  hour = -hour;
18  minute = -minute;
19  } else {
20  printer.print('+');
21  }
22  common::printPad2(printer, hour);
23  printer.print(':');
24  common::printPad2(printer, minute);
25 }
26 
27 TimeOffset TimeOffset::forOffsetString(const char* offsetString) {
28  // verify exact ISO 8601 string length
29  if (strlen(offsetString) != kTimeOffsetStringLength) {
30  return forError();
31  }
32 
33  return forOffsetStringChainable(offsetString);
34 }
35 
37  const char* s = offsetString;
38 
39  // '+' or '-'
40  char utcSign = *s++;
41  if (utcSign != '-' && utcSign != '+') {
42  return forError();
43  }
44 
45  // hour
46  uint8_t hour = (*s++ - '0');
47  hour = 10 * hour + (*s++ - '0');
48  s++;
49 
50  // minute
51  uint8_t minute = (*s++ - '0');
52  minute = 10 * minute + (*s++ - '0');
53  s++;
54 
55  offsetString = s;
56  if (utcSign == '+') {
57  return forHourMinute(hour, minute);
58  } else {
59  return forHourMinute(-hour, -minute);
60  }
61 }
62 
63 }
static TimeOffset forError()
Return an error indicator.
Definition: TimeOffset.h:105
static TimeOffset forOffsetStringChainable(const char *&offsetString)
Variant of forOffsetString() that updates the string pointer to the next unprocessed character...
Definition: TimeOffset.cpp:36
static TimeOffset forOffsetString(const char *offsetString)
Create from an offset string ("-07:00" or "+01:00").
Definition: TimeOffset.cpp:27
void printTo(Print &printer) const
Print the human readable string.
Definition: TimeOffset.cpp:10
static TimeOffset forHourMinute(int8_t hour, int8_t minute)
Create TimeOffset from (hour, minute) offset.
Definition: TimeOffset.h:74
A thin wrapper that represents a time offset from a reference point, usually 00:00 at UTC...
Definition: TimeOffset.h:53
void toHourMinute(int8_t &hour, int8_t &minute) const
Extract hour and minute representation of the offset.
Definition: TimeOffset.h:137