Click here to Skip to main content
11,583,555 members (63,693 online)
Click here to Skip to main content
Articles » Database » Database » General » Downloads
Add your own
alternative version

DarkSide SQL Mini Version 1, The embedded database

, 23 Mar 2006 BSD 122.3K 2.7K 56
An embedded database library in C++.
// mydate.cpp
// defines a date type
// Date: 28 Aug 2003 12:04 AM
// Last Modified: 28 Aug 2003 9:43 AM
// Author: Vijay Mathew Pandyalakal

/* Copyright 2000 - 2005 Vijay Mathew Pandyalakal.  All rights reserved.
 *
 * This software may be used or modified for any purpose, personal or
 * commercial.  Open Source redistributions are permitted.  
 *
 * Redistributions qualify as "Open Source" under  one of the following terms:
 *   
 *    Redistributions are made at no charge beyond the reasonable cost of
 *    materials and delivery.
 *
 *    Redistributions are accompanied by a copy of the Source Code or by an
 *    irrevocable offer to provide a copy of the Source Code for up to three
 *    years at the cost of materials and delivery.  Such redistributions
 *    must allow further use, modification, and redistribution of the Source
 *    Code under substantially the same terms as this license.
 *
 * Redistributions of source code must retain the copyright notices as they
 * appear in each source code file, these license terms, and the
 * disclaimer/limitation of liability set forth as paragraph 6 below.
 *
 * Redistributions in binary form must reproduce this Copyright Notice,
 * these license terms, and the disclaimer/limitation of liability set
 * forth as paragraph 6 below, in the documentation and/or other materials
 * provided with the distribution.
 *
 * The Software is provided on an "AS IS" basis.  No warranty is
 * provided that the Software is free of defects, or fit for a
 * particular purpose.  
 *
 * Limitation of Liability. The Author shall not be liable
 * for any damages suffered by the Licensee or any third party resulting
 * from use of the Software.
 */


#include <string>
#include <ctime>
#include <vector>
using namespace std;

#include "mydate.h"
using namespace openutils;

MyDate::MyDate() {
    time_t now;
    time(&now); 
    struct tm *l_time;			 
    l_time = localtime(&now);	
    this->m_nYear = l_time->tm_year + 1900;
    this->m_nMonth = l_time->tm_mon + 1;
    this->m_nDay = l_time->tm_mday;
    m_nOldYear = m_nYear;
    m_nOldMonth = m_nMonth;
    m_nOldDay = m_nDay;
}

MyDate::MyDate(const char* strd) {
	if(strcmpi(strd,"null") == 0) return;
    MyDate dt;
    char* date = new char[strlen(strd) + 1];
    strcpy(date,strd);
    m_nOldYear = dt.m_nYear;
    m_nOldMonth = dt.m_nMonth;
    m_nOldDay = dt.m_nDay;	
    int len = strlen(date);
    if(len < 7 || len > 10) {
		delete[] date;
		throw MyDateException("Invalid date format. Expected YYYY-MM-DD");
	}
    bool year_found = false;
    char buff[11];
    int j = 0;
    int d = 0,m = 0,y = 0;
    for(int i=0;i<len;i++) {
		char c = date[i];		
		if(c == '-') {
			if(!year_found) {
				buff[j] = '\0';
				y = atoi(buff);
				strcpy(buff,"");
				year_found = true;
				j = 0;
			}else {
				buff[j] = '\0';
				m = atoi(buff);
				strcpy(buff,"");
				j = 0;
			}
		}else {
			buff[j] = c;
			j++;
		}
    }
	buff[j] = '\0';
	int yl = strlen(buff);
	if(yl < 1 && yl > 2) {
		delete[] date;
		throw MyDateException("Invalid day");
	}
	/*if(yl == 2) {
		if(buff[0] == '0') {
			char tmp[5];
			sprintf(tmp,"20%s",buff);
			strcpy(buff,tmp);
		}
	}*/
    d = atoi(buff);
    this->m_nYear = y;
    this->m_nMonth = m;
    this->m_nDay = d;	
    delete[] date;
    validate();
}

