/* time.c : return the elapsed seconds since midnight Jan 1 1970 GMT */
/* written by Eric R. Smith and placed in the public domain */

#include <time.h>
#include <osbind.h>
#include "lib.h"

static struct tm this_tm;
int _dst;

/* unixtime: convert a DOS time/date pair into a unix time */
/* in case people were wondering about whether or not DST is applicable
 * right now, we set the external variable _dst to the value
 * returned from mktime
 */

time_t 
_unixtime(dostime, dosdate)
	unsigned dostime, dosdate;
{
	time_t t;
	struct tm *stm = &this_tm;

	stm->tm_sec = (dostime & 31) << 1;
	stm->tm_min = (dostime >> 5) & 63;
	stm->tm_hour = (dostime >> 11) & 31;
	stm->tm_mday = dosdate & 31;
	stm->tm_mon = ((dosdate >> 5) & 15) - 1;
	stm->tm_year = 80 + ((dosdate >> 9) & 255);
	stm->tm_isdst = -1;	/* we don't know about DST */
	stm->tm_wday = stm->tm_yday = -1; /* or about these */

/* mktime expects a local time, which is what we're giving it */
	t = mktime(stm);

	_dst = (stm->tm_isdst == 1) ? 1 : 0;
	return t;
}

time_t time(t)
	time_t *t;
{
	unsigned dostime, dosdate;
	time_t	made;

	dostime = Tgettime();
	dosdate = Tgetdate();
	made = _unixtime(dostime, dosdate);
	if (t)
		*t = made;
	return made;
}
