And here is the include file for class datetime ... (used in the above program that tests class Rational) ...
// datetime.h
#ifndef dwDATETIME_H
#define dwDATETIME_H
/*
For BEGINNING COMPUTER PROGRAMMING using HLA, Python, C/C++, goto ...
http://developers-heaven.net/forum/index.php/topic,46.0.html
*/
#include <iostream>
#include <iomanip> // setw, setfill
#include <sstream>
#include <string>
#include <ctime>
#include <cctype> // toupper
//using namespace std;
class datetime
{
public:
datetime() // default constructor to fill up private time data 'right now'
{
char c;
time_t now = time( 0 );
std::istringstream iss( ctime( &now ) ); // "Fri Nov 27 16:22:24 2009"
std::string weekdayStr, monthStr;
iss >> weekdayStr >> monthStr >> day
>> hour >> c >> minute >> c >> second // using char 'c' to skip ':'
>> year;
weekday = find_weekday( weekdayStr );
month = find_month( monthStr );
}
std::string get_date()
{
std::ostringstream oss;
oss << year << "-" << std::setfill('0') << std::setw(2) << (month+1) << "-"
<< std::setw(2) << day << std::setfill(' ');
return oss.str();
}
std::string get_time()
{
std::ostringstream oss;
oss << std::setfill('0') << std::setw(2) << hour << ":"
<< std::setw(2) << minute << ":"
<< std::setw(2) << second << std::setfill(' ');
return oss.str();
}
std::string get_datetime() { return get_date() + " " + get_time(); }
std::string get_weekdayname() { return daysTable[ weekday ]; }
std::string get_weekdaynameL() { return daysTable[ weekday+14 ]; }
std::string get_weekdaynumber() { return daysTable[ weekday+7 ]; } //1st,2nd...7th
std::string get_monthname() { return monthsTable[ month ]; }
std::string get_monthnameL() { return monthsTable[ month+24 ]; }
std::string get_monthnumber01() { return monthsTable[ month+12 ]; }
std::string get_monthnumber() { return monthsTable[ month+36 ]; } //1st,2nd...12th
std::string get_monthdaynumber() { return dayOfMonthTable[ day-1 ]; }//1st,2nd...31st
int get_year() { return year; }
int get_month() { return month + 1; }
int get_day() { return day; }
int get_weekday() { return weekday + 1; }
int get_hour() { return hour; }
int get_minute() { return minute; }
int get_second() { return second; }
private:
int year;
int month; // 0..11
int day; // 1..31
int weekday; // 0..6
int hour; // 0..23
int minute; // 0..59
int second; // 0..59
static const std::string monthsTable[];
static const std::string daysTable[];
static const std::string dayOfMonthTable[];
static int find_weekday( std::string wdStr )
{
for( int n = 0; n < 7; ++n )
if( daysTable[n] == wdStr ) return n; // 0..6
// else if error ... handle ... but for now ... out of bounds error
return 7;
}
static int find_month( std::string mStr )
{
for( int n = 0; n < 12; ++n )
if( monthsTable[n] == mStr ) return n; // 0..11
// else if error ... handle ... but for now ... out of bounds error
return 12;
}
};
// define your (language) static const arrays here ... (after def'n above)
const std::string datetime::daysTable[] =
{
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
"1st", "2nd", "3rd", "4th", "5th", "6th", "7th",
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday"
};
const std::string datetime::monthsTable[] =
{
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
"01", "02", "03", "04", "05", "06",
"07", "08", "09", "10", "11", "12",
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December",
"1st", "2nd", "3rd", "4th", "5th", "6th",
"7th", "8th", "9th", "10th", "11th", "12th"
};
const std::string datetime::dayOfMonthTable[] =
{
"1st", "2nd", "3rd", "4th", "5th", "6th",
"7th", "8th", "9th", "10th", "11th", "12th",
"13th", "14th", "15th", "16th", "17th", "18th",
"19th", "20th", "21st", "22nd", "23rd", "24th",
"25th", "26th", "27th", "28th", "29th", "30th", "31st"
};
#ifndef dwTOALLCAPS_H
#define dwTOALLCAPS_H
// utility function used here ...
std::string toAllCaps( std::string inStr ) // make a local copy of string passed in
{
for( int i = inStr.size()-1; i >= 0; --i ) inStr[i] = toupper( inStr[i] );
return inStr;
}
#endif
#endif