OOFILE | Downloads | Purchasing | Press | Services | Company Information | Soapbox | References | F.A.Q. | HOME

OOFTST 5 - tests dbDate field   

This sample lets you test the date and time field input, looping around entering sample dates and showing the resulting date and/or time parsed from your entry. Simple stream I/O is used to interact with the user.


The include file is:

#include "oofile.h"	// the general oofile library

Here we declare some procudeures to be defined later in the program

void getStringMaybeBlank(char* readInto);
void testDate();
void testTime();
void testDateTime();  

 

int
main()
{
	cout << "OOFILE Validation Suite - Test 5\n"
		 << "Today's date is : " << dbDate::today << endl << endl
		 << "Simple test to try the input routines of the dbDate, dbTime & dbDateTime types\n\n" 
		 << "A number of sample entries are shown.\n\n";   

We run through each of the procedures -> each being a group of tests. Then finish.

	testDate();
	testTime();
	testDateTime();
	cout << endl << "done" << endl;
	return EXIT_SUCCESS;
}       
  

This is the first test procedure and we start by declaring a few unsigned short variables. Year, month and day.

void testDate()
{
	unsigned short year, month, day;  

We then demonstrate the use of a function that changes the default order of the date. an example of this is to change between european standard order of DDMMYYYY to American standard of MMDDYYYY. We do this by calling the method     dbDate::sDefaultDateOrder=dbDate:: < here you specify the order you want>

Testing: sDefaultDateOrder on arbitrary date

	cout << "change the default date order to Month, Day Year" << endl;  
	dbDate::sDefaultDateOrder=dbDate::orderMDY;

Here, we set the base year to be 1900 -> this will help us catch out any millenium-changeover bugs.

Testing: set base year to 1900

	dbDateTime::sBaseYear = 1900;

Now, we create a list of widely varying dates in a variety of formats. We include text months, numbers, numer-and-letter (eg 1st), different delimiters, dates after 2000 and dates with an unspecified year. Each of these is read in as a string.

Testing: Date after year 2000
Testing: Y2K Leap year problem
Testing: Date with unspecified year
Testing: Delimiters of->  '/' , '-' , ' ' , ':' and ",  "
Testing: Absence of delimeter but using month in letters
Testing: Numeric entries for day, month, year
Testing: Text entries for month -> 3-letter, full name, upper/lower/mixed case
Testing: Day entries consisting of number and letter (eg "1st")
Testing: Different ordering of month, day, year

  	char * 	test[] = {	"Jan 15 2001",
				"Feb 29th 2000",
				"Feb, 24, 1923",
				"3/2/67",
				"4-30-1988",
				"MAY12:1971",
				"June17",
				"september 4th",
				"oct 1st 38",
				"11/19"		};
	unsigned short numtests = 10;

Now, we create a standalone variable so that we may acess the current date. We get the curent date by calling dbDate::currentDate().

Testing: dbDate::currentDate()

	oofDate aDate;  // define a standalone date variable
	aDate = dbDate::currentDate();
	cout << "The current date is: " << aDate << endl;

We then test subtraction of a constant from the date, attempting to achieve the date from two weeks ago by subtracting 14.

Testing: subtraction of constant from date.

	oofDate twoWeeksAgo = aDate-14;
	cout << "Two weeks ago was: " << twoWeeksAgo << endl;

We then go through each of the dates given and see if they are able to be processed.

	for (int i = 0; i < numtests; ++i) {
		cout << "Attempting to process the date: " << test[i] << endl;

We must convert each date from a string to a date format by using dbDate::str2ymd(). This function requires the three unsigned short variables we declared at the beginning and these will store the year, month and day of the date.

Testing: conversion of strings to dates using dbDate::str2ymd()

		if (dbDate::str2ymd(test[i],year,month,day))
			cout << " = (YMD) " << year << "/" << month 
               << "/" << day << endl << endl;
		else
			cout << "bad date" << endl;
	}  

This section is commented out, but it allows you to enter dates yourself

This is obviously almost identical to the above, but instead of using dbDate::str2ymd we use dbDate::istream2ymd.

/*
	for (;;) {
		cout << "Enter a date (MDY): " << flush;
		if (dbDate::istream2ymd(cin, year, month, day)) {
			cout << " = (YMD) " << year << "/" << month 
               << "/" << day << endl << endl;

We are also introduced to 2 stream commands: ignore() and clear().

ignore() ignores number of characters (given by INT_MAX) or up to the '\n' delimiter.

			cin.ignore(INT_MAX, '\n');

clear() clears the error state. Otherwise, if the buffer is in a bad state all insertion and extraction operations
will be ignored.

			cin.clear();
		}
		else {
			cout << "bad date" << endl;
			break;
		}
	}
*/
}
  

