Xbase DBMS Chapter 15

#

xbDate Methods

Chapter Updated 8/21/03

This chapter lists the xbDate methods and provides an example program to demonstrate usage.
Xbase .DBF files and the xbDate class handle dates in the format of CCYYMMDD (Date8).

### xbDate Method List API

Method | Description
xbDate( const char * Date8 ) xbDate( const xbString & Date8 ) xbDate() | Class constructors. If no date provided, or invalid date provided, date is set to Sysdate().
~xbDate() | Class destructor
xbString & CharDayOf( const char *Date8 ) xbString & CharDayOf( const xbString & Date8 ) xbString & CharDayOf() | Returns Sun - Sat day of week for a given date
xbString & CharMonthOf( const char *Date8 ) xbString & CharMonthOf( const xbString & Date8 ) xbString & CharMonthOf() | Returns Jan - Dec month of year for a given date
int DateIsValid( const char *Date8 ) const int DateIsValid( const xbString & Date8 ) const | Checks a date for validity, returns true (1) if date is valid
int DayOf( int Format, const char *Date8 ) const int DayOf( int Format, const xbString &Date8 ) const int DayOf( int Format ) const | Returns day of WEEK, MONTH or YEAR from a given date where Format is one of XB_FMT_WEEK => Number of day in week 0-6 (sun-Sat) XB_FMT_MONTH => Number of day in month 1-31 XB_FMT_YEAR => Number of day in year 1-366
xbString & Formatdate( const char * Format, const char * Date8 ) xbString & FormatDate( const xbString & Format, const char * Date8 ) xbString & FormatDate( const char * Format, const xbString & Date8 ) xbString & FormatDate( const xbString & Format, const xbString & Date8 ) xbString & FormatDate( const xbString & Format ) xbString & FormatDate( const char * ) | Return a formatted date. Format - A format specifier with the following paramaters: 1) YYDDD - A julian date format 2) YY or YYYY will print a 2 or 4 digit year 3) M,MM,MMM or MMMM M - one digit month if no leading zero MM - two digit month, contains leading zero MMM - Jan through Dec MMMM - January through December 4) D,DD,DDD or DDDD D - one digit dayif no leading zero DD - two digit day, contains leading zero DDD - Sun through Sat (or julian if YYDDD) DDDD - Sunday through Saturday
const xbString & GetDate() const xbString & GetDate() | Return date
const xbString & GetFormattedDate() const xbString & GetFormattedDate() | Returns formatted date. See notes on format paramaters.
int IsLeapYear( const char *Date8 ) const int IsLeapYear( const xbString &Date8 ) const int IsLeapYear() const | Returns true if given date is a leap year
long JulianDays( const char *Date8 ) const long JulianDays( const xbString & Date8 ) const long JulianDays() const | Returns number of days since 1/1/0100 to given date
xbString & JulToDate8( long ) | Converts julian based date to CCYYMMDD format.
xbString & LastdayOfMonth( const char *Date8 ) xbString & LastDayOfMonth( const xbString & Date8 ) xbString & LastDayOfMonth() | Returns the Date8 format of the last day of the month for a given date
int MonthOf( const char *Date8 ) const int MonthOf( const xbString &Date8) const int MonthOf() const | Returns month of a date
int SetDate( const char * Date8 ) int SetDate( const xbString & Date8 ) | Sets the xbDate to Date8. Return 1 on success, 0 on error.
xbString & Sysdate() | This method sets the class date and returns the system date
int YearOf( const char *Date8 ) const int YearOf( const xbString &Date8 ) const int YearOf() const | Returns century and year of a date
xbString &operator+=( int ) | += operator
xbString &operator-=( int ) | -= operator
xbString &operator++( int ) | ++ operator
xbString &operator--( int ) | -- operator
xbString &operator+( int ) | + operator
xbString &operator-( int ) | - operator
long operator-( const xbDate & ) const | - operator
int operator==( const xbDate & ) const | == operator
int operator!=( const xbDate & ) const | != operator
int operator<( const xbDate & ) const | < operator
int operator>( const xbDate & ) const | > operator
int operator<=( const xbDate & ) const | <= operator
int operator>=( const xbDate & ) const | >= operator

