AceTime  1.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.
TimeOffset.cpp
1 /*
2  * MIT License
3  * Copyright (c) 2018 Brian T. Park
4  */
5 
6 #include <string.h> // strlen()
7 #include "common/util.h"
8 #include "common/DateStrings.h"
9 #include "TimeOffset.h"
10 
11 namespace ace_time {
12 
13 using common::printPad2;
14 
15 void TimeOffset::printTo(Print& printer) const {
16  int8_t hour;
17  int8_t minute;
18  toHourMinute(hour, minute);
19 
20  if (mMinutes < 0) {
21  printer.print('-');
22  hour = -hour;
23  minute = -minute;
24  } else {
25  printer.print('+');
26  }
27  common::printPad2(printer, hour);
28  printer.print(':');
29  common::printPad2(printer, minute);
30 }
31 
32 TimeOffset TimeOffset::forOffsetString(const char* offsetString) {
33  // verify exact ISO 8601 string length
34  if (strlen(offsetString) != kTimeOffsetStringLength) {
35  return forError();
36  }
37 
38  return forOffsetStringChainable(offsetString);
39 }
40 
42  const char* s = offsetString;
43 
44  // '+' or '-'
45  char utcSign = *s++;
46  if (utcSign != '-' && utcSign != '+') {
47  return forError();
48  }
49 
50  // hour
51  uint8_t hour = (*s++ - '0');
52  hour = 10 * hour + (*s++ - '0');
53  s++;
54 
55  // minute
56  uint8_t minute = (*s++ - '0');
57  minute = 10 * minute + (*s++ - '0');
58  s++;
59 
60  offsetString = s;
61  if (utcSign == '+') {
62  return forHourMinute(hour, minute);
63  } else {
64  return forHourMinute(-hour, -minute);
65  }
66 }
67 
68 }
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:41
static TimeOffset forOffsetString(const char *offsetString)
Create from an offset string ("-07:00" or "+01:00").
Definition: TimeOffset.cpp:32
void printTo(Print &printer) const
Print the human readable string.
Definition: TimeOffset.cpp:15
static TimeOffset forHourMinute(int8_t hour, int8_t minute)
Create TimeOffset from (hour, minute) offset.
Definition: TimeOffset.h:77
A thin wrapper that represents a time offset from a reference point, usually 00:00 at UTC...
Definition: TimeOffset.h:56
void toHourMinute(int8_t &hour, int8_t &minute) const
Extract hour and minute representation of the offset.
Definition: TimeOffset.h:125