This next procedure tests the time field type.

void 
testTime()
{

Here we have a test that is being prepared for future use, testing conversion from long to the individual components of the time field, and the testing of this when the specified long is -1

//dbTime::long2dhmsm( -1, day, hour, minute, second, millisecond );
//cout << "dbTime::long2dhmsm test with a long of: -1" << endl;
//cout << "day " << day << "  "<< hour << ':' 
       << minute << '.' << second << ' ' << millisecond << endl;  

For now, however, we are testing in a similar way to our date test. We create a tet list of different types of times. We include 24hr and 12hr times, different specifications of am and pm (including using the word "o'clock"), different delimiters and inclusion/exclusion of some of the smaller units of measurement. All these times are read in as strings for later conversion.

Testing: 24hr and 12 hr times
Testing: specification of am or pm (in upper/lower/mixed case) or lack thereof
Testing: use of the words "o'clock"
Testing: inappropriate use of "AM" when a 24hr time is specified
Testing: lack of smaller units of measurement
Testing: delimiters -> ':' , '.' and combinations of these
Testing: specification of units by word (eg "30 seconds")

	unsigned short day, hour, minute, second, millisecond;
	char * 	test[] = {	"12:30.30am",			// 00:30:30
				"2.20 pm",			// 14:20:00
				"5:21",				// 5:21:00
				"2:30.5 PM",			// 14:30:05
				"23:59:59 AM",			// 23:59:59 (ignore AM)
				"12:00:00 AM",			// 00:00:00
				"5Pm",				// 17:00:00
				"5 o'clock",			// 5:00:00
				"5 pm 30 minutes, 5 seconds",	// 17:30:05
				"12pm",				// 12:00:00
				"1pm"				// 13:00:00
			};
	unsigned short numtests = 11;

Now, we create a standalone variable so that we may acess the current time. We get the curent time by calling dbTime::currentTime().

Testing: dbTime::currentTime()

	oofTime aTime;  // define a standalone date variable
	aTime = dbTime::currentTime();
	cout << "The current time is: " << aTime << endl << endl;

We then go through each of the dates given and see if they are able to be processed.

	for (int i = 0; i < numtests; ++i) {
		cout << "Processing the time: " << test[i] << endl;

We must convert each time from a string to time format by using dbTime::str2dhmsm(). This function requires the five unsigned short variables we declared earlier and these will store the day, hour, minute, second and millisecond of the time given.

Testing: conversion of string to time using dbTime::str2dhmsm()

		if (dbTime::str2dhmsm(test[i],day,hour,minute,second,millisecond)) {
			cout << " = (H:M:S)  " << hour << ':';
			if( minute < 10 ) cout << '0';
			cout << minute << ':';
			if( second < 10 ) cout << '0';
			cout << second << endl << endl;
		}
		else {
			cout << " = bad date" << endl << endl;
		}
	}
}
  

This third procedure tests the combination date and time field.

