Trading System API  3.0
Library for Simulating and Deploying Trading and Investment Strategies
tsa::date Class Reference

A date of the Gregorian calendar. More...

#include <TSATime.h>

Inheritance diagram for tsa::date:
tsa::date_time

Public Types

Public Member Functions

 date (const date &other)
 Copy constructor.
 
 date (const char *date_str)
 Constructor. Requires unambiguous date string (e.g. yyyy-mm-dd, yyyy/mm/dd, yyyy.mm.dd)
 
 date (const variant &variant)
 Constructor. May throw exception if variant cannot be converted to a date.
 
 date (uint32_t yyyymmdd=date::undefined)
 Constructor. By default, self is not initialized; defined() will return false. Use the date::now argument to initialize to the current system date. Otherwise initialize to any other date by submitting a valid yyyymmdd argument. More...
 
 date (int year, int month, int day)
 Constructor taking year, month, and day arguments.
 
void assign (const date &, bool _error_on_undefined=true)
 Assigns a given date to self. Throws an exception if given date is not defined (see defined()) and _error_on_undefined flag is true.
 
void clear (void)
 Sets self to an 'undefined' state, where defined() will return false.
 
int day_of_month (void) const
 Returns the day of the month (1-31).
 
week_day day_of_week (void) const
 Returns the day of the week (Sunday = 0, Saturday = 6).
 
int day_of_year (void) const
 Returns the day of the year (1-366).
 
void decr (int num_days)
 Decrements self by num_days.
 
void decr_to_prev_weekday (void)
 Decrements self to previous weekday, skipping sundays or saturdays.
 
bool defined (void) const
 Returns true if self is not 'null'; in other words, the date was defined.
 
bool in_leap_year (void) const
 Returns true if the date's year is a leap year.
 
void incr (int num_days)
 Increments self by num_days.
 
void incr_to_next_weekday (void)
 Increments self to next weekday, skipping sundays and saturdays.
 
bool is_weekend (void) const
 Returns true if current day of week is either Saturday or Sunday.
 
void julian_day (int julian_day)
 Resets the internal julian-day value.
 
int julian_day (void) const
 Returns the julian-day.
 
int month (void) const
 Returns the month (1-12).
 
bool operator!= (const date &other) const
 Returns true if self is not equal to other.
 
void operator+= (const duration &num_days)
 Adds a given number of days to self. The given duration must not have an intraday component.
 
void operator-= (const duration &num_days)
 Subtracts a given number of days from self. The given duration must not have an intraday component.
 
bool operator< (const date &other) const
 Returns true if self is smaller than other.
 
bool operator<= (const date &other) const
 Returns true if self is smaller or equal to other.
 
dateoperator= (const date &other)
 Assignment operator.
 
bool operator== (const date &other) const
 Returns true if self is equal to other.
 
bool operator> (const date &other) const
 Returns true if self is larger than other.
 
bool operator>= (const date &other) const
 Returns true if self is larger or equal to other.
 
void set (uint32_t year, uint32_t month, uint32_t day)
 Sets self to given year, month, day combination.
 
void set_from_iso_string (const std::string &iso_date_string)
 Sets the date from the given ISO date string with format: "yyyy-mm-dd". This member function is much faster then the one declaring a format parameter. More...
 
void set_from_string (const std::string &date_string, const char *format_string="")
 Sets the date from the given date_string interpreted according to given format_string. More...
 
void set_to_now (tsa::local_or_utc_time_select lu)
 Sets self to the operating system's current date.
 
std::string to_string (const char *format) const
 Returns the date as a string formated according to given format. More...
 
std::string to_string (void) const
 Returns the date as an ISO date string (yyyy-mm-dd).
 
int year (void) const
 Returns the year.
 
void yyyymmdd (uint32_t yyyymmdd)
 Sets the date from an integer in yyyymmdd format such as 19990403 (representing April 3 1999).
 
uint32_t yyyymmdd (void) const
 Returns the current date as a number.
 

Static Public Member Functions

static bool is_leap_year (int year)
 Returns true if the given year is a leap year, otherwise returns false.
 