MyDate::MyDate(int d,int m,int y) {
    MyDate dt;
    m_nOldYear = dt.m_nYear;
    m_nOldMonth = dt.m_nMonth;
    m_nOldDay = dt.m_nDay;
    this->m_nYear = y;
    this->m_nMonth = m;
    this->m_nDay = d;
    validate();
}

void MyDate::setDate(const char* date) {
    m_nOldYear = m_nYear;
    m_nOldMonth = m_nMonth;
    m_nOldDay = m_nDay;
    MyDate sd(date);
    *this = sd;    
}

void MyDate::setYear(int y) {
    m_nOldYear = m_nYear;
    this->m_nYear = y;
    validate();
}

void MyDate::setDay(int d) {
    m_nOldDay = m_nDay;
    this->m_nDay = d;
    validate();
}

void MyDate::setMonth(int m) {
    m_nOldMonth = m_nMonth;
    this->m_nMonth = m;
    validate();
}

int MyDate::getYear() {
    return m_nYear;
}

int MyDate::getMonth() {
    return m_nMonth;
}

string MyDate::getShortMonth() {
	char ret[4];
	if(m_nMonth == 1) {
		strcpy(ret,"JAN");
	}else if(m_nMonth == 2) {
		strcpy(ret,"FEB");
	}else if(m_nMonth == 3) {
		strcpy(ret,"MAR");
	}else if(m_nMonth == 4) {
		strcpy(ret,"APR");
	}else if(m_nMonth == 5) {
		strcpy(ret,"MAY");
	}else if(m_nMonth == 6) {
		strcpy(ret,"JUN");
	}else if(m_nMonth == 7) {
		strcpy(ret,"JUL");
	}else if(m_nMonth == 8) {
		strcpy(ret,"AUG");
	}else if(m_nMonth == 9) {
		strcpy(ret,"SEP");
	}else if(m_nMonth == 10) {
		strcpy(ret,"OCT");
	}else if(m_nMonth == 11) {
		strcpy(ret,"NOV");
	}else if(m_nMonth == 12) {
		strcpy(ret,"DEC");
	}
	return ret;
}

void MyDate::getFullMonth(char* ret) {
	if(m_nMonth == 1) {
		strcpy(ret,"January");
	}else if(m_nMonth == 2) {
		strcpy(ret,"February");
	}else if(m_nMonth == 3) {
		strcpy(ret,"March");
	}else if(m_nMonth == 4) {
		strcpy(ret,"April");
	}else if(m_nMonth == 5) {
		strcpy(ret,"May");
	}else if(m_nMonth == 6) {
		strcpy(ret,"June");
	}else if(m_nMonth == 7) {
		strcpy(ret,"July");
	}else if(m_nMonth == 8) {
		strcpy(ret,"August");
	}else if(m_nMonth == 9) {
		strcpy(ret,"September");
	}else if(m_nMonth == 10) {
		strcpy(ret,"October");
	}else if(m_nMonth == 11) {
		strcpy(ret,"November");
	}else if(m_nMonth == 12) {
		strcpy(ret,"December");
	}	
}

int MyDate::getDay() {
    return m_nDay;
}

string MyDate::getDate() {
    char buff[15];
	sprintf(buff,"%04d-%02d-%02d",
			m_nYear,m_nMonth,m_nDay);	
    string s = buff;
    return s;
}