void 
testDateTime()
{

First, we create a list of widely varying dates and times in a variety of formats. These are made up of a combination of the first two test sets Dates and Times. In addition, we test the entry of year as 'YY and the varieties of delimitation between the date and time. Each of these is read in as a string.

Testing: Delimitation between date and time -> '.' , ' ' , '-'
Testing: Combinations of date and time formats (as tested seperately earlier)
Testing: Entry of year as 'YY (eg "  '65")

	char * 	test[] = {	"Jan 15 2001 12:30.30am",
				"Feb 29th 2000 24:00",
						"Feb, 24, 1923. 2.20 pm",
						"3/2/67 5:21",
						"4-30-1988 2:30.5 PM",
						"MAY12:1971 - 12:00:00 AM",
						"June17 '95 5Pm",
						"september 4th",
						"oct 1st 38 12pm",
						"11/19/11 1pm"		};
	unsigned short numtests = 10;
	unsigned short year, month, day, hour, minute, second;

Now, we create a standalone variable so that we may acess the current date and time. We get the curent date and time by calling dbDateTime::currentTime().

Testing: dbDateTime::currentTime()

	oofDateTime aDateTime;  // define a standalone date variable
	aDateTime = dbDateTime::currentTime();
	cout << "The current time is: " << aDateTime << endl << endl;

We then go through each of the dates given and see if they are able to be processed.

	for (int i = 0; i < numtests; ++i) {
		cout << "Processing the time: " << test[i] << endl;

We must convert each time from a string to date-time format by using dbDateTime::str2ymdhms(). This function requires the five unsigned short variables we declared earlier and these will store the day, hour, minute, second and millisecond of the time given.

Testing: conversion of string to time using dbDateTime::str2ymdhms()

		if (dbDateTime::str2ymdhms(test[i],year,month,day,hour,minute,second)) {
			cout << " = (DD/MM/YY HH:MM:SS)  ";
			  
			cout << day << '/' << month << '/' << year << ' ';  
			cout << hour << ':';
			if( minute < 10 ) cout << '0';
			cout << minute << ':';
			if( second < 10 ) cout << '0';
			cout << second << endl << endl;
		}
		else {
			cout << " = bad date" << endl << endl;
		}
	}

Then we do some extra individual tests

First, we test the conversion from an unsigned long to individual parts of the date-time and back using the functions: dbDateTime::ulong2ymdhsm() and dbDate::ymdhms2ulong()

Testing: dbDateTime::ulong2ymdhsm()
Testing: dbDate::ymdhms2ulong()

	dbDateTime::ulong2ymdhms(dbDateTime::ymdhms2ulong(1970, 2, 10, 0, 0, 0),
                                              year,month,day,hour,minute,second);
	cout << "Testing dbDateTime::ulong2ymdhms parsing...1970, 2, 10, 0, 0, 0" 
            << endl;
	cout << day << '/' << month << '/' << year << ' ';
	cout << hour << ':';
	if( minute < 10 ) cout << '0';
	cout << minute << ':';
	if( second < 10 ) cout << '0';
	cout << second << endl << endl;  

We then test the ability for it to convert the date-time to a stream using dbDateTime::ymdhms2stream()

Testing: dbDateTime::ymdhms2stream()

	cout << "Testing dbDateTime::ymdhms2stream parsing...1975,10,5,13,40,29" 
            << endl;
	dbDateTime::ymdhms2stream("DD/MM/YY hh:MIN:SEC pm",1975,10,5,13,40,29,cout);
	cout << endl<< endl;

We then test its ability to convert to a stream from just a date using dbDate::ymd2stream

Testing: dbDate::ymd2stream

	cout << "Testing dbDate:ymd2Stream parsing...1975,10,5" << endl;
	dbDate::ymd2Stream("YYYY/MM/DD",1975,10,5,cout);
	cout << endl<< endl;
}	 

 

Feature index

(c) Copyright A.D. Software 1994-2000 (All Rights Reserved).
Last Updated: 9th September 2001