ValeriF21
ValeriF21

Reputation: 474

Check for overlapping dates and time

int CompareDates(Date _date1, Date _date2)
{
    char *date1, *date2;
    int res = 0;
    date1 = ConvertDateToString(_date1);
    date2 = ConvertDateToString(_date2);

    res = strcmp(date2, date1);

    free(date1);
    free(date2);

    return res;
}

I've got an array of structures with date, type of 3 integers (dd/mm/yy), and a start time and end time.

And if I want to add another "meeting," I need to check if there is no overlap with another one - I mean check by date and time.

When I use the function above, it has bugs and doesn't work well. What is the most efficient way to compare to dates and, if they equal, compare the time also?

Is there any function that could do it for me? (If there is a way that I can compare a date string, that's okay too.)

Upvotes: 1

Views: 68

Answers (2)

Adrian Mole
Adrian Mole

Reputation: 51894

You'd be better off comparing each int member of the given Date structures, rather than converting to strings and then comparing those. So, assuming your Date structure is defined like this:

typedef struct {
    int day;
    int month;
    int year;
} Date;

you could have this for your comparison function:

int CompareDates(Date _date1, Date _date2)
{
    // First, compare the two years...
    if (_date1.year < _date2.year) return -1;
    else if (_date1.year > _date2.year) return 1;
    // Years are equal: compare months...
    if (_date1.month < _date2.month) return -1;
    else if (_date1.month > _date2.month) return 1;
    // Years AND months equal: compare days...
    if (_date1.day < _date2.day) return -1;
    else if (_date1.day > _date2.day) return 1;
    // All three are equal: return ZERO...
    return 0;
}

This may seem/look a little 'clumsy', but it's almost certainly more efficient than having to create two strings, format them, then free those strings.

Feel free to ask for further clarification and/or explanation.

Upvotes: 4

mevets
mevets

Reputation: 10445

A subtle brilliance in UNIX/C was in defining time as the number of seconds since an agreed upon epoch [ jan 01/ 1970 ]. Somewhat less than brilliant was that the UNIX definition would endure for now 50 years, and is within 2 decades of causing a major disruption....

Aside, the idea of having a monotonically increasing counter, so that any two (date, time) pairs could be reasonably compared was a massive step forward in computer science. For most of computings history, its practitioners have been particularly confused by the concept and purpose of a date. This simple definition gave them something they can work on.

An integral representation does not just make it easy to determine which date.time precedes another; it also permits ranges of [date.time, date.time] to be compared in terms of before, after, overlapping, etc...

So, rather than worrying too much about how dates are supposed to work, what your responsibilities are, and all that bother, convert them to numbers, compare them and report on the results. Just like a good little computer person.

Upvotes: 3

Related Questions