string MyDate::getDate(const char* format) {
	vector<int> vctDDPos;
	vector<int> vctMMPos;
	vector<int> vctMONPos;
	vector<int> vctMonthPos;
	vector<int> vctYYPos;
	vector<int> vctYYYYPos;
	string arg = format;
	int pos = 0;
	int curr_pos = 0;
	while(true) {
		pos = arg.find("DD",curr_pos);
		if(pos == -1) break;
		vctDDPos.push_back(pos);
		curr_pos = pos + 2;
	}
	pos = 0;
	curr_pos = 0;
	while(true) {
		pos = arg.find("MM",curr_pos);
		if(pos == -1) break;
		vctMMPos.push_back(pos);
		curr_pos = pos + 2;
	}
	pos = 0;
	curr_pos = 0;
	while(true) {
		pos = arg.find("MON",curr_pos);
		if(pos == -1) break;
		vctMONPos.push_back(pos);
		curr_pos = pos + 3;
	}
	pos = 0;
	curr_pos = 0;
	while(true) {
		pos = arg.find("MONTH",curr_pos);
		if(pos == -1) break;
		vctMonthPos.push_back(pos);
		curr_pos = pos + 5;
	}
	pos = 0;
	curr_pos = 0;
	while(true) {
		pos = arg.find("YY",curr_pos);
		if(pos == -1) break;
		vctYYPos.push_back(pos);
		curr_pos = pos + 2;
	}
	pos = 0;
	curr_pos = 0;
	while(true) {
		pos = arg.find("YYYY",curr_pos);
		if(pos == -1) break;
		vctYYYYPos.push_back(pos);
		curr_pos = pos + 4;
	}
	char* buff = new char[arg.size() + vctMonthPos.size() + 51];
	strcpy(buff,arg.c_str());
	int i;
	char strDD[3];
	sprintf(strDD,"%02d",this->getDay());
	for(i=0;i<vctDDPos.size();i++) {
		int j;
		int cpos = vctDDPos[i];
		int count = 0;
		for(j=0;j<arg.size();j++) {
			if(j == cpos) {
				buff[j] = strDD[0];
				j++;
				buff[j] = strDD[1];				
				continue;
			}
			buff[j] = buff[j];
		}
		buff[j] = '\0';
	}
	char strMM[3];
	sprintf(strMM,"%02d",this->getMonth());
	for(i=0;i<vctMMPos.size();i++) {
		int j;
		int cpos = vctMMPos[i];
		int count = 0;
		for(j=0;j<arg.size();j++) {
			if(j == cpos) {
				buff[j] = strMM[0];
				j++;
				buff[j] = strMM[1];				
				continue;
			}
			buff[j] = buff[j];
		}
		buff[j] = '\0';
	}
	char strMon[4];
	string sm = this->getShortMonth();
	strcpy(strMon,sm.c_str());
	for(i=0;i<vctMONPos.size();i++) {
		int j;
		int cpos = vctMONPos[i];
		int count = 0;
		for(j=0;j<arg.size();j++) {
			if(j == cpos) {
				buff[j] = strMon[0];
				j++;
				buff[j] = strMon[1];				
				j++;
				buff[j] = strMon[2];
				continue;
			}
			buff[j] = buff[j];
		}
		buff[j] = '\0';
	}
	char strYear[5];
	sprintf(strYear,"%02d",this->getYear());
	for(i=0;i<vctYYPos.size();i++) {
		int j;
		int cpos = vctYYPos[i];
		int count = 0;
		for(j=0;j<arg.size();j++) {
			if(j == cpos) {
				buff[j] = strYear[2];
				j++;
				buff[j] = strYear[3];				
				continue;
			}
			buff[j] = buff[j];
		}
		buff[j] = '\0';
	}
	sprintf(strYear,"%04d",this->getYear());
	for(i=0;i<vctYYYYPos.size();i++) {
		int j;
		int cpos = vctYYYYPos[i];
		int count = 0;
		for(j=0;j<arg.size();j++) {
			if(j == cpos) {
				buff[j] = strYear[0];
				j++;
				buff[j] = strYear[1];
				j++;
				buff[j] = strYear[2];
				j++;
				buff[j] = strYear[3];				
				continue;
			}
			buff[j] = buff[j];
		}
		buff[j] = '\0';
	}
	char strFullMon[16];
	this->getFullMonth(strFullMon);	
	for(i=0;i<vctMonthPos.size();i++) {
		int j;
		int cpos = vctMonthPos[i];
		int count = 0;
		char* tmp = new char[arg.size() + vctMonthPos.size() + 51];
		strcpy(tmp,buff);
		int sz = arg.size();
		int c = 0;
		int idx = 0;
		for(j=0;j<sz;j++) {
			if(j == cpos) {
				for(int k=0;k<strlen(strFullMon);k++) {
					buff[idx] = strFullMon[k];
					idx++;
				}	
				int diff = strlen(strFullMon) - 5;
				if(diff > 0) { sz += diff;}
				c += 5;
				//j--;
				continue;
			}
			buff[idx] = tmp[c];
			c++;
			idx++;
		}
		buff[idx] = '\0';
		delete[] tmp;
	}
	string ret = buff;
	delete[] buff;
	return ret;
}

