summaryrefslogtreecommitdiff
path: root/html/xbc15.htm
blob: de01ec4032715c60b34f2675e1a8578ac3f3f13c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<!DOCTYPE HTML PUBLIC>
<html>
<title>Xbase DBMS Chapter 15</title>
<body bgcolor=#FFFFFF>
<h1><p align="center">xbDate Methods</p></h1>
<p align="center">Chapter Updated 8/21/03</p><hr><br>
 
This chapter lists the xbDate methods and provides an example program to demonstrate usage.<br>
Xbase .DBF files and the xbDate class handle dates in the format of CCYYMMDD (Date8).<br><br><br> 

<center>
<table border>
<caption align=top><h3>xbDate Method List API</h3></caption>

<tr><th align=left>Method<td>Description

<tr><th align=left>xbDate( const char * Date8 )<br>xbDate( const xbString & Date8 )<br>xbDate()
  <td>Class constructors. If no date provided, or invalid date provided, date is set to Sysdate().

<tr><th align=left>~xbDate()<td>Class destructor

<tr><th align=left>xbString & CharDayOf( const char *Date8 )<br>
  xbString & CharDayOf( const xbString & Date8 )<br>xbString & CharDayOf()
  <td>Returns <em>Sun - Sat</em> day of week for a given date

<tr><th align=left>xbString & CharMonthOf( const char *Date8 )<br>xbString & CharMonthOf( const xbString & Date8 )<br>
  xbString & CharMonthOf()<td>Returns <em>Jan - Dec</em> month of year for a given date

<tr><th align=left>int DateIsValid( const char *Date8 ) const<br>int DateIsValid( const xbString & Date8 ) const
  <td>Checks a date for validity, returns true (1) if date is valid

<tr><th align=left>int DayOf( int Format, const char *Date8 ) const<br>int DayOf( int Format, const xbString &Date8 ) const<br>
  int DayOf( int Format ) const<td>Returns day of WEEK, MONTH or YEAR from a given date where <em>Format</em> is one of<br>
  <em>XB_FMT_WEEK</em> => Number of day in week 0-6 (sun-Sat)<br>
  <em>XB_FMT_MONTH</em> => Number of day in month 1-31<br>
  <em>XB_FMT_YEAR</em> => Number of day in year 1-366

<tr><th align=left>xbString & Formatdate( const char * Format, const char * Date8 )<br>
  xbString & FormatDate( const xbString & Format, const char * Date8 )<br>
  xbString & FormatDate( const char * Format, const xbString & Date8 )<br>
  xbString & FormatDate( const xbString & Format, const xbString & Date8 )<br>
  xbString & FormatDate( const xbString & Format )<br>
  xbString & FormatDate( const char * )<br>
  
  <td>Return a formatted date.<br>

      Format - A format specifier with the following paramaters:<br>

        1) YYDDD    -  A julian date format<br>

        2) YY or YYYY will print a 2 or 4 digit year<br>

        3) M,MM,MMM or MMMM<br>

           M    - one digit month if no leading zero<br>
           MM   - two digit month, contains leading zero<br> 
           MMM  - Jan through Dec<br>
           MMMM - January through December<br>

        4) D,DD,DDD or DDDD<br>
   
           D    - one digit dayif no leading zero<br>
           DD   - two digit day, contains leading zero <br>
           DDD  - Sun through Sat (or julian if YYDDD)<br>
           DDDD - Sunday through Saturday<br>


<tr><th align=left>const xbString & GetDate() const<br>xbString & GetDate()<td>Return date

<tr><th align=left>const xbString & GetFormattedDate() const<br>xbString & GetFormattedDate()<td>
  Returns formatted date.  See notes on format paramaters.

<tr><th align=left>int IsLeapYear( const char *Date8 ) const<br>int IsLeapYear( const xbString &Date8 ) const<br>
  int IsLeapYear() const<td>Returns true if given date is a leap year

<tr><th align=left>long JulianDays( const char *Date8 ) const<br>long JulianDays( const xbString & Date8 ) const<br>
  long JulianDays() const<td>Returns number of days since 1/1/0100 to given date

<tr><th align=left>xbString & JulToDate8( long )<td>Converts julian based date to CCYYMMDD format.

<tr><th align=left>xbString & LastdayOfMonth( const char *Date8 )<br>xbString & LastDayOfMonth( const xbString & Date8 )<br>
  xbString & LastDayOfMonth()<td>Returns the Date8 format of the last day of the month for a given date

<tr><th align=left>int MonthOf( const char *Date8 ) const<br>int MonthOf( const xbString &Date8) const<br>
int MonthOf() const<td>Returns month of a date

<tr><th align=left>int SetDate( const char * Date8 )<br>int SetDate( const xbString & Date8 )<td>
  Sets the xbDate to <em>Date8</em>. Return 1 on success, 0 on error.
  
  
<tr><th align=left>xbString & Sysdate()<td>This method sets the class date and returns the system date

<tr><th align=left>int YearOf( const char *Date8 ) const<br>int YearOf( const xbString &Date8 ) const<br>
int YearOf() const<td>Returns century and year of a date

<tr><th align=left>xbString &operator+=( int )<td>+= operator
<tr><th align=left>xbString &operator-=( int )<td>-= operator
<tr><th align=left>xbString &operator++( int )<td>++ operator
<tr><th align=left>xbString &operator--( int )<td>-- operator
<tr><th align=left>xbString &operator+( int )<td>+ operator
<tr><th align=left>xbString &operator-( int )<td>- operator
<tr><th align=left>long operator-( const xbDate & ) const<td>- operator
<tr><th align=left>int operator==( const xbDate & ) const<td>== operator
<tr><th align=left>int operator!=( const xbDate & ) const<td>!= operator
<tr><th align=left>int operator<( const xbDate & ) const<td>< operator
<tr><th align=left>int operator>( const xbDate & ) const<td>> operator
<tr><th align=left>int operator<=( const xbDate & ) const<td><= operator
<tr><th align=left>int operator>=( const xbDate & ) const<td>>= operator

</table></center>
<br><br><br>

<h2>Sample program</h2><br>
The source for this program is in the libtest directory. <br><br>

<xmp>
    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;
}
    

</xmp>
<hr>
<p><img src="xbase.jpg"><br><hr>
</BODY>
</HTML>