## Sample program

The source for this program is in the libtest directory.

        Xbase project source code

        This program tests the xdate routines

        Copyright (C) 1997  Gary A. Kunkel

        This program is free software; you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation; either version 2 of the License, or
        (at your option) any later version.

        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.

        You should have received a copy of the GNU General Public License
        along with this program; if not, write to the Free Software
        Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

        Contact:

          Mail:

            Technology Associates, Inc.
            XBase Project
            1455 Deming Way #11
            Sparks, NV  89434
            USA

         Email:

           xbase@techass.com
           xdb-devel@lists.sourceforge.net
           xdb-users@lists.sourceforge.net

         Website:

           xdb.sourceforge.net

        This program tests and demonstrates usage of the various xbDate methods

        Several of the tests pass either StringDate or Chardate to the method
        being tested - this is done to test the class methods.  In actual usage,
        this may not be necessary due to the instance of the class already
        containing a date value.
    */

    #include "xbase.h"

    int main()
    {
       xbString StringDate( "19601007" );      /* oct 7 1960 */
       char     CharDate[9] = "19611109";      /* nov 9 1961 */

       xbDate d1;                              /* today is default   */
       xbDate d2( StringDate );                /* from string data   */
       xbDate d3( CharDate );                  /* from char data     */
       xbDate d4;                              /* another date class */

       std::cout << "This program tests the XDATE routines" << std::endl;
       std::cout << "Date 1 (Sysdate) is    " << d1.GetDate() << std::endl;
       std::cout << "Date 2 (StringDate) is " << d2.GetDate() << std::endl;
       std::cout << "Date 3 (CharDate) is   " << d3.GetDate() << std::endl;

       std::cout << "This year is  " << d1.YearOf() << std::endl;
       std::cout << "Year of xbString Date is " <<  d2.YearOf( StringDate )
                 << std::endl;
       std::cout << "Year of char Date is "     <<  d3.YearOf( CharDate )
                 << std::endl;

       std::cout << "This Month is " << d1.MonthOf() << std::endl;
       std::cout << "Month of xbString Date is " <<  d2.MonthOf( StringDate )
                 << std::endl;
       std::cout << "Month of char Date is "     <<  d3.MonthOf( CharDate )
                 << std::endl;

       std::cout << "Today is day " << d1.DayOf( XB_FMT_WEEK ) << " of the week"
                 << std::endl;
       std::cout << "StringDate is day " << d2.DayOf( XB_FMT_MONTH, StringDate )
                 << " of the month" << std::endl;
       std::cout << "CharDate is day " << d3.DayOf( XB_FMT_YEAR, CharDate )
                 << " of the year" << std::endl;

       if( d1.IsLeapYear())
          std::cout << "This is a leapyear" << std::endl;
       else
          std::cout << "This is not a leap year." << std::endl;

       if( d2.IsLeapYear( StringDate ))
          std::cout << "StringDate is a leapyear" << std::endl;
       else
          std::cout << "StringDate is not a leap year." << std::endl;

       if( d3.IsLeapYear( CharDate ))
          std::cout << "CharDate is a leapyear" << std::endl;
       else
          std::cout << "CharDate is not a leap year." << std::endl;

       std::cout << "Today is " << d1.Sysdate() << std::endl;

       if( d1.DateIsValid( "19951301" ))
          std::cout << "19951301 is a valid date" << std::endl;
       else
          std::cout << "19951301 is not a valid date" << std::endl;

       if( d1.DateIsValid( "19920229" ))
          std::cout << "19920229 is a valid date" << std::endl;
       else
          std::cout << "19920229 is not a valid date" << std::endl;

       if( d2.DateIsValid( StringDate ))
          std::cout << StringDate << " is a valid date" << std::endl;
       else
          std::cout << StringDate << " is not a valid date" << std::endl;

       std::cout << "Today's Julian date " << d1.JulianDays() << std::endl;
       std::cout << "StringDate Julian date " << d2.JulianDays( StringDate )
                 << std::endl;

       std::cout << "There are "
                 << ( d1.JulianDays( "19951101" ) - d1.JulianDays( "19951001" ))
                 << " days between 10/1/95 and 11/1/95." << std::endl;

       std::cout << "Todays Julian date (Number of days since Jan 1 0100):"
                 << d1.JulianDays() << std::endl;

       d4 = d1;    // set d4 class = to sysdate
       std::cout << "Object d4 initialized to " << d4.GetDate() << std::endl;
       std::cout << "This should be todays date: "
                 << d4.JulToDate8(d4.JulianDays()) << std::endl;
       std::cout << "In 7 days it will be "
                 << d4.JulToDate8(d4.JulianDays() + 7L ) << std::endl;

       std::cout << "Today is " << d1.CharDayOf() << std::endl;
       std::cout << "StringDate day is " << d2.CharDayOf( StringDate )
                 << std::endl;
       std::cout << "This month is " << d1.CharMonthOf() << std::endl;
       std::cout << "StringDate month is " << d2.CharMonthOf() << std::endl;

       /* various format routines using different formats, strings and chars */
       xbString xbStFmt( "MM/DD/YY" );
       std::cout << "Format (YYDDD)     " << d1.FormatDate( "YYDDD" ) << std::endl;
       std::cout << "Format (MM/DD/YY)  " << d1.FormatDate( xbStFmt ) << std::endl;
       xbStFmt = "DD/MM/YY";
       std::cout << "Format (DD/MM/YY)  " << d1.FormatDate(xbStFmt, "19730110")
                 << std::endl;
       std::cout << "Format (MMMM DD,YYYY)    "
                 <<  d1.FormatDate( "MMMM DD,YYYY", d1.GetDate()) << std::endl;
       xbStFmt = "DDDD, MMMM DD YYYY";
       std::cout << "Format (DDDD, MMMM DD YYYY) "
                 <<  d2.FormatDate( xbStFmt, d2.GetDate()) << std::endl;

       std::cout << "Last day this month " << d1.LastDayOfMonth() << std::endl;
       std::cout << "Last day of month for StringDate is "
                 << d2.LastDayOfMonth( d2.GetDate()) << std::endl;

       std::cout << "Overloaded operators test..." << std::endl;

       if( d1 == d2 )
         std::cout << d1.GetDate() << " is equal to " << d2.GetDate()
                   << std::endl;
       else
         std::cout << d1.GetDate() << " is not equal to " << d2.GetDate()
                   << std::endl;

       if( d1 != d3 )
         std::cout << d1.GetDate() << " is not equal to " << d3.GetDate()
                   << std::endl;
       else
         std::cout << d1.GetDate() << " is equal to " << d3.GetDate()
                   << std::endl;

       if( d1 < d2 )
         std::cout << d1.GetDate() << " is less than " << d2.GetDate()
                   << std::endl;
       else
         std::cout << d1.GetDate() << " is not less than " << d2.GetDate()
                   << std::endl;

       if( d1 > d2 )
         std::cout << d1.GetDate() << " is greater than " << d2.GetDate()
                   << std::endl;
       else
         std::cout << d1.GetDate() << " is not greater than " << d2.GetDate()
                   << std::endl;

       if( d1 <= d2 )
         std::cout << d1.GetDate() << " is less than or equal to " << d2.GetDate()
                   << std::endl;
       else
         std::cout << d1.GetDate() << " is not less than or equal to "
                   << d2.GetDate() << std::endl;

       if( d1 >= d2 )
         std::cout << d1.GetDate() << " is greater than or equal to "
                   << d2.GetDate() << std::endl;
       else
         std::cout << d1.GetDate() << " is not greater than or equal to "
                   << d2.GetDate() << std::endl;

       d1.Sysdate();
       d1++;
       std::cout << "Tomorrow is " << d1.GetDate() << std::endl;
       d1-=2;
       std::cout << "Yesterday was " << d1.GetDate() << std::endl;
       std::cout << "There are " << d1 - d2 << " days between " << d1.GetDate()
                 << " and " << d2.GetDate() << std::endl;

       return 0;
    }