#include <andrewos.h>
#include <class.h>

#include <date.eh>

static char *shortMonths[] = {
    "Jan",
    "Feb",
    "Mar",
    "Apr",
    "May",
    "Jun",
    "Jul",
    "Aug",
    "Sep",
    "Oct",
    "Nov",
    "Dec"
};
static char *months[] = {
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
};
static char *shortWeekdays[] = {
    "Sun",
    "Mon",
    "Tues",
    "Wed",
    "Thur",
    "Fri",
    "Sat"
};
static char *weekdays[] = {
    "Sunday",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday"
};

boolean date__Parse(classid, str, year, month, day, hour, min, sec, weekday, gtm)
struct classheader *classid;
char *str;
long *year;
long *month;
long *day;
long *hour;
long *min;
long *sec;
long *weekday;
long *gtm;
{
    struct tm timeBuf;

    if (parsedate(str, &timeBuf, 1, 1, 1, 0)) {
	return TRUE;
    }

    if (timeBuf.tm_hour == -1)
	timeBuf.tm_hour = 0;
    if (timeBuf.tm_min == -1)
	timeBuf.tm_min = 0;
    if (timeBuf.tm_sec == -1)
	timeBuf.tm_sec = 0;
    *year = timeBuf.tm_year;
    *month = timeBuf.tm_mon;
    *day = timeBuf.tm_mday;
    *hour = timeBuf.tm_hour;
    *min = timeBuf.tm_min;
    *sec = timeBuf.tm_sec;
    *weekday = timeBuf.tm_wday;
    *gtm = gtime(&timeBuf);

    return FALSE;
}


boolean date__ParseIntoTimeStruct(classid, str, timeBuf)
struct classheader *classid;
char *str;
struct tm *timeBuf;
{
    return parsedate(str, timeBuf, 1, 1, 1, 0);
}

boolean date__ParseIntoTime(classid, str, time)
struct classheader *classid;
char *str;
time_t *time;
{
    if (parseDate(str, timeBuf, 1, 1, 1, 0)) {
	return TRUE;
    }
    else {
	*time = gtime(timeBuf);
	return FALSE;
    }
}
	
char *date__MonthStr(classid, month)
struct classheader *classid;
long month;
{
    return months[month];
}

char *date__ShortMonthStr(classid, month)
struct classheader *classid;
long month;
{
    return shortMonths[month];
}

char *date__WeekdayStr(classid, weekday)
struct classheader *classid;
long weekday;
{
    return weekdays[weekday];
}
char *date__ShortWeekdayStr(classid, weekday)
struct classheader *classid;
long weekday;
{
    return shortWeekdays[weekday];
}

char *date__ShortDate(classid, year, month, day, buffer)
struct classheader *classid;
long year;
long month;
long day;
char *buffer;
{
    if (buffer == NULL) {
	buffer = (char *) malloc(sizeof(char) * 10);
    }
    sprintf(buffer, "%2d-%s-%02d", day, shortMonths[month], year);
    return buffer;
}

char *date__Date(classid, year, month, day, buffer)
struct classheader *classid;
long year;
long month;
long day;
char *buffer;
{
    if (buffer == NULL) {
	buffer = (char *) malloc(sizeof(char) * 20);
    }
    sprintf(buffer, "%d-%s-%d", day, months[month], year);
    return buffer;
}



