#include <common/systime.hpp>

String SystemTime::toString(void)const
{
	String systemTime;
	String dayString;
	String monthString;

	getMonth(monthString);
	getDay(dayString);
	::sprintf(systemTime,"%s%s %d,%d %02d:%02d:%02d",(LPSTR)dayString,(LPSTR)monthString,day(),year(),hour(),minute(),second());
	return systemTime;
}

String SystemTime::toStringShort(void)const
{
	String systemTime;
	String dayString;
	String monthString;

	getMonth(monthString);
	getDay(dayString,true);
	::sprintf(systemTime,"%s%s %d,%d %02d:%02d:%02d",(LPSTR)dayString,(LPSTR)monthString,day(),year(),hour(),minute(),second());
	return systemTime;
}

bool SystemTime::operator>(const SystemTime &someSystemTime)const
{
	if(year()>someSystemTime.year())return TRUE;
  else if(year()<someSystemTime.year())return FALSE;
	if(month()>someSystemTime.month())return TRUE;
	else if(month()<someSystemTime.month())return FALSE;
	if(day()>someSystemTime.day())return TRUE;
	else if(day()<someSystemTime.day())return FALSE;
	if(hour()>someSystemTime.hour())return TRUE;
	else if(hour()<someSystemTime.hour())return FALSE;
	if(minute()>someSystemTime.minute())return TRUE;
	else if(minute()<someSystemTime.minute())return FALSE;
	if(second()>someSystemTime.second())return TRUE;
	else if(second()<someSystemTime.second())return FALSE;
	if(milliseconds()>someSystemTime.milliseconds())return TRUE;
  return FALSE;
}

bool SystemTime::operator<(const SystemTime &someSystemTime)const
{
  if(*this>someSystemTime)return FALSE;
	if(*this==someSystemTime)return FALSE;
  return TRUE;
}

void SystemTime::makeTime(WORD year,WORD month,WORD day,WORD hour,WORD minute,WORD second)
{
  WORD wFatDate;
	WORD wFatTime;
	FILETIME fileTime;

	wFatDate=day;
	wFatDate|=(month<<5);
	wFatDate|=((year-1980)<<9);
	wFatTime=second/2;
	wFatTime|=(minute<<5);
	wFatTime|=(hour<<11);
	::DosDateTimeToFileTime(wFatDate,wFatTime,&fileTime);
	::FileTimeToSystemTime(&fileTime,&((SYSTEMTIME&)*this));
}

void SystemTime::getMonth(String &monthString)const
{
	switch(month())
	{
		case January :
			monthString="January";
			break;
		case February :
			monthString="February";
			break;
		case March :
			monthString="March";
			break;
		case April :
			monthString="April";
			break;
		case May :
			monthString="May";
			break;
		case June :
			monthString="June";
			break;
		case July :
			monthString="July";
			break;
		case August :
			monthString="August";
			break;
		case September :
			monthString="September";
			break;
		case October :
			monthString="October";
			break;
		case November :
			monthString="November";
			break;
		case December :
			monthString="December";
			break;
	}
}

void SystemTime::getDay(String &dayString,bool shortName)const
{
	switch(dayOfWeek())
	{
		case Monday :
		  if(shortName)dayString="Mon";
			else dayString="Monday";
			break;
		case Tuesday :
		  if(shortName)dayString="Tue";
			else dayString="Tuesday";
			break;
		case Wednesday :
		  if(shortName)dayString="Wed";
			else dayString="Wednesday";
			break;
		case Thursday :
		  if(shortName)dayString="Thu";
			else dayString="Thursday";
			break;
		case Friday :
		  if(shortName)dayString="Fri";
			else dayString="Friday";
			break;
		case Saturday :
		  if(shortName)dayString="Sat";
			else dayString="Saturday";
			break;
		case Sunday :
		  if(shortName)dayString="Sun";
			else dayString="Sunday";
			break;
		default :
			dayString="unknown";
      break;
	}
  dayString+=", ";
}

void SystemTime::daysAdd360(short dayCount)
{
  long totalDays;
	WORD isThirtyFirst;

	if(!year()&&!day()&&!month()||!dayCount)return;
	isThirtyFirst=LongestDaysInMonth==day();
	if(isThirtyFirst)day(LongestDaysInMonth-1);
	totalDays=360*(long)year()+30*(long)month()+(long)day()+dayCount-391;
	day((short)(totalDays%30)+1);
	month(Month((short)(totalDays/30)%12+1));
	year((short)(totalDays/360)+1);
	if(!(dayCount%30)&&isThirtyFirst)
	switch(month())
	{
	  case January : 
		case March :
		case May :
		case July :
		case August :
		case October :
		case December :
		  day(day()+1);
			break;
	}
}
