#include "xtime.h"
#include "xstring.h"
#include "time.h"
#include "xdate.h"


/*@ XTime::operator ==(const XTime &ti)
@group operators
@remarks Compare two XTimes
*/

/*@ XTime::operator !=(const XTime &ti)
@group operators
@remarks Compare two XTimes
*/


/*@ XTime::operator =(const XTime &ti)
@group operators
@remarks Copy the current time to another XTime instance
*/


/*@ XTime :: XTime(UCHAR hours, UCHAR minutes, UCHAR seconds)
@group constructors/desructors
@remarks Construct a XTime, if no parameters are used all values are zero, call GetCurrentTime() to get the current time.
@parameters: UCHAR hours<BR>
            UCHAR minutes<BR>
            UCHAR seconds
*/
XTime :: XTime(UCHAR hours, UCHAR minutes, UCHAR seconds)
{
	t.hours = hours;
	t.minutes = minutes;
	t.seconds = seconds;
	t.dayLightSave = FALSE;
}


/*@ XTime::Compare(const XTime * ti)
@group misc
@remarks Compares the time stored with another time
@parameters: XTime * time
@returns  <t 'ø' c=2>
				øSHORT result      ø-1 the time stored is earlier<BR>
                            0 the times are equal<BR>
                            1 the time stored is later<BR>
			</t>
*/
SHORT XTime::Compare(const XTime * ti) const
{
	if (t.hours < ti->t.hours)
		return -1;
	if (t.hours > ti->t.hours)
		return 1;
	if (t.minutes < ti->t.minutes)
		return -1;
	if (t.minutes > ti->t.minutes)
		return 1;
	if (t.seconds < ti->t.seconds)
		return -1;
	return (t.seconds > ti->t.seconds ? 1 : 0);
}


/*@ XTime::GetCurrentTime(void)
@group misc
@remarks Query the current time.
*/
void XTime::GetCurrentTime(void)
{
	tm *ti;
	time_t ltime;

	time(&ltime);
	ti = localtime(&ltime);

	t.hours = ti->tm_hour;
	t.minutes = ti->tm_min;
	t.seconds = ti->tm_sec;
	t.dayLightSave = ti->tm_isdst;
}


/*@ XTime::Format(XString * buffer, const char *format)
@group misc
@remarks Print the date formated in a string
@parameters: XString * buffer    buffer which will hold data<BR>
            char * format       string which holds the format to use, see strftime() for valid format attributes
*/
void XTime::Format(XString * buffer, const char *format)
{
	tm  td;

	td.tm_sec = t.seconds;
	td.tm_min = t.minutes;
	td.tm_hour = t.hours;
	td.tm_mday = 0;
	td.tm_mon = 0;
	td.tm_year = 0;
	td.tm_wday = 0;
	td.tm_yday = 0;
	td.tm_isdst = t.dayLightSave;

	size_t l = strftime(buffer->GetBuffer(512), 512, format, &td);

	buffer->ReleaseBuffer(l);
}


XDate :: XDate(UCHAR day, UCHAR month, SHORT year, UCHAR weekD, USHORT dayOfYear)
{
	d.days = day;
	d.months = month;
	d.years = year;
	d.weekday = weekD;
	d.dayOfYear = dayOfYear;
}


SHORT XDate::Compare(const XDate * date) const
{
	if (d.years < date->d.years)
		return -1;
	if (d.years > date->d.years)
		return 1;
	if (d.months < date->d.months)
		return -1;
	if (d.months > date->d.months)
		return 1;
	if (d.days < date->d.days)
		return -1;
	return (d.days > date->d.days ? 1 : 0);
}


/*@ XDate::GetCurrentDate(void)
@group misc
@remarks Query the current date from the system
*/
void XDate::GetCurrentDate(void)
{
	tm *t;
	time_t ltime;

	time(&ltime);
	t = localtime(&ltime);
	d.days = t->tm_mday;
	d.months = t->tm_mon;
	d.years = t->tm_year + 1900;
	d.weekday = t->tm_wday;
	d.dayOfYear = t->tm_yday;
}


/*@ XDate::Format(XString * buffer, const char *format)
@group misc
@remarks Print the date into a string
@parameters XString * buffer		string which get the date<BR>
				char * format			format, see strftime() for details
*/
void XDate::Format(XString * buffer, const char *format)
{
	tm  td;

	td.tm_sec = 0;
	td.tm_min = 0;
	td.tm_hour = 0;
	td.tm_mday = d.days;
	td.tm_mon = d.months;
	td.tm_year = d.years - 1900;
	td.tm_wday = d.weekday;
	td.tm_yday = d.dayOfYear;
	td.tm_isdst = 0;

	size_t l = strftime(buffer->GetBuffer(512), 512, format, &td);

	buffer->ReleaseBuffer(l);
}
