#include "sumstl.h"

extern struct DOSLibary *DOSBase;


// DateToSeconds:
//
// Wandelt beliebigen Datums-String in Sekunden seit 1.1.1978 um.
// Kennt bisher die folgenden Formate:
//
//  0         1         2
//  012345678901234567890
//
// "01 Jan 91  11:22:33"    Fido Standard                 DF_FIDO
// "01 Jan 91  11:22"       Fido Standard ohne Sekunden
// "Wed 13 Jan 86 02:34"    Fido SEAdog
// "JJMMTThhmm"             Z-Netz                        DF_ZNETZ
// "01-Jan-78 10:11:12"     AmigaDOS                      DF_ADOS


LONG __regargs DateToSeconds(UBYTE *misc)
{
	#define space(x) (misc[x]==' ')

	int len = misc ? strlen(misc) : 0;
	UBYTE dbuf[10] = "01-Jan-78";
	UBYTE tbuf[10] = "00:00:00";
	struct DateTime dt = { {0,0,0},FORMAT_DOS,0,NULL,NULL,NULL };

	dt.dat_StrDate = dbuf;
	dt.dat_StrTime = tbuf;

	if (len>=19 && space(2) && space(6) && space(9) && space(10))
	{
		// Standard Fido

		dbuf[0]=misc[0];
		dbuf[1]=misc[1];
		dbuf[3]=misc[3];
		dbuf[4]=misc[4];
		dbuf[5]=misc[5];
		dbuf[7]=misc[7];
		dbuf[8]=misc[8];

		tbuf[0]=misc[11+0];
		tbuf[1]=misc[11+1];
		tbuf[3]=misc[11+3];
		tbuf[4]=misc[11+4];
		tbuf[6]=misc[11+6];
		tbuf[7]=misc[11+7];
	}
	else if (len>=16 && space(2) && space(6) && space(9) && space(10))
	{
		// Standard Fido ohne Sekunden

		dbuf[0]=misc[0];
		dbuf[1]=misc[1];
		dbuf[3]=misc[3];
		dbuf[4]=misc[4];
		dbuf[5]=misc[5];
		dbuf[7]=misc[7];
		dbuf[8]=misc[8];

		tbuf[0]=misc[11+0];
		tbuf[1]=misc[11+1];
		tbuf[3]=misc[11+3];
		tbuf[4]=misc[11+4];
	}
	else if (len>=19 && space(3) && space(6) && space(10) && space(13))
	{
		// SEAdog

		dbuf[0]=misc[4+0];
		dbuf[1]=misc[4+1];
		dbuf[3]=misc[4+3];
		dbuf[4]=misc[4+4];
		dbuf[5]=misc[4+5];
		dbuf[7]=misc[4+7];
		dbuf[8]=misc[4+8];

		tbuf[0]=misc[14+0];
		tbuf[1]=misc[14+1];
		tbuf[3]=misc[14+3];
		tbuf[4]=misc[14+4];
	}
	else if (len==10)
	{
		// Z-Netz

		dt.dat_Format = FORMAT_CDN; // (dd-mm-yy)

		dbuf[0]=misc[4];
		dbuf[1]=misc[5];
		dbuf[2]='-';
		dbuf[3]=misc[2];
		dbuf[4]=misc[3];
		dbuf[5]='-';
		dbuf[6]=misc[0];
		dbuf[7]=misc[1];
		dbuf[8]=0;

		tbuf[0]=misc[6];
		tbuf[1]=misc[7];
		tbuf[3]=misc[8];
		tbuf[4]=misc[9];
	}
	else
	{
		// Unbekanntes Format, AmigaDOS soll sein Glück probieren

		char *x;

		if (x=strstr(misc,"day ")) misc=x+4;

		strncpy(dbuf,misc,9);    // Die ersten 9 Zeichen sind Datum

		if ((x=strchr(misc,':')) && x>=misc+2)
		{
			strncpy(tbuf,x-2,8);  // 2 Zeichen vor dem ersten : ist die Zeit
		}
	}

	if (StrToDate(&dt))
		return((dt.dat_Stamp.ds_Days*24*60+dt.dat_Stamp.ds_Minute)*60+dt.dat_Stamp.ds_Tick/TICKS_PER_SECOND);
	else
		return(0);
}



// SecondsToDate
//
// Wandelt Sekunden seit 1.1.1978 je nach <format> in Datumsstring um.
// Ergebnis kommt nach <buf> (DF_MINLEN!) oder ist static falls buf==NULL.
// Kennt die Formate DF_ZNETZ, DF_ADOS und DF_FIDO.

char * __regargs SecondsToDate(LONG seconds,LONG format,char *buf)
{
	UBYTE dbuf[LEN_DATSTRING];
	UBYTE tbuf[LEN_DATSTRING];
	struct DateTime dt = { {0,0,0},FORMAT_DOS,0,NULL,NULL,NULL };
	static UBYTE buffer[DF_MINLEN];

	if (!buf) buf=buffer;

	dt.dat_StrDate = dbuf;
	dt.dat_StrTime = tbuf;

	dt.dat_Stamp.ds_Days   = seconds/(24*60*60);
	seconds%=24*60*60;
	dt.dat_Stamp.ds_Minute = seconds/60;
	seconds%=60;
	dt.dat_Stamp.ds_Tick   = seconds*TICKS_PER_SECOND;

	if (format==DF_ZNETZ)
	{
		dt.dat_Format = FORMAT_CDN;
		if (DateToStr(&dt))
		{
			buf[0]=dbuf[6];
			buf[1]=dbuf[7];
			buf[2]=dbuf[3];
			buf[3]=dbuf[4];
			buf[4]=dbuf[0];
			buf[5]=dbuf[1];
			buf[6]=tbuf[0];
			buf[7]=tbuf[1];
			buf[8]=tbuf[3];
			buf[9]=tbuf[4];
			buf[10]=0;
		}
		else
			strcpy(buf,"0000000000");
	}
	else if (format==DF_ADOS)
	{
		if (DateToStr(&dt))
		{
			strcpy(buf,dbuf);
			strcpy(buf+10,tbuf);
			buf[9]=' ';
		}
		else
			strcpy(buf,"xx-xxx-xx xx:xx:xx");
	}
	else if (format==DF_FIDO)
	{
		if (DateToStr(&dt))
		{
			strcpy(buf,dbuf);
			strcpy(buf+11,tbuf);
			buf[2]=buf[6]=buf[9]=buf[10]=' ';
		}
		else
			strcpy(buf,"xx xxx xx  xx:xx:xx");
	}
	else
	{
		buf[0]=0;
	}

	return(buf);
}


LONG __regargs CurrentSeconds(VOID)
{
	struct DateStamp ds;

	if (DateStamp(&ds))
	{
		return((ds.ds_Days*24*60+ds.ds_Minute)*60+ds.ds_Tick/TICKS_PER_SECOND);
	}
	return(0);
}
