/* File time.c.  The ClockTime ADT implemented using a record.*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "bool.h"
#include "time.h"


/* Make
 *----------------------------------------------------------------
 */
ClockTime
MakeTime(int hour, int minute)
{
    ClockTime t;
    t.hour = hour;
    t.minute = minute;
    return t;
}


/* MakeTimeFromString
 *----------------------------------------------------------------
 */
ClockTime
MakeTimeFromString(char *stime)
{
    ClockTime t;
    char *ptr;
    ptr = strchr(stime, ':');
    assert(ptr != NULL);
    *ptr = '\0';
    ptr++;
    t.hour = atoi(stime);
    t.minute = atoi(ptr);
    return t;
}


/* GetHour
 *----------------------------------------------------------------
 */
int
GetHour(ClockTime t)
{
    return t.hour;
}


/* GetMinute
 *----------------------------------------------------------------
 */
int
GetMinute(ClockTime t)
{
    return t.minute;
}


/* DisplayTime
 *----------------------------------------------------------------
 */
void
DisplayTime(ClockTime t)
{
        if(t.hour < 10)
	    printf("0");
	printf("%d:", t.hour);
        if(t.minute < 10)
	    printf("0");
        printf("%d", t.minute);
}


/* DisplayTimeString
 *----------------------------------------------------------------
 */
char *
DisplayTimeString(ClockTime t, char timeStr[])
{
    char mstr[3];
    int index = 0;
    /*create the hour part of the string in the final buffer*/
    if(t.hour < 10) {
	timeStr[index] = '0';
	index++;
    }
    sprintf(&timeStr[index], "%d:", t.hour);
    
    /*create the minutes part of the string in a separate array*/
    index = 0;
    if(t.minute < 10) {
	mstr[index] = '0';
	index++;
    }
    /*the following must be true for sprintf and strncat to work correctly*/
    assert(index <= 1 && t.minute < 100 && strlen(timeStr) == 3);
    
    sprintf(&mstr[index], "%d", t.minute);
    
    strncat(timeStr, mstr, 3); /*concatenate the two strings
			     We need to copy 3 characters to get the '\0' */
    return timeStr;
}

/* TimeGreaterThan
 *----------------------------------------------------------------
 */
boolean
TimeGreaterThan(ClockTime t1, ClockTime t2)
{
    return ((t1.hour > t2.hour) ||
	    (t1.hour == t2.hour && t1.minute > t2.minute));
}

/* TimeEqual
 *----------------------------------------------------------------
 */
boolean
TimeEqual(ClockTime t1, ClockTime t2)
{
        return (t1.hour == t2.hour && t1.minute == t2.minute);
}

/* TimeDifference
 *----------------------------------------------------------------
 */
ClockTime
TimeDifference(ClockTime t1, ClockTime t2)
{
    ClockTime resTime;
    assert(TimeGreaterThan(t2, t1) || TimeEqual (t2, t1));

    resTime.hour = t2.hour - t1.hour;
    
    if(t2.minute >= t1.minute)  /* Bug fix: This line changed */
	resTime.minute = t2.minute - t1.minute;
    else {
	resTime.minute = t2.minute + 60 - t1.minute;
	resTime.hour--;
    }
    
    return resTime;
}

/* TimeAdd
 *----------------------------------------------------------------
 */
ClockTime
TimeAdd(ClockTime t1, ClockTime t2)
{
        ClockTime resTime;

        resTime.hour = t2.hour + t1.hour;
        resTime.minute = t2.minute + t1.minute;
        if(resTime.minute >= 60) {
            resTime.hour++;
            resTime.minute -= 60;
	}
	
        return resTime;
}
