AceTime 2.4.0 Date and time classes for Arduino that support timezones from the TZ Database.

DateStrings.h
1 /*
2 * MIT License
3 * Copyright (c) 2018 Brian T. Park
4 */
5
6 #ifndef ACE_TIME_COMMON_DATE_STRINGS_H
7 #define ACE_TIME_COMMON_DATE_STRINGS_H
8
9 #include <stdint.h>
10 #include <string.h>
11 #include "../../zoneinfo/compat.h" // strncpy_P()
12
13 namespace ace_time {
14
26 class DateStrings {
27 public:
31 static const uint8_t kBufferSize = 10;
32
37 static const uint8_t kShortNameLength = 3;
38
40 const char* monthLongString(uint8_t month) {
41 uint8_t index = (month < kNumMonthNames) ? month : 0;
42 strncpy_P(mBuffer, getStringAt(kMonthNames, index), kBufferSize);
43 mBuffer[kBufferSize - 1] = '\0';
44 return mBuffer;
45 }
46
48 const char* monthShortString(uint8_t month) {
49 uint8_t index = (month < kNumMonthNames) ? month : 0;
50 strncpy_P(mBuffer, getStringAt(kMonthNames, index), kShortNameLength);
51 mBuffer[kShortNameLength] = '\0';
52 return mBuffer;
53 }
54
56 const char* dayOfWeekLongString(uint8_t dayOfWeek) {
57 uint8_t index = (dayOfWeek < kNumDayOfWeekNames) ? dayOfWeek : 0;
58 strncpy_P(mBuffer, getStringAt(kDayOfWeekNames, index), kBufferSize);
59 mBuffer[kBufferSize - 1] = '\0';
60 return mBuffer;
61 }
62
64 const char* dayOfWeekShortString(uint8_t dayOfWeek) {
65 uint8_t index = (dayOfWeek < kNumDayOfWeekNames) ? dayOfWeek : 0;
66 strncpy_P(mBuffer, getStringAt(kDayOfWeekNames, index), kShortNameLength);
67 mBuffer[kShortNameLength] = '\0';
68 return mBuffer;
69 }
70
71 private:
72 static const char* getStringAt(const char* const* strings, uint8_t i) {
73 return (const char*) pgm_read_ptr(&strings[i]);
74 }
75
76 static const char * const kDayOfWeekNames[];
77 static const char * const kMonthNames[];
78 static const uint8_t kNumDayOfWeekNames;
79 static const uint8_t kNumMonthNames;
80
81 char mBuffer[kBufferSize];
82 };
83
84 }
85
86 #endif
ace_time::DateStrings
Class that translates a numeric month (1-12) or dayOfWeek (1-7) into a human readable string.
Definition: DateStrings.h:26
ace_time::DateStrings::dayOfWeekLongString
const char * dayOfWeekLongString(uint8_t dayOfWeek)
Return the short dayOfWeek name.
Definition: DateStrings.h:56
ace_time::DateStrings::dayOfWeekShortString
const char * dayOfWeekShortString(uint8_t dayOfWeek)
Return the short dayOfWeek name.
Definition: DateStrings.h:64
ace_time::DateStrings::monthLongString
const char * monthLongString(uint8_t month)
Return the long month name.
Definition: DateStrings.h:40
ace_time::DateStrings::monthShortString
const char * monthShortString(uint8_t month)
Return the short month name.
Definition: DateStrings.h:48
ace_time::DateStrings::kShortNameLength
static const uint8_t kShortNameLength
Number of prefix characters to use to create a short name.
Definition: DateStrings.h:37
ace_time::DateStrings::kBufferSize
static const uint8_t kBufferSize
Length of the longest month or week name, including the '\0' terminator.
Definition: DateStrings.h:31