0

I need some help in doing a comparison of dates. The program works when the user enters the input date and it will compare it with a text document file on its date. If the dates is between the user enter date and it matches the document date then it will be executed.. Im thinking of using a switch case for this case instead of if.. is it possible?

if (date_compare(tld->begin, d) > 0 && //compare user input start date with actual date.txt begin input date
        date_compare(tld->end, d) < 0) //compare user input end date with actual date.txt input date
    return 0;

1 Answers1

0

I would consider using some library for date arithmetic and comparison. It can be very tricky to properly code it for a serious production code, especially with locales. Starting with leap years, through leap seconds, into pearls like one of the top questions Why is subtracting these two times (in 1927) giving a strange result?.

For simple starters I would recommend using standard library difftime from chrono utilities.

The if you showed seems fine for me, and you can easily wrap difftime with date_compare.

Two concerns though.

What is the order of operands for your date_compare? For difftime the signature is double difftime( time_t time_end, time_t time_beg );, which would seem to be opposite to what you use. Otherwise the statement would logically be always false.

Also you seem to check whether d is inside (tld->start; tld->begin) and if so you exit with 0. In other words, you return 0 when the file fits into the date interval, I hope this is intended.

Community
  • 1
  • 1
luk32
  • 15,812
  • 38
  • 62