summaryrefslogtreecommitdiff
path: root/src/tests/xb_test_date.cpp
blob: 1d9b5232268e691ba234916bf2ce928e04f56a11 (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
/* xb_test_date.cpp

XBase64 Software Library

Copyright (c) 1997,2003,2014,2022 Gary A Kunkel

The xb64 software library is covered under the terms of the GPL Version 3, 2007 license.

Email Contact:

    XDB-devel@lists.sourceforge.net
    XDB-users@lists.sourceforge.net

*/

// This program tests the date class xbDate

// usage:   xb_test_date QUITE|NORMAL|VERBOSE


#include "xbase.h"

using namespace xb;

#include "tstfuncs.cpp"


int main( int argCnt, char **av )
{
  int iRc  = 0;
  xbInt16 po = 1;      /* print option */
                       /* 0 - QUIET    */
                       /* 1 - NORMAL   */
                       /* 2 - VERBOSE  */

  if( argCnt > 1 ) {
    if( av[1][0] == 'Q' )
      po = 0;
    else if( av[1][0] == 'V' )
      po = 2;
  }

#ifdef XB_LOGGING_SUPPORT
  xbXBase x;
  x.EnableMsgLogging();
  if( po ){
    std::cout << "Logfile is [" << x.GetLogFqFileName().Str() << "]" << std::endl;
  }
  xbString sMsg;
  sMsg.Sprintf( "Program [%s] initializing...", av[0] );
  x.WriteLogMessage( sMsg );
#endif
  InitTime();

  xbDate d1;
#ifdef XB_DEBUG_SUPPORT
  if( po > 0 )
    d1.DumpDateTables();
#endif

  xbDate d2( "19890209" );
  xbString s3 = "20120709";
  xbDate d3( s3 );
  xbString s4;
  xbString sFmt = "";
  xbString sOutDate; 
  xbString s;

  iRc += TestMethod( po, "FormatDate( '', sOutDate ) (sys default format)", d3.FormatDate( sFmt, sOutDate ), 0 );
  iRc += TestMethod( po, "FormatDate( '', sOutDate ) (sys default format)", sOutDate, "07/09/12", 8 );
  iRc += TestMethod( po, "CenturyOf()" , d3.CenturyOf(), 20 );
  iRc += TestMethod( po, "YearOf()", d3.YearOf(), 2012 );
  iRc += TestMethod( po, "MonthOf()", d3.MonthOf(), 7 );

  iRc += TestMethod( po, "DayOf( XB_FMT_DAY )", d3.DayOf( XB_FMT_WEEK ), 2 );
  iRc += TestMethod( po, "DayOf( XB_FMT_MONTH )", d3.DayOf( XB_FMT_MONTH ), 9 );
  iRc += TestMethod( po, "DayOf( XB_FMT_YEAR )", d3.DayOf( XB_FMT_YEAR ), 191 );

  iRc += TestMethod( po, "DateIsValid( '12345678' )", d3.DateIsValid( "12345678" ), 0 );
  iRc += TestMethod( po, "d3.DateIsValid( '20120708' )", d3.DateIsValid( "20120708" ), 1 );
  iRc += TestMethod( po, "d3.IsLeapYear()", d3.IsLeapYear(), 1 );
  iRc += TestMethod( po, "d2.IsLeapYear()", d2.IsLeapYear(), 0 );

  d1.Set( "20010102" );
  iRc += TestMethod( po, "d1.Set('20010102')", d1.Str(), "20010102", 8 );
  iRc += TestMethod( po, "d1.CalcRollingCenturyForYear(10)", d1.CalcRollingCenturyForYear( 10 ), 20 );

  iRc += TestMethod( po, "d1.JulianDays()", d1.JulianDays(), 2451912 );


  iRc += TestMethod( po, "d1.Set( '20140709' )", d1.Set( "20140709" ) , 0 );
  d1.CharDayOf( s );
  iRc += TestMethod( po, "d1.CharDayOf(s)", s, "Wednesday", 9 );

  d1.CharMonthOf( s );
  iRc += TestMethod( po, "d1.CharMonthOf()", s, "July", 4 );

  iRc += TestMethod( po, "d1.JulToDate8( 2451913 )", d1.JulToDate8( 2451912 ), 0 );
  iRc += TestMethod( po, "d1.JulToDate8( 2451913 )", d1.Str(), "20010102", 8 );
  iRc += TestMethod( po, "d1.LastDayOfMonth()", d1.LastDayOfMonth(), 0 );
  iRc += TestMethod( po, "d1.LastDayOfMonth()", d1.Str(), "20010131", 8 );

  iRc += TestMethod( po, "d1.CTOD( '12/01/02' )", d1.CTOD( "12/01/02" ), 0 );
  iRc += TestMethod( po, "d1.CTOD( '12/01/02' )", d1.Str(), "20021201", 8 );

  xbString s1, s2;
  s1 = "MM-DD-YY";
  d1 = "20130402";
  iRc+= TestMethod( po, "d1.FormatDate( s1, sOutDate )", d1.FormatDate( s1, sOutDate ), 0 );
  iRc+= TestMethod( po, "d1.FormatDate( s1, sOutDate )", sOutDate, "04-02-13", 8 );

  d1 = "20120203";
  iRc += TestMethod( po, "d1=20120203", d1.Str(), "20120203", 8 );

  s1 = "20130405";
  d1 = s1;
  iRc += TestMethod( po, "d1=s1", d1.Str(), "20130405", 8 );

  d2 = d1;
  iRc += TestMethod( po, "d2=d1", d2.Str(), "20130405", 8 );

  d2+=5;
  iRc += TestMethod( po, "d2+=5", d2.Str(), "20130410", 8 );

  d2-=7;
  iRc += TestMethod( po, "d2-=5", d2.Str(), "20130403", 8 );

  d2++;
  iRc += TestMethod( po, "d2++", d2.Str(), "20130404", 8 );

  d2--;
  iRc+= TestMethod( po, "d2--", d2.Str(), "20130403", 8 );
  iRc+= TestMethod( po, "d2-d1", d1-d2, 2 );
  iRc+= TestMethod( po, "d1+2", d1+2, "20130407", 8 );
  iRc+= TestMethod( po, "d1-5", d1-5, "20130402", 8 );

//  d1.Dump( "d1" );
//  d2.Dump( "d2" );

  iRc+= TestMethod( po, "d1==d2", d1==d2, 0 );
  d2 = d1;
  iRc+= TestMethod( po, "d1==d2", d1==d2, 1 );
  iRc+= TestMethod( po, "d1>=d2", d1>=d2, 1 );
  iRc+= TestMethod( po, "d1<=d2", d1<=d2, 1 );
  iRc+= TestMethod( po, "d1!=d2", d1!=d2, 0 );
  d2++;
  iRc+= TestMethod( po, "d1!=d2", d1!=d2, 1 );

  iRc+= TestMethod( po, "d1>d2", d1>d2, 0 );
  iRc+= TestMethod( po, "d1>=d2", d1>=d2, 0 );
  iRc+= TestMethod( po, "d1<d2", d1<d2, 1 );
  iRc+= TestMethod( po, "d1<=d2", d1<=d2, 1 );

  d2-=2;
  iRc+= TestMethod( po, "d1>d2", d1>d2, 1 );
  iRc+= TestMethod( po, "d1>=d2", d1>=d2, 1 );
  iRc+= TestMethod( po, "d1<d2", d1<d2, 0 );
  iRc+= TestMethod( po, "d1<=d2", d1<=d2, 0 );
  iRc+= TestMethod( po, "d1-d2", d1-d2, 1 );

/*
  xbDate d4( "20171015" );
  std::cout << "day of [" << d4.Str() << "][" << d4.DayOf( XB_FMT_WEEK ) << "]\n";
  d4++;
  std::cout << "day of [" << d4.Str() << "][" << d4.DayOf( XB_FMT_WEEK ) << "]\n";
  d4++;
  std::cout << "day of [" << d4.Str() << "][" << d4.DayOf( XB_FMT_WEEK ) << "]\n";
  d4++;
  std::cout << "day of [" << d4.Str() << "][" << d4.DayOf( XB_FMT_WEEK ) << "]\n";
  d4++;
  std::cout << "day of [" << d4.Str() << "][" << d4.DayOf( XB_FMT_WEEK ) << "]\n";
  d4++;
  std::cout << "day of [" << d4.Str() << "][" << d4.DayOf( XB_FMT_WEEK ) << "]\n";
  d4++;
  std::cout << "day of [" << d4.Str() << "][" << d4.DayOf( XB_FMT_WEEK ) << "]\n";
  d4++;
  std::cout << "day of [" << d4.Str() << "][" << d4.DayOf( XB_FMT_WEEK ) << "]\n";
*/

  if( po > 0 || iRc < 0 )
    fprintf( stdout, "Total Errors = %d\n", iRc * -1 );

#ifdef XB_LOGGING_SUPPORT
  sMsg.Sprintf( "Program [%s] terminating with [%d] errors...", av[0], iRc * -1 );
  x.WriteLogMessage( sMsg );
#endif

  return iRc;
}