Table of Contents ChiLib Library Documentation

4. Date

4.1. Constructors

      Date(int d, int m, int y); 

The Date default constructor initializes the date to day 0 of the Julian day number sequence: Jan 1, 4713 B.C. The second version initializes the day, month, and year to values you provide. Specify years B.C. as negative numbers, that is, use -1 for the year 1 B.C. If the values do not correspond to a legal date, a precondition error is raised.

4.2. Accessors

      int day() const; 
      int month() const; 
      int year() const; 
      Weekday weekday() const; 

      String months = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"; 
      String days = "Mon Tue Wed Thu Fri Sat Sun"; 
      Date d(30, 4, 1777);  
      cout << "Carl Friedrich Gauss was born on " 
         << days.field(d.weekday()) << ", "  
         << months.field(d.month() - 1) << " "  
         << << ", " << d.year(); 
      // Carl Friedrich Gauss was born on Wednesday, Apr 30, 1777 

4.3. Date arithmetic

      Date add_days(long n) const; 
      void advance(long d); 
      long days_between(Date d); 

The add_days operation returns the date that is n days from the current stored date. In order to advance the date by d days , use the mutator operation advance.

      Date d(10, 5, 1994); 
      cout << d.add_days(3); // OH NO! Friday the 13th in 3 days! 
      // let's skip it and set our date to May 20, the next Friday. 

The days_between operation returns the number of days between the instance's date and Date d. If d is earlier than the instance's date, the number returned is positive; if later, the number is negative.

      Date labor_day(1, 9, 1994); 
      Date xmas_day(25, 12, 1994); 
      cout << "There are " << xmas_day.days_between(labor_day)  
        << " days between Labor Day and Christmas."; 
      // days_between returns 115 

4.4. Comparison

      int compare(const Date& b) const 
      static int compare(const Date& a, const Date& b); 
      static int hash(const Date& a); 

The compare operations determine whether another Date is less than, equal to, or greater than that of the instance, returning an integer < 0, zero, or an integer > 0.

The operators == != < <= > >= are overloaded to call compare.

The shared (static) functions are used as comparison functions for sorting. They are functions, not class operations, because several templates require function pointers as arguments.

      Map<Date, String> m(Date::hash, Date::compare);