Dates

Class Date

The class Date stores a year, a month, a day and the time (hour, minutes and seconds). Several operations are allowed on a Date instance, among which:

For further details and other features, the user should refer to the full list of available methods.

Example

dates.cpp:

#include "Talos.hxx"
using namespace Talos;

int main()
{
  Date date(20010330);
  cout << date.GetDate() << endl;

  // Automatic correction of erroneous date.
  // Here: moves to November.
  date.SetDate(2000, 10, 35);
  cout << date.GetDate() << endl;
  cout << "Formatted date: " << date.GetDate("%y-%m-%d") << endl;
  cout << "Time: " << date.GetDate("%h:%i %ss") << endl;

  // "Moving" in time.
  date.AddSeconds(-1);
  cout << "Full (new) date: " << date.GetDate("%y-%m-%d at %h:%i %ss") << endl;

  // Basic computations.
  cout << "Seconds from the 1st January: " << date.GetNumberOfSeconds() << endl;
  cout << "Days from the 1st January: " << date.GetNumberOfDays() << endl;
  cout << "Week day (0: Monday): " << date.GetWeekDay() << endl;
  cout << "Is " << date.GetYear() << " a leap year? ";
  if (date.LeapYear())
    cout << "Yes." << endl;
  else
    cout << "No." << endl;

  // Between two dates.
  Date next_date(date);
  next_date.AddMonths(5);
  cout << "Next date: " << next_date.GetDate("%y-%m-%d at %h:%i %ss") << endl;
  cout << "Number of days from " << date.GetDate("%y-%m-%d") << ": "
       << next_date.GetDaysFrom(date) << endl;

  return 0;
}

Result:

20010330
20001104
Formatted date: 2000-11-04
Time: 00:00 00s
Full (new) date: 2000-11-03 at 23:59 59s
Seconds from the 1st January: 26611199
Days from the 1st January: 307
Week day (0: Monday): 4
Is 2000 a leap year? Yes.
Next date: 2001-04-03 at 23:59 59s
Number of days from 2000-11-03: 151

makefile (launch make dates):

CC	=	g++
INCPATH	=	Talos # Put the path to Talos.
LINK	=	g++

TARGETS	=	read read_config read_configs exceptions dates

all: $(TARGETS)

$(TARGETS): % : %.o
	$(LINK) -o $@ $<

%.o : %.cpp
	$(CC) -I$(INCPATH) -c -o $@ $<

clean:
	rm -f $(TARGETS) $(TARGETS:%=%.o)