void MyDate::addDays(int days) {
    m_nOldDay = m_nDay;    
    for(int i=0;i<days;i++) {	
		this->m_nDay++;
		int d = getDaysInMonth();
		if(this->m_nDay > d) {
			this->m_nDay = 1;
			this->m_nMonth++;
			if(this->m_nMonth > 12) {
				this->m_nMonth = 1;
				this->m_nYear++;			
			}
		}	
    }
	correctDays();
}

void MyDate::addMonths(int mons) {
    m_nOldMonth = m_nMonth;
    for(int i=0;i<mons;i++) {
	if(this->m_nMonth > 12) {
	    this->m_nMonth = 1;
	    this->m_nYear++;				
	}
	this->m_nMonth++;
    }
    correctDays();
}

void MyDate::addYears(int yrs) {
    m_nOldYear = m_nYear;
	this->m_nYear += yrs;	
	correctDays();
}

void MyDate::lessDays(int days) {
    m_nOldDay = m_nDay;    
    for(int i=0;i<days;i++) {	
		this->m_nDay--;		
		if(this->m_nDay <= 0) {			
			this->m_nMonth--;
			int d = getDaysInMonth();
			this->m_nDay = d;
			if(this->m_nMonth <= 0) {
				this->m_nMonth = 12;
				this->m_nYear--;			
			}
		}	
    }
	correctDays();
}

void MyDate::lessMonths(int mons) {
    m_nOldMonth = m_nMonth;
    for(int i=0;i<mons;i++) {
	if(this->m_nMonth <= 0) {
	    this->m_nMonth = 12;
	    this->m_nYear--;				
	}
	this->m_nMonth--;
    }
    correctDays();
}

void MyDate::lessYears(int yrs) {
    m_nOldYear = m_nYear;	
	this->m_nYear -= yrs;
	if(m_nYear < 0) m_nYear *= -1;
	correctDays();
	validate();
}

int MyDate::differenceInDays(MyDate date) {
    int diff = 0;  
	MyDate tmp_this = *this;
    MyDate tmp_date = date;
    if(*this > date) {	
		while(true) {
			if(*this == tmp_date) break;
			tmp_date.addDays(1);
			diff++;
		}
	}else if(*this < date){
		while(true) {
			if(tmp_this == tmp_date) break;
			tmp_this.addDays(1);
			diff++;
		}
	}else {
		diff = 0;
	}
    return diff;
}

int MyDate::differenceInMonths(MyDate date) {
    int diff = 0;    
    MyDate tmp_this = *this;
    MyDate tmp_date = date;
    if(*this > date) {	
		while(true) {
			if(tmp_this.m_nYear == tmp_date.m_nYear) {
				if(tmp_this.m_nMonth == tmp_date.m_nMonth) break;
			}
			tmp_date.addMonths(1);
			diff++;
		}    
	}else if(*this < date) {	
		while(true) {
			if(tmp_this.m_nYear == tmp_date.m_nYear) {
				if(tmp_this.m_nMonth == tmp_date.m_nMonth) break;
			}
			tmp_this.addMonths(1);
			diff++;
		}   
	}else {
		diff = 0;
	}
    return diff;
}

int MyDate::differenceInYears(MyDate date) {
    int ret = this->m_nYear - date.getYear();
    if(ret < 0) ret *= -1;
    return ret;
}

