LocalTimeRK
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes
LocalTime Class Reference

Global time settings. More...

#include <LocalTimeRK.h>

Public Member Functions

LocalTime & withConfig (LocalTimePosixTimezone config)
 Sets the default global timezone configuration.
 
const LocalTimePosixTimezone & getConfig () const
 Gets the default global timezone configuration.
 
LocalTime & withScheduleLookaheadDays (int value)
 Sets the maximum number of days to look ahead in the schedule for a match (default: 3) More...
 
int getScheduleLookaheadDays () const
 Gets the maximum number of days to look ahead in the schedule for a match. More...
 

Static Public Member Functions

static LocalTime & instance ()
 Get the global singleton instance of this class.
 
static void timeToTm (time_t time, struct tm *pTimeInfo)
 Converts a Unix time (seconds past Jan 1 1970) UTC value to a struct tm. More...
 
static time_t tmToTime (struct tm *pTimeInfo)
 Converts a struct tm to a Unix time (seconds past Jan 1 1970) UTC. More...
 
static String getTmString (struct tm *pTimeInfo)
 Returns a human-readable string version of a struct tm. More...
 
static time_t stringToTime (const char *str, struct tm *pTimeInfo=NULL)
 Converts a string in ISO-8601 format (ignoring the timezone) More...
 
static String timeToString (time_t time, char separator=' ')
 Converts a time to a string in a modified ISO-8601 format with no timezone. More...
 
static int lastDayOfMonth (int year, int month)
 Returns the last day of the month in a given month and year. More...
 
static int dayOfWeekOfMonth (int year, int month, int dayOfWeek, int ordinal)
 Return the nth instance of a day of week in a month and year. More...
 

Protected Member Functions

 LocalTime ()
 This class is a singleton and should not be manually allocated.
 
virtual ~LocalTime ()
 This class is a singleton and should not be manually destructed.
 
 LocalTime (const LocalTime &)=delete
 This class is not copyable.
 
LocalTime & operator= (const LocalTime &)=delete
 This class is not copyable.
 

Protected Attributes

LocalTimePosixTimezone config
 Global default timezone. More...
 
int scheduleLookaheadDays = 100
 Number of days to look forward to see if there are scheduled events. Default: 100.
 

Static Protected Attributes

static LocalTime * _instance
 Singleton instance of this class.
 

Detailed Description

Global time settings.

Member Function Documentation

◆ dayOfWeekOfMonth()

int LocalTime::dayOfWeekOfMonth ( int  year,
int  month,
int  dayOfWeek,
int  ordinal 
)
static

Return the nth instance of a day of week in a month and year.

Parameters
yearThe 4-digit year (2020, for example)
monthMonth 1 - 12 inclusive, 1 = January, 12 = December
dayOfWeek0 = Sunday, 1 = Monday, 2 = Tuesday, ..., 6 = Saturday
ordinal1 = first instance of that day of week in the month, 2 = second, ...
Returns
int The day of the month, or 0 if that ordinal does not exist in the month

◆ getScheduleLookaheadDays()

int LocalTime::getScheduleLookaheadDays ( ) const
inline

Gets the maximum number of days to look ahead in the schedule for a match.

Returns
int

◆ getTmString()

String LocalTime::getTmString ( struct tm *  pTimeInfo)
static

Returns a human-readable string version of a struct tm.

Parameters
pTimeInfoPointer to a struct tm to convert

◆ lastDayOfMonth()

int LocalTime::lastDayOfMonth ( int  year,
int  month 
)
static

Returns the last day of the month in a given month and year.

Parameters
yearThe year (note, actual year like 2021, not the value of tm_year).
monthThe month (1 - 12)

For example, If the month in the current object is January, returns 31. The year is required to handle leap years when the month is February.

◆ stringToTime()

time_t LocalTime::stringToTime ( const char *  str,
struct tm *  pTimeInfo = NULL 
)
static

Converts a string in ISO-8601 format (ignoring the timezone)

Parameters
strString to convert
pTimeInfoPointer to a struct tm to fill in or NULL if you don't need that
Returns
a time_t (Unix time, seconds past January 1, 1970, UTC)

The string must be of the form "YYYY-MM-DDTHH:MM:SS". Any additional characters are ignored, so it's OK if the time includes the timezone, though it will be ignored. Same for milliseconds. The T between the day and hour can be any single non-numeric character, such as a space, instead of a T.

See also timeToString to convert in the other direction.

◆ timeToString()

String LocalTime::timeToString ( time_t  time,
char  separator = ' ' 
)
static

Converts a time to a string in a modified ISO-8601 format with no timezone.

Parameters
timeUnix time (seconds past Jan 1 1970) UTC
separatorthe separator between the day of month and hour, typically T or a space.
Returns
a time_t (Unix time, seconds past January 1, 1970, UTC)

The string will be of the form "YYYY-MM-DDTHH:MM:SS".

See also timeToString to convert in the other direction.

◆ timeToTm()

void LocalTime::timeToTm ( time_t  time,
struct tm *  pTimeInfo 
)
static

Converts a Unix time (seconds past Jan 1 1970) UTC value to a struct tm.

Parameters
timeUnix time (seconds past Jan 1 1970) UTC
pTimeInfoPointer to a struct tm that is filled in with the time broken out into components

The struct tm contains the following members:

  • tm_sec seconds (0-59). Could in theory be up to 61 with leap seconds, but never is
  • tm_min minute (0-59)
  • tm_hour hour (0-23)
  • tm_mday day of month (1-31)
  • tm_mon month (0-11). This is 0-11 not 1-12! Beware!
  • tm_year year since 1900. Note: 2021 is 121, not 2021 or 21! Beware!
  • tm_wday Day of week (Sunday = 0, Monday = 1, Tuesday = 2, ..., Saturday = 6)
  • tm_yday Day of year (0 - 365). Note: zero-based, January 1 = 0
  • tm_isdst Daylight saving flag, always 0 on Particle devices

This basically a wrapper around localtime_r or gmtime_r.

◆ tmToTime()

time_t LocalTime::tmToTime ( struct tm *  pTimeInfo)
static

Converts a struct tm to a Unix time (seconds past Jan 1 1970) UTC.

Parameters
pTimeInfoPointer to a struct tm

Note: tm_wday, tm_yday, and tm_isdst are ignored for calculating the result, however tm_wday and tm_yday are filled in with the correct values based on the date, which is why pTimeInfo is not const.

This basically a wrapper around mktime or timegm.

◆ withScheduleLookaheadDays()

LocalTime & LocalTime::withScheduleLookaheadDays ( int  value)
inline

Sets the maximum number of days to look ahead in the schedule for a match (default: 3)

Parameters
value
Returns
LocalTime&

Field Documentation

◆ config

LocalTimePosixTimezone LocalTime::config
protected

Global default timezone.

The LocalTimeConverter class will use this if a config is not set for that specific converter.


The documentation for this class was generated from the following files: