#include "time.h"
#include <stdio.h>
#include <strings.h>
#include <ctype.h>


#define MAXLINE 1000

long rltime();

long ltime(tstring) /* convert time strings, e.g. "10:30am Jan 30 1985"
		       to long ints -- relative (+ or -) dates permitted
		       (returns seconds since midnight, Jan. 1, 1970) */
char *tstring;
{
	struct tm tmstruct;
	long t, maketime();

	if (*tstring == '+')
		return(time(0) + rltime(++tstring));
	else if (*tstring == '-')
		return(time(0) - rltime(++tstring));
	if (partime(tstring, &tmstruct) == 0) {
		fprintf(stderr, "ltime.c: failed to parse time string -- \"%s\"\n", tstring);
		exit(1);
	}
	if ((t = maketime(&tmstruct)) == 0) {
		fprintf(stderr, "ltime.c: got nonsensical parsing of time string -- \"%s\"\n", tstring);
		exit(1);
	}
	return(t);
}

long rltime(tstring) /* convert relative time strings to long ints,
			e.g. "1week 3hours" ==> 615600 (seconds) */
char *tstring;
{
	int multiplier = 0;
	char timeword[128];

	while (*tstring == ' ' || *tstring == '\t' || *tstring == '\n')
		tstring++; 		/* trim leading spaces */
	if (*tstring == '\0')
		return(0);

	while (*tstring >= '0' && *tstring <= '9')
		multiplier = multiplier * 10 + (*tstring++ - '0');
	if (multiplier == 0)
		multiplier = 1;

	while (*tstring == ' ' || *tstring == '\t' || *tstring == '\n')
		tstring++; 		/* trim leading spaces */

	if (strncmp(tstring, "weeks", 5) == 0)
		return(multiplier*7*24*60*60 + rltime(tstring+5));
	if (strncmp(tstring, "week", 4) == 0)
		return(multiplier*7*24*60*60 + rltime(tstring+4));
	if (strncmp(tstring, "days", 4) == 0)
		return(multiplier*24*60*60 + rltime(tstring+4));
	if (strncmp(tstring, "day", 3) == 0)
		return(multiplier*24*60*60 + rltime(tstring+3));
	if (strncmp(tstring, "hours", 5) == 0)
		return(multiplier*60*60 + rltime(tstring+5));
	if (strncmp(tstring, "hour", 4) == 0)
		return(multiplier*60*60 + rltime(tstring+4));
	if (strncmp(tstring, "minutes", 7) == 0)
		return(multiplier*60 + rltime(tstring+7));
	if (strncmp(tstring, "minute", 6) == 0)
		return(multiplier*60 + rltime(tstring+6));
	if (strncmp(tstring, "mins", 4) == 0)
		return(multiplier*60 + rltime(tstring+4));
	if (strncmp(tstring, "min", 3) == 0)
		return(multiplier*60 + rltime(tstring+3));
	if (strncmp(tstring, "seconds", 7) == 0)
		return(multiplier + rltime(tstring+7));
	if (strncmp(tstring, "second", 6) == 0)
		return(multiplier + rltime(tstring+6));
	if (strncmp(tstring, "secs", 4) == 0)
		return(multiplier + rltime(tstring+4));
	if (strncmp(tstring, "sec", 3) == 0)
		return(multiplier + rltime(tstring+3));
	else
		fprintf(stderr, "rltime: can't parse time string '%s' ... returning zero\n", tstring);
}


rctime(time, tstring)
long time;
char *tstring;
{
	int weeks, days, hours, minutes, seconds;
	char cbuf[MAXLINE];
	int gotone = 0;		/* have we already put something in?
				   (for putting spaces in the right places) */
	
	weeks = (time - time%(7*24*60*60)) / (7*24*60*60);
	time -= weeks*7*24*60*60;
	days = (time - time%(24*60*60)) / (24*60*60); 
	time -= days*24*60*60;
	hours = (time - time%(60*60)) / (60*60);
	time -= hours*60*60;
	minutes = (time - time%60) / 60;
	seconds = time%60;
	
	*tstring = '\0';   /* clear out crud */
	
	if (weeks > 0) {
		itoa(weeks, cbuf);
		strcat(tstring, cbuf);
		strcat(tstring, (weeks == 1) ? " week" : " weeks");
		gotone = 1;
	}
	if (days > 0) {
		if (gotone)
			strcat(tstring, " ");
		itoa(days, cbuf);
		strcat(tstring, cbuf);
		strcat(tstring, (days == 1) ? " day" : " days");
		gotone = 1;
	}
	if (hours > 0) {
		if (gotone)
			strcat(tstring, " ");
		itoa(hours, cbuf);
		strcat(tstring, cbuf);
		strcat(tstring, (hours == 1) ? " hour" : " hours");
		gotone = 1;
	}
	if (minutes > 0) {
		if (gotone)
			strcat(tstring, " ");
		itoa(minutes, cbuf);
		strcat(tstring, cbuf);
		strcat(tstring, (minutes == 1) ? " minute" : " minutes");
		gotone = 1;
	}
	if (seconds > 0) {
		if (gotone)
			strcat(tstring, " ");
		itoa(seconds, cbuf);
		strcat(tstring, cbuf);
		strcat(tstring, (seconds == 1) ? " second" : " seconds");
		gotone = 1;
	}
}