bool MyDate::operator > (MyDate date) {
    if(date.getYear() < this->m_nYear) {
		return true;
    }else if(date.getYear() == this->m_nYear) {
	if(date.getMonth() < this->m_nMonth) {
	    return true;
	}else if(date.getMonth() == this->getMonth()) {
	    if(date.getDay() < this->m_nDay) {
		return true;
	    }else {
		return false;
	    }
	}else {
	    return false;
	}
    }else {
	return false;
    }
    return false;
}

bool MyDate::operator < (MyDate date) {
    if(date.getYear() > this->m_nYear) {
	return true;
    }else if(date.getYear() == this->m_nYear) {
	if(date.getMonth() > this->m_nMonth) {
	    return true;
	}else if(date.getMonth() == this->getMonth()) {
	    if(date.getDay() > this->m_nDay) {
		return true;
	    }else {
		return false;
	    }
	}else {
	    return false;
	}
    }else {
	return false;
    }
    return false;
}

bool MyDate::operator == (MyDate date) {
    if(date.getYear() == this->getYear() && date.getMonth() == this->getMonth() &&
       date.getDay() == this->getDay()) {
	return true;
    }
    return false;
}

bool MyDate::operator != (MyDate date) {
    if(date.getYear() != this->getYear() || date.getMonth() != this->getMonth() ||
       date.getDay() != this->getDay()) {
	return true;
    }
    return false;
}

void MyDate::operator = (MyDate date) {
    m_nOldYear = m_nYear;
    m_nOldMonth = m_nMonth;
    m_nOldDay = m_nDay;
    this->m_nYear = date.getYear();
    this->m_nMonth = date.getMonth();
    this->m_nDay = date.getDay();
    validate();
}

void MyDate::operator = (char* date) {
    this->setDate(date);
}

void MyDate::validate() {    
	if(this->m_nYear <= 0) {		
		char buff[51];
		sprintf(buff,"Not a valid year: %d",m_nYear);
		restore();
		throw MyDateException(buff);
	}
    if(this->m_nMonth < 1 || this->m_nMonth > 12) {		
		char buff[51];
		sprintf(buff,"Not a valid month: %d",m_nMonth);
		restore();
		throw MyDateException(buff);
    }
    if(this->m_nDay < 1 || this->m_nDay > 31) {			
		char buff[51];
		sprintf(buff,"Not a valid day: %d",m_nDay);
		restore();
		throw MyDateException(buff);
    }
    if(this->m_nDay > this->getDaysInMonth()) {			
		char buff[51];
		sprintf(buff,"Not a valid day: %d",m_nDay);
		restore();
		throw MyDateException(buff);
    }    
}

int MyDate::getDaysInMonth() {
    if(this->m_nMonth == 1) {
	return 31;
    }else if(this->m_nMonth == 2) {
		if(isLeapYear()) return 29;
		else return 28;		
    }else if(this->m_nMonth == 3) {
		return 31;
    }else if(this->m_nMonth == 4) {
		return 30;
    }else if(this->m_nMonth == 5) {
		return 31;
    }else if(this->m_nMonth == 6) {
		return 30;
    }else if(this->m_nMonth == 7) {
		return 31;
    }else if(this->m_nMonth == 8) {
		return 31;
    }else if(this->m_nMonth == 9) {
		return 30;
	}else if(this->m_nMonth == 10) {
		return 31;
    }else if(this->m_nMonth == 11) {
		return 30;
    }else if(this->m_nMonth == 12) {
		return 31;
    }
	return -1;
}

bool MyDate::isLeapYear() {
	bool ret = true;
	if(m_nYear <= 1752) {
		if(m_nYear % 4 == 0) return true;
	}else {
		if((m_nYear % 4 == 0 && m_nYear % 100 != 0)
			|| m_nYear % 400 == 0) {
			return true;
		}
	}
	return false;
}

void MyDate::correctDays() {
    int days = getDaysInMonth();
    if(this->m_nDay > days) {
	this->m_nDay = days;
    }
}

void MyDate::restore() {
	m_nYear = m_nOldYear;
	m_nMonth = m_nOldMonth;
	m_nDay = m_nOldDay;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License

Share

About the Author

No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 23 Mar 2006
Article Copyright 2003 by AnOldGreenHorn
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid