Click here to Skip to main content
Click here to Skip to main content
Articles » Database » Database » General » Downloads
 
Add your own
alternative version
Go to top

DarkSide SQL Mini Version 1, The embedded database

, 23 Mar 2006
An embedded database library in C++.
// mytime.cpp
// defines a time type
// Date: 22/11/03
// 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>
using namespace std;

#include "mytime.h"
using namespace openutils;

MyTime::MyTime() {
	struct tm *newtime;
    time_t long_time;
    time( &long_time );                /* Get time as long integer. */
    newtime = localtime( &long_time ); /* Convert to local time. */
    hr = newtime->tm_hour;
    min = newtime->tm_min;
	sec = newtime->tm_sec;
}

MyTime::MyTime(const char* time) {
	int len = strlen(time);
	if(len < 5) {
		throw MyTimeException("Invalid time format");
	}
	bool hr_found = false;
	char *buff = new char[len+1];
	strcpy(buff,"");
	int j = 0;
	for(int i=0;i<len;i++) {
		char c = time[i];
		if(c == ':') {
			if(hr_found) {
				buff[j] = '\0';
				min = atoi(buff);
				strcpy(buff,"");
				j = 0;
			}else {
				hr_found = true;
				buff[j] = '\0';
				hr = atoi(buff);
				strcpy(buff,"");
				j = 0;
			}
		}else {
			buff[j] = c;
			j++;
		}
	}
	buff[j] = '\0';
	sec = atoi(buff);
	delete[] buff;
	validate();
}

void MyTime::setTime(const char* time) {
	MyTime t(time);
	*this = t;
}

string MyTime::getTime(bool _24hourformat) {
	string ret = "";
	char buff[24];
	if(_24hourformat) {		
		sprintf(buff,"%02d:%02d:%02d",hr,min,sec);
	}else {
		int tmp_hr = hr;
		char am_pm[3];
		strcpy(am_pm,"AM");
		if(tmp_hr > 12) {
			tmp_hr -= 12;
			strcpy(am_pm,"PM");
		}
		sprintf(buff,"%02d:%02d:%02d %s",tmp_hr,min,sec,am_pm);
	}
	ret = buff;
	return ret;
}

bool MyTime::operator == (MyTime time) {
	if(hr == time.hr && min == time.min && sec == time.sec) {
		return true;
	}
	return false;
}
bool MyTime::operator != (MyTime time) {
	if(hr != time.hr || min != time.min || sec != time.sec) {
		return true;
	}
	return false;
}
bool MyTime::operator > (MyTime time) {
	if(hr > time.hr) {
		return true;
	}else if(hr == time.hr) {
		if(min > time.min) {
			return true;
		}else if(min == time.min) {
			if(sec > time.sec) {
				return true;
			}else {
				return false;
			}
		}else {
			return false;
		}
	}else {
		return false;
	}
	return false;
}

bool MyTime::operator < (MyTime time) {
	if(hr < time.hr) {
		return true;
	}else if(hr == time.hr) {
		if(min < time.min) {
			return true;
		}else if(min == time.min) {
			if(sec < time.sec) {
				return true;
			}else {
				return false;
			}
		}else {
			return false;
		}
	}else {
		return false;
	}
	return false;
}

void MyTime::validate() {
	if(hr < 1 || hr > 24) {
		throw MyTimeException("Invalid hour");
	}
	if(min < 0 || min > 59) {
		throw MyTimeException("Invalid minute");
	}
	if(sec < 0 || sec > 59) {
		throw MyTimeException("Invalid second");
	}
}

MyTimeException::MyTimeException(const char* er) {
	str = er;
}

string MyTimeException::getMessage() {
	return str;
}

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 23 Mar 2006
Article Copyright 2003 by AnOldGreenHorn
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid