A start to ...
// classDateTime.h // 2013-08-18 //
#ifndef CLASSDATETIME_H
#define CLASSDATETIME_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
{
private:
int year;
int month; // 0..11 // get_month() const {return month + 1;}
int day; // 1..31
int weekday; // 0..6 // returns weekday + 1
int hour; // 0..23
int minute; // 0..59
int second; // 0..59
public:
DateTime() // default constructor to fill up private time data 'right now'
{
time_t now = time( 0 );
std::istringstream iss( ctime( &now ) ); // "Fri Nov 27 16:22:24 2009"
std::string weekdayStr, monthStr;
char c; // dummy char to hold ':' char between hour:minute:second //
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() const
{
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() const
{
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_date_time() const { return get_date() + " " + get_time(); }
std::string get_weekdayname() const { return daysTable[ weekday ]; }
std::string get_weekdaynameL() const { return daysTable[ weekday+14 ]; }
std::string get_weekdaynumber() const { return daysTable[ weekday+7 ]; } //1st,2nd...7th
std::string get_monthname() const { return monthsTable[ month ]; }
std::string get_monthnameL() const { return monthsTable[ month+24 ]; }
std::string get_monthnumber01() const { return monthsTable[ month+12 ]; }
std::string get_monthnumber() const { return monthsTable[ month+36 ]; } //1st,2nd...12th
std::string get_monthdaynumber() const{ return dayOfMonthTable[ day-1 ]; }//1st,2nd...31st
int get_year() const { return year; }
int get_month() const { return month + 1; }
int get_day() const { return day; }
int get_weekday() const { return weekday + 1; }
int get_hour() const { return hour; }
int get_minute() const { return minute; }
int get_second() const { return second; }
private:
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 declaration 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"
};
#endif