Class Datemath

Index Home MAE > MAE Architecture > MAE Data Types > Class Index > Class Datemath

Summary
Public Properties
Public Methods
Datemath(timestamp)
Datemath(params)
Datemath(timestamp, params)
void clear()
bool isValid() const
bool isInvalid() const
inline time_t getTimestamp() const
int getYear() const
int getMonth() const
int getDay() const
int getDayOfWeek() const
int getHour() const
int getMinute() const
int getSecond() const
static inline time_t now()
static inline Datemath zerotime()
inline time_t setTimestamp(timestamp, defaultToNow)
string display(params, format)
time_t calcItem(param)
time_t calc(params)
string format(format) const
string printFormattedDate(fmt) const
inline Datemath operator+(n) const
inline Datemath operator+=(n)
inline Datemath operator+(params) const
inline Datemath operator+=(params)
inline static string display(starttime, params, format)
inline static time_t calcItem(starttime, param)
inline static time_t calc(starttime, params)
inline static string format(starttime, fmt)
inline static string printFormattedDate(t, fmt)
inline string toString() const
Private Properties
Private Methods
time_t backupToMonth(t, month)

#include <Datemath.h >

Summary

A class for manipulating time to calculate new dates/times.

Public Properties

static const string months

The first three letters of each month, e.g. months[0]="Jan", etc.

static const string dow

The first three letters of the day of the week, e.g. dow[0]="Sun", etc.


Public Methods

Datemath(timestamp)

time_t timestamp

an Unix epoch timestamp. Not requires. Default: time(0)

Default value: 0


Constructor. Initializes to provided timestamp. If none provided, then now.

Datemath(params)

const string& params

Space separated list of terms. See calcItem().


Constructor. Takes a list of terms, which are used to calculate the desired time.

Datemath(timestamp, params)

time_t timestamp

an Unix epoch timestamp. Not requires. Default: time(0)

const string& params

Space separated list of terms. See calcItem().


Constructor.  Provided a start time and terms to modify it.

void clear()

Reset to beginning of time.

bool isValid() const

Determines if the current time value is valid. The beginning of time is considered invalid.

Return value: True if current time is valid.

bool isInvalid() const

Determines if the current time value is invalid. The beginning of time is considered invalid.

Return value: True if current time is 0, the beginning of time.

inline time_t getTimestamp() const

Get the calculated time as Unix epoch time.

Return value: the current calculated time as Unix epoch time.

int getYear() const

Return the caclulated time's year.

Return value: the year, e.g. 1970+

int getMonth() const

Return the caclulated time's month.

Return value: the month, e.g. 1-12

int getDay() const

Return the caclulated time's day.

Return value: the day, e.g. 1-31

int getDayOfWeek() const

Return the caclulated time's day of week.

Return value: the day of week, 0=Sun, 1=Mon, etc. 6=Sat

int getHour() const

Return the caclulated time's hour.

Return value: the hour, e.g. 0-23

int getMinute() const

Return the caclulated time's minute.

Return value: the minute, e.g. 0-59

int getSecond() const

Return the caclulated time's second.

Return value: the second, e.g. 0-59

static inline time_t now()

Get the current Unix epoch time for right now.

Return value: Unix epoch time for now

static inline Datemath zerotime()

Get the beginning of Unix epoch time for zero time (epoch time=0, Jan 1, 1970).

Return value: Unix epoch time for zero time

inline time_t setTimestamp(timestamp, defaultToNow)

time_t timestamp

Unix epoch time

bool defaultToNow

if timestamp is 0, then use 0=beginning of time (if false) or to now (if true)

Default value: false


Set the current time in Datemath.

Return value: Unix epoch time of new timestamp

string display(params, format)

const string& params

Space separated list of terms. See calcItem().

const string& format

Instructions on how to convert the time to text, like date(1) command. See format().


Modify the current time by the parameters, then convert time to the format provided.

Return value: the current Datemat time formatted into a string

time_t calcItem(param)

string param

A Datemath modifier (see above)


Modifify the calculated time by one Datemath modifier parameter:

noon - set the time of day to 12:00:00pm

midnight - set the time of day to 00:00:00am

firstday - set the day of the month to the first day in the month, e.g. 1

lastday - set the day of the month to the last day in the

Sun - set the date to the previous Sunday

Mon - set the date to the previous Sunday

Tue - set the date to the previous Sunday

Wed - set the date to the previous Sunday

Thu - set the date to the previous Sunday

Fi - set the date to the previous Sunday

Sat - set the date to the previous Sunday

now - set the date and time to the current moment, e.g. time(0)

HH:MM - set the time to the time specified for the current date

HH:MM:SS - set the time to the time specified for the current date

MM/DD/YY - set the current date to the date specified (if no century, then +2000)

YY-MM-DD - set the current date to the date specified (if no century, then +2000)

