LocalTimeRK
|
Perform time conversions. This is the main class you will need. More...
#include <LocalTimeRK.h>
Public Types | |
enum class | Position { BEFORE_DST , IN_DST , AFTER_DST , BEFORE_STANDARD , IN_STANDARD , AFTER_STANDARD , NO_DST } |
Whether the specified time is DST or not. See also isDST(). More... | |
Public Member Functions | |
LocalTimeConvert & | withConfig (LocalTimePosixTimezone config) |
Sets the timezone configuration to use for time conversion. More... | |
LocalTimeConvert & | withTime (time_t time) |
Sets the UTC time to begin conversion from. More... | |
LocalTimeConvert & | withCurrentTime () |
Use the current time as the time to start with. More... | |
void | convert () |
Do the time conversion. More... | |
bool | isDST () const |
Returns true if the current time is in daylight saving time. | |
bool | isStandardTime () const |
Returns true of the current time in in standard time. | |
void | addSeconds (int seconds) |
Adds a number of seconds to the current object. More... | |
void | nextMinuteMultiple (int increment, int startingModulo=0) |
Moves the current time the next specified multiple of minutes. More... | |
void | nextTime (LocalTimeHMS hms) |
Moves the current time the next specified local time. This could be today or tomorrow. More... | |
void | nextTimeList (std::initializer_list< LocalTimeHMS > hmsList) |
Moves the current time the closest local time om hmsList. This could be today or tomorrow. More... | |
void | nextDay (LocalTimeHMS hms=LocalTimeIgnoreHMS()) |
Moves the current time to the next day. More... | |
void | nextDayOrTimeChange (LocalTimeHMS hms=LocalTimeIgnoreHMS()) |
Moves the current to the next day, or right after the next time change, whichever comes first. More... | |
bool | nextDayOfWeek (int dayOfWeek, LocalTimeHMS hms=LocalTimeIgnoreHMS()) |
Moves the current time to the next of the specified day of week. More... | |
void | nextWeekday (LocalTimeHMS hms=LocalTimeIgnoreHMS()) |
Returns the next day that is a weekday (Monday - Friday) More... | |
void | nextWeekendDay (LocalTimeHMS hms=LocalTimeIgnoreHMS()) |
Returns the next day that is a weekend day (Saturday or Sunday) More... | |
bool | nextDayOfMonth (int dayOfMonth, LocalTimeHMS hms=LocalTimeIgnoreHMS()) |
Moves the date and time (local time) forward to the specified day of month and local time. More... | |
bool | nextDayOfNextMonth (int dayOfMonth, LocalTimeHMS hms=LocalTimeIgnoreHMS()) |
Moves the date and time (local time) forward to the specified day of month and local time. More... | |
bool | nextDayOfWeekOrdinal (int dayOfWeek, int ordinal, LocalTimeHMS hms=LocalTimeIgnoreHMS()) |
Moves the date and time (local time) forward to the specified day of month and local time. More... | |
void | nextLocalTime (LocalTimeHMS hms) |
Sets the time to the nearest hms in local time in the future. More... | |
bool | nextSchedule (const LocalTimeSchedule &schedule) |
Sets the time to the nearest scheduled time in the future base on the schedule. More... | |
void | atLocalTime (LocalTimeHMS hms) |
Changes the time of day to the specified hms in local time on the same local day. More... | |
LocalTimeHMS | getLocalTimeHMS () const |
Get the value of this object as a LocalTimeHMS (hour minute second) More... | |
LocalTimeYMD | getLocalTimeYMD () const |
Get the value of this object as a LocalTimeYMD (year month day0) More... | |
String | timeStr () |
Works like Time.timeStr() to generate a readable string of the local time. More... | |
String | format (const char *formatSpec) |
Works like Time.format() More... | |
String | zoneName () const |
Returns the abbreviated time zone name for the current time. More... | |
int | lastDayOfMonth () const |
Returns the last day of the month, local time (based on localTimeValue) | |
Data Fields | |
Position | position = Position::NO_DST |
Where time is relative to DST. | |
LocalTimePosixTimezone | config |
Timezone configuration for this time conversion. More... | |
time_t | time |
The time that is being converted. This is always Unix time at UTC. More... | |
LocalTimeValue | localTimeValue |
The local time that corresponds to time. More... | |
time_t | dstStart |
The time that daylight saving starts, Unix time, UTC. More... | |
struct tm | dstStartTimeInfo |
The struct tm that corresponds to dstStart (UTC) | |
time_t | standardStart |
The time that standard time starts, Unix time, UTC. More... | |
struct tm | standardStartTimeInfo |
The struct tm that corresponds to standardStart (UTC) | |
Perform time conversions. This is the main class you will need.
|
strong |
Whether the specified time is DST or not. See also isDST().
void LocalTimeConvert::addSeconds | ( | int | seconds | ) |
Adds a number of seconds to the current object.
seconds |
void LocalTimeConvert::atLocalTime | ( | LocalTimeHMS | hms | ) |
Changes the time of day to the specified hms in local time on the same local day.
hms | A LocalTimeHMS object with hour, minute, and second |
You can use the string constructor like this to set the time to 2:00 PM local time.
It's possible that this will set the time to a time earlier than the object's current time. To only set a time in the future, use nextLocalTime() instead.
Upon completion, all fields are updated appropriately. For example:
void LocalTimeConvert::convert | ( | ) |
Do the time conversion.
You must call this after changing the configuration or the time using withTime() or withCurrentTime()
String LocalTimeConvert::format | ( | const char * | formatSpec | ) |
Works like Time.format()
formatSpec | the format specifies, which can be |
There are many options to strftime described here: https://www.cplusplus.com/reference/ctime/strftime/?kw=strftime
Unlike Time.format(), you can use Z to output the timezone abbreviation, for example "EDT" for the Eastern United States, daylight saving instead of -04:00.
The z formatting matches that of Time.format(), which is wrong. The correct output should be "-400" but the output will be "-04:00" for compatibility.
|
inline |
Get the value of this object as a LocalTimeHMS (hour minute second)
|
inline |
Get the value of this object as a LocalTimeYMD (year month day0)
void LocalTimeConvert::nextDay | ( | LocalTimeHMS | hms = LocalTimeIgnoreHMS() | ) |
Moves the current time to the next day.
hms | If specified, moves to that time of day (local time). If omitted, leaves the current time and only changes the date. |
Upon completion, all fields are updated appropriately. For example:
bool LocalTimeConvert::nextDayOfMonth | ( | int | dayOfMonth, |
LocalTimeHMS | hms = LocalTimeIgnoreHMS() |
||
) |
Moves the date and time (local time) forward to the specified day of month and local time.
dayOfMonth | The day of the month (1 = first day of the month). See also special cases below. |
hms | If specified, moves to that time of day (local time). If omitted, leaves the current time and only changes the date. |
This version will move to the closest forward time. It could be as close as 1 second later, but it will always advance at least once second. It could be as much as 1 month minus 1 second later.
Upon completion, all fields are updated appropriately. For example:
dayOfMonth is normally a a day number (1 = first day of the month). There are also special cases: 0 = the last day of the month, -1 the second to last day of month, ... The number of days in the month are based on the date in local time.
bool LocalTimeConvert::nextDayOfNextMonth | ( | int | dayOfMonth, |
LocalTimeHMS | hms = LocalTimeIgnoreHMS() |
||
) |
Moves the date and time (local time) forward to the specified day of month and local time.
dayOfMonth | The day of the month (1 = first day of the month). See also special cases below. |
hms | If specified, moves to that time of day (local time). If omitted, leaves the current time and only changes the date. |
This version always picks the next month, even if the target day of month hasn't been reached in this month yet. This will always more forward at least a month, and may be as much as two months minus one day.
Upon completion, all fields are updated appropriately. For example:
dayOfMonth is normally a a day number (1 = first day of the month). There are also special cases: 0 = the last day of the month, -1 the second to last day of month, ... The number of days in the month are based on the date in local time.
bool LocalTimeConvert::nextDayOfWeek | ( | int | dayOfWeek, |
LocalTimeHMS | hms = LocalTimeIgnoreHMS() |
||
) |
Moves the current time to the next of the specified day of week.
dayOfWeek | The day of week (0 - 6, 0 = Sunday, 1 = Monday, ..., 6 = Saturday) |
hms | If specified, moves to that time of day (local time). If omitted, leaves the current time and only changes the date. |
Upon completion, all fields are updated appropriately. For example:
bool LocalTimeConvert::nextDayOfWeekOrdinal | ( | int | dayOfWeek, |
int | ordinal, | ||
LocalTimeHMS | hms = LocalTimeIgnoreHMS() |
||
) |
Moves the date and time (local time) forward to the specified day of month and local time.
dayOfWeek | The day of week (0 - 6, 0 = Sunday, 1 = Monday, ..., 6 = Saturday) |
ordinal | 1 = first of that day of week in month, 2 = second, ... |
hms | If specified, moves to that time of day (local time). If omitted, leaves the current time and only changes the date. |
If you specify an ordinal that does not exist (for example, there may not be a 5th ordinal for certain days in certain months), returns false and leaves the date unchanged.
Upon successful completion, all fields are updated appropriately. For example:
void LocalTimeConvert::nextDayOrTimeChange | ( | LocalTimeHMS | hms = LocalTimeIgnoreHMS() | ) |
Moves the current to the next day, or right after the next time change, whichever comes first.
hms | If specified, moves to that time of day (local time). If omitted, leaves the current time and only changes the date. |
Upon completion, all fields are updated appropriately. For example:
This method is used when you want to synchronize an external device clock daily to keep it synchronized, or right after a time change.
Do not pick the local time of the time change as the hms time! For example, in the United State do not select 02:00:00. The reason is that on spring forward, that time doesn't actually exist, because as soon as the clock hits 02:00:00 it jumps forward to 03:00:00 local time. Picking 03:00:00 or really any other time that's not between 02:00:00 and 02:59:59 is fine. During fall back, even though you've picked the time sync time to be 03:00 local time it will sync at the time of the actual time change correctly, which is why this function is different than nextDay().
void LocalTimeConvert::nextLocalTime | ( | LocalTimeHMS | hms | ) |
Sets the time to the nearest hms in local time in the future.
hms | The time of day to set in local time |
Moves the time forward to the next instance of hms in local time. If hms has not occurred yet, it will select the one today, but it will always move the time forward.
There is a weird special case on the beginning of daylight saving (spring forward in the northern hemisphere). If you select a hms between 2:00 AM and 2:59:59 AM local time, this time does not exist on spring forward day because it's the hour that is skipped. In order to preserve the requirement that the time will always be advanced by this call, it will jump forward to the next day when the 2:00 hour occurs next. (The hour may be different in other locations, for example it's 1:00 AM in the UK.)
In the case of fall back, if you specify a hms in the repeated hour (1:00:00 to 1:59:59) the time returned will be the second time this time occurs, in standard time in the US.
Upon completion, all fields are updated appropriately. For example:
void LocalTimeConvert::nextMinuteMultiple | ( | int | increment, |
int | startingModulo = 0 |
||
) |
Moves the current time the next specified multiple of minutes.
increment | Typically something like 5, 15, 20, 30 that 60 is evenly divisible by |
startingModulo | (optional). If present, must be 0 < startingModulo < increment |
Moves the time forward to the next multiple of that number of minutes. For example, if the clock is at :10 past the hour and the multiple is 15, then time will be updated to :15. If the time is equal to an even multple, the next multiple is selected.
Upon completion, all fields are updated appropriately. For example:
bool LocalTimeConvert::nextSchedule | ( | const LocalTimeSchedule & | schedule | ) |
Sets the time to the nearest scheduled time in the future base on the schedule.
schedule |
void LocalTimeConvert::nextTime | ( | LocalTimeHMS | hms | ) |
Moves the current time the next specified local time. This could be today or tomorrow.
hms | Moves to the next occurrence of that time of day (local time) |
Upon completion, all fields are updated appropriately. For example:
void LocalTimeConvert::nextTimeList | ( | std::initializer_list< LocalTimeHMS > | hmsList | ) |
Moves the current time the closest local time om hmsList. This could be today or tomorrow.
hmsList | An initialize list of LocalTimeHMS surrounded by {} |
For example, this sets the time to the nearest noon or midnight local time greater than the time set in this object:
conv.nextTimeList({LocalTimeHMS("00:00"), LocalTimeHMS("12:00")});
void LocalTimeConvert::nextWeekday | ( | LocalTimeHMS | hms = LocalTimeIgnoreHMS() | ) |
Returns the next day that is a weekday (Monday - Friday)
hms | If specified, moves to that time of day (local time). If omitted, leaves the current time and only changes the date. |
Upon completion, all fields are updated appropriately. For example:
void LocalTimeConvert::nextWeekendDay | ( | LocalTimeHMS | hms = LocalTimeIgnoreHMS() | ) |
Returns the next day that is a weekend day (Saturday or Sunday)
hms | If specified, moves to that time of day (local time). If omitted, leaves the current time and only changes the date. |
Upon completion, all fields are updated appropriately. For example:
String LocalTimeConvert::timeStr | ( | ) |
Works like Time.timeStr() to generate a readable string of the local time.
Uses asctime formatting, which looks like "Fri Jan 1 18:45:56 2021". The strings are not localized; they're always in English.
|
inline |
Sets the timezone configuration to use for time conversion.
If you do not use withConfig() the global default set in the LocalTime class is used. If neither are set, the local time is UTC (with no DST).
|
inline |
Use the current time as the time to start with.
This does not start the conversion; you must also call the convert() method after setting all of the settings you want to use.
|
inline |
Sets the UTC time to begin conversion from.
time | The time (UTC) to set. This is the Unix timestamp (seconds since January 1, 1970) UTC such as returned by Time.now(). |
This does not start the conversion; you must also call the convert() method after setting all of the settings you want to use.
For the current time, you can instead use withCurrentTime();
String LocalTimeConvert::zoneName | ( | ) | const |
Returns the abbreviated time zone name for the current time.
For example, for the United States east coast, EST or EDT depending on whether the current time is DST or not. See also isDST().
This string comes from the LocalTimePosixTimezone object.
LocalTimePosixTimezone LocalTimeConvert::config |
Timezone configuration for this time conversion.
If you don't specify this using withConfig then the global setting is retrieved from the LocalTime singleton instance.
time_t LocalTimeConvert::dstStart |
The time that daylight saving starts, Unix time, UTC.
The config specifies the rule (2nd Sunday in March, for example), and the local time that the change occurs. This is the UTC value that corresponds to that rule in the year specified by time.
Note in the southern hemisphere, dstStart is after standardStart.
LocalTimeValue LocalTimeConvert::localTimeValue |
time_t LocalTimeConvert::standardStart |
The time that standard time starts, Unix time, UTC.
The config specifies the rule (1st Sunday in November, for example), and the local time that the change occurs. This is the UTC value that corresponds to that rule in the year specified by time.
Note in the southern hemisphere, dstStart is after standardStart.
time_t LocalTimeConvert::time |
The time that is being converted. This is always Unix time at UTC.
Seconds after January 1, 1970, UTC. Using methods like nextDay() increment this value.