static bool is_valid (int year, int month, int day)
 Returns true if arguments form a valid date. More...
 

Static Public Attributes

static const date gregorian_start = date(1753, 01, 01)
 Gregorian calendar start date (of this implementation - Jan 1 1753).
 
static const date max = date(99999, 12, 31)
 Largest valid date (dec 31 99999AD).
 
static const int max_julian_day = 38245309
 Largest valid julian day.
 
static const int max_yyyymmdd = 999991231
 Largest valid yyyymmdd.
 
static const date min = date(100, 01, 01)
 Smallest valid date ( jan 1 100AD).
 
static const int min_julian_day = 1757585
 Smallest valid julian day.
 
static const int min_yyyymmdd = 1000101
 Smallest valid yyyymmdd.
 
static const uint32_t undefined = (std::numeric_limits<uint32_t>::max)()
 Constructor flag to instruct self to stay undefined (see defined()).
 

Friends

date operator+ (const date &date, const duration &num_days)
 Adds num_days to self. The given duration must not have an intraday component. More...
 
date operator- (const date &Left, const duration &num_days)
 Subtracts num_days from self. The given duration must not have an intraday component.
 

Related Functions

(Note that these are not member functions.)

std::ostream & operator<< (std::ostream &stream, const date &date)
 Writes date to stream in "yyyy-mm-dd" format. If the date is undefined, output is "[undefined-date]". More...
 

Detailed Description

A date of the Gregorian calendar.

Date manipulation algorithms are accurate from the year 1753 onwards in accordance with the Gregorian Calendar. The class represents a continuous day sequence from the year 100 AD onwards. A date instance always represents a valid date. Any errors will result in an exception.

Constructor & Destructor Documentation

tsa::date::date ( uint32_t  yyyymmdd = date::undefined)
explicit

Constructor. By default, self is not initialized; defined() will return false. Use the date::now argument to initialize to the current system date. Otherwise initialize to any other date by submitting a valid yyyymmdd argument.

Parameters
yyyymmddThe yyyymmdd date.

Member Function Documentation

bool tsa::date::is_valid ( int  year,
int  month,
int  day 
)
static

Returns true if arguments form a valid date.

Parameters
yearThe year.
monthThe month.
dayThe day.
Returns
true if valid, false if not.
void tsa::date::set_from_iso_string ( const std::string iso_date_string)

Sets the date from the given ISO date string with format: "yyyy-mm-dd". This member function is much faster then the one declaring a format parameter.

Parameters
iso_date_stringThe ISO date string.
void tsa::date::set_from_string ( const std::string date_string,
const char *  format_string = "" 
)

Sets the date from the given date_string interpreted according to given format_string.

The format string essentially provides the YMD sequence, as well as the field separator. Valid field separators are '.', '-', '/'.

If the month format is set to 'mmm', then the function will required the month to be a 3 letter short month name, such as 'Jan'.

Examples for valid date formats are:

  • yyyy-mm-dd
  • yyyy.mmm.dd
  • dd/mmm/yyyy
  • d/m/y
  • y-m-d.
    Parameters
    date_stringThe date string.
    format_stringThe format string.
std::string tsa::date::to_string ( const char *  format) const

Returns the date as a string formated according to given format.

Format strings are case sensitive and must be in lower case! Valid separators in format string are '.' , '/' and '-' .

Examples of format strings and the outputs they produce (for Nov 4 1999) are:

  • "yyyymmdd" produces 19991104
  • "ddmmyyyy" produces 04111999
  • "y/m/d" produces 1999/11/04
  • "m.d.y" produces 11.04.1999
  • "d-m-y" produces 4-11-1999
  • "y/mmm/d" produces 1999/Nov/04
  • "mmm.d.y" produces Nov.04.1999
  • "d-mmm-y" produces 04-Nov-1999.
    Parameters
    formatDescribes the format to use.
    Returns
    The date as a std::string.

Friends And Related Function Documentation

date operator+ ( const date date,
const duration num_days 
)
friend

Adds num_days to self. The given duration must not have an intraday component.

Parameters
dateThe date to which num_days are added.
num_daysThe number of days to add.
Returns
date