+n, -n - Modify the current time by adding ro subtracting <n > seconds

+ns, -ns - Modify the current time by adding ro subtracting <n > seconds

+nm, -nm - Modify the current time by adding ro subtracting <n > minutess

+nh, -nh - Modify the current time by adding ro subtracting <n > hours

+nd, -nd - Modify the current time by adding ro subtracting <n > days

+nw, -nw - Modify the current time by adding ro subtracting <n > weeks

+nM, -nM - Modify the current time by adding ro subtracting <n > months

+nY, -nY - Modify the current time by adding ro subtracting <n > years (Y or y allowed)

+nQ, -nQ - Modify the current time by adding ro subtracting <n > quarters

Jan - change the current month to January (year not changed)

Feb - change the current month to February (year not changed)

Mar - change the current month to March (year not changed)

Apr - change the current month to April (year not changed)

May - change the current month to May (year not changed)

Jun - change the current month to June (year not changed)

Jul - change the current month to July (year not changed)

Aug - change the current month to August (year not changed)

Sep - change the current month to September (year not changed)

Oct - change the current month to October (year not changed)

Nov - change the current month to November (year not changed)

Dec - change the current month to December (year not changed)

Return value: Unix epoch time of new timestamp

time_t calc(params)

string params

A space sparated list of Datemath modifier terms.  See calcItem().


Modify the calculated time by the provides list of modififer paramters.

Return value: Unix epoch time of new timestamp

string format(format) const

const string& format

A text string containing time placeholders (see above)


From the current time in Datemath, output the date and time in the format provided.

Just like date(1), the format may contain plain text and time placeholders. The

placeholders will be replaced with the appropriate text for the time. Placeholders are:

%s - seconds since the beginning of time (epoch time)

%D - date at MM/DD/YY

%d - day of month, e.g. 01-31

%e - day of month, e.g.  1-31

%m - month of year, e.g. 01-12

%y - year of century, e.g. 00-99

%Y - year with century, e.g. 2023

%b - name of month, e.g. Jan-Dec

%T - time of day, e.g. HH:MM:SS

%H - hour of day, e.g. 00-23

%I - hour of day, e.g. 1-12

%M - minute of hour, e.g. 00-59

%S - seconds within the current minute, e.g. 00-59

%a - the weekday name, e.g. Sun-Sat

%w - the weeday number, e.g. 0-6

%Z - the local timezone

Return value: a string with time per the format provided

string printFormattedDate(fmt) const

const string& fmt

A text string containing time placeholders. See format().


Same as format(): From the current time in Datemath, output the date and time in the format provided.

Return value: a string with time per the format provided

inline Datemath operator+(n) const

long n

the number of seconds to add


Add seconds to the time in Datemath, returns a new Datemath value.

Return value: a new Datemath value, modified

inline Datemath operator+=(n)

long n

the number of seconds to add


Add seconds to the current time in Datemath, returns the new/modified Datemath value.

Return value: the modified Datemath value

inline Datemath operator+(params) const

const string& params

a string with Datemath modifiers. See calcItem().


Modifies the time in Datemath by the parameters given, returns a new Datemath value.

Return value: a new Datemath value, modified

inline Datemath operator+=(params)

const string& params

a string with Datemath modifiers separated by spaces. See calcItem().


Modifies the time in Datemath by the parameters given, returns the new/modified Datemath value.

Return value: the modified Datemath value

inline static string display(starttime, params, format)

time_t starttime

Unix epoch time

const string& params

a string with Datemath modifies separated by spaces. See calcItem().

const string& format

A text string containing time placeholders. See format().


Given a time, modifiers, and format, output the newly calculated time in the format provided.

Return value: a formated time string

inline static time_t calcItem(starttime, param)

time_t starttime

Unix epoch time

const string& param

a Datemath modifier. See other calcItem().


From the current time provided, calculate a new time based on modifier provided.

Return value: the modified Unix epoch time

inline static time_t calc(starttime, params)

time_t starttime

Unix epoch time

const string& params

a string with Datemath modifiers separated by spaces. See calcItem().


From the current time provided, calculate a new time based on the list of modifiers provided.

Return value: the modified Unix epoch time

inline static string format(starttime, fmt)

time_t starttime

Unix epoch time

const string& fmt

A text string containing time placeholders. See format().


Given a time and format, output the time in the format provided.

Return value: a formated time string

inline static string printFormattedDate(t, fmt)

time_t t

Unix epoch time

const string& fmt

A text string containing time placeholders. See format().


Same as format(). Given a time and format, output the time in the format provided.

Return value: a formated time string

inline string toString() const

Convert the current time to epoch time as a string.

Return value: epoch time as a string (not as time_t)

Private Properties

time_t t

time last processed


Private Methods

time_t backupToMonth(t, month)

time_t t

starting Unix epoch time

int month

the number of months to turn back


Go back in time the specified number of months

Return value: epoch time