//	Zinc Application Framework - Z_DATE.CPP
//	COPYRIGHT (C) 1990-1999.  All Rights Reserved.
//	Zinc Software Incorporated.  Pleasant Grove, Utah  USA

#include <z_date.hpp>
#include <z_string.hpp> // For strcmp().
#define ZAF_DATE_DATA_INFO
#include "gbl_def.cpp"

// ----- ZafDateData --------------------------------------------------------

ZafDateData::ZafDateData(void) :
	ZafUTimeData()
{
	// No additional information needs to be initialized.
}

ZafDateData::ZafDateData(int year, int month, int day) :
	ZafUTimeData(year, month, day, 0, 0, 0, 0)
{
	// No additional information needs to be initialized.
}

ZafDateData::ZafDateData(const ZafIChar *string, const ZafIChar *format) :
	ZafUTimeData()
{
	// Set the date from the string argument.
	SetDate(string, format);
}

ZafDateData::ZafDateData(const ZafDateData &copy) :
	ZafUTimeData(copy)
{
	// No additional information needs to be initialized.
}

ZafDateData::ZafDateData(const ZafUTimeData &copy) :
	ZafUTimeData(copy)
{
	// No additional information needs to be initialized.
}

ZafDateData::~ZafDateData(void)
{
	// No information needs to be deleted.
}

void ZafDateData::Add(const ZafUTimeData &utime)
{
	PushLevel();
	long tUsec = uSec;
	ZafUTimeData::Add(utime);
	uSec = tUsec;
	PopLevel();
}

void ZafDateData::Clear(void)
{
	// Defer to the base class.
	ZafUTimeData::Clear();
}

void ZafDateData::Date(int *year, int *month, int *day, int *dayOfWeek)
{
	ConvertJDay(year, month, day, dayOfWeek);
}

ZafData *ZafDateData::Duplicate(void)
{
	return (new ZafDateData(*this));
}

int ZafDateData::FormattedText(ZafIChar *buffer, int maxLength, const ZafIChar *format) const
{
	// Format the text.
	return (Sprintf(buffer, maxLength, format ? format : zafLocale->dateStringOutputFormat));
}

bool ZafDateData::IsA(ZafClassID compareID) const
{
	return ((compareID == classID) ? true : ZafUTimeData::IsA(compareID));
}

bool ZafDateData::IsA(ZafClassName compareName) const
{
	return (!strcmp(compareName, className) ? true : ZafUTimeData::IsA(compareName));
}

ZafError ZafDateData::SetDate(int year, int month, int day)
{
	// Defer to the base class.
	return SetUTime(year, month, day, 0, 0, 0, 0);
}

ZafError ZafDateData::SetDate(const ZafDateData &number)
{
	// Defer to the base class.
	return SetUTime(number);
}

ZafError ZafDateData::SetDate(const ZafIChar *buffer, const ZafIChar *format)
{
	// Reset the value.
	PushLevel();
	if (!buffer || !buffer[0])
		Clear();
	else if (!Sscanf(buffer, (format && format[0]) ? format : zafLocale->dateStringInputFormat))
		SetError(ZAF_ERROR_INVALID);
	else
		SetError(ZAF_ERROR_NONE);
	PopLevel();

	// Return the error status.
	return (Error());
}

ZafError ZafDateData::SetIncrementDate(int year, int month, int day)
{
	// Increments are based off the date 1/1/1.
	return (SetDate(year + 1, month + 1, day + 1));
}

void ZafDateData::Subtract(const ZafUTimeData &utime)
{
	PushLevel();
	long tUsec = uSec;
	ZafUTimeData::Subtract(utime);
	uSec = tUsec;
	PopLevel();
}

long ZafDateData::Value(void) const
{
	// Return the day value.
	return (jDay);
}

ZafDateData ZafDateData::operator+(long days)
{
	ZafDateData tmp(*this);
	tmp += ZafDateData(0, 0, (int)days);
	return (tmp);
}

ZafDateData ZafDateData::operator-(long days)
{
	ZafDateData tmp(*this);
	tmp -= ZafDateData(0, 0, (int)days);
	return (tmp);
}

ZafDateData &ZafDateData::operator+=(const ZafUTimeData &date)
{
	Add(date);
	return (*this);
}

ZafDateData &ZafDateData::operator-=(const ZafUTimeData &date)
{
	Subtract(date);
	return (*this);
}

ZafDateData ZafDateData::operator++(void)
{
	// Reset the value.
	PushLevel();
	*this += 1L;
	PopLevel();

	// Return a reference pointer.
	return *this;
}

ZafDateData ZafDateData::operator++(int)
{
	// Reset the value.
	PushLevel();
	ZafDateData tmp(*this);
	*this += 1L;
	PopLevel();

	// Return a temporary reference pointer.
	return tmp;
}

ZafDateData ZafDateData::operator--(void)
{
	// Reset the value.
	PushLevel();
	*this -= 1L;
	PopLevel();

	// Return a reference pointer.
	return *this;
}

ZafDateData ZafDateData::operator--(int)
{
	// Reset the value.
	PushLevel();
	ZafDateData tmp(*this);
	*this -= 1L;
	PopLevel();

	// Return a temporary reference pointer.
	return tmp;
}

// ----- Persistent functions -----------------------------------------------
#if defined(USE_ZAF_PERSISTENCE)
ZafDateData::ZafDateData(const ZafIChar *name, ZafDataPersistence &persist) :
	ZafUTimeData(name, persist.PushLevel(className, classID, ZAF_PERSIST_FILE))
{
	Bind();
	persist.PopLevel();
	if (persist.Error() != ZAF_ERROR_NONE)
		SetError(persist.Error());
}

ZafElement *ZafDateData::Read(const ZafIChar *name, ZafDataPersistence &persist)
{
	return (new ZafDateData(name, persist));
}

void ZafDateData::Write(ZafDataPersistence &persist)
{
	// Write the object.
	ZafUTimeData::Write(persist.PushLevel(className, classID, ZAF_PERSIST_FILE));
	persist.PopLevel();
	if (persist.Error() != ZAF_ERROR_NONE)
		SetError(persist.Error());
}
#endif
