4
  • Any year evenly divisible by 400 is a leap year (e.g., 2000 was a leap year).
  • Any other year evenly divisible by 100 is not a leap year (e.g., 1700, 1800 and 1900 were not leap years).
  • Any other year evenly divisible by 4 is a leap year (e.g., 1996 and 2004 are leap years).

But I'm not sure how to make nested if states in my c-program that would produce the right answer...

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user133466
  • 3,391
  • 18
  • 63
  • 92

6 Answers6

13

Convert them both to UNIX epoch time and subtract the difference.

UNIX Epoch time is the total number of seconds for a date since 1 January 1970 00:00:00.0

Once you've got the number of seconds, you divide that difference by the number of seconds in a day (which is 24 hours * 60 minutes * 60 seconds = 86400 seconds).

mauris
  • 42,982
  • 15
  • 99
  • 131
Rap
  • 6,851
  • 3
  • 50
  • 88
  • 8
    Yes, yes, yes. Do this. DO NOT muck with calendrics if you value your sanity. The length of a day is fixed (if you ignore leap seconds, which you should because software does). The epoch timeline is a simple scalar number. This is the representation you want. Any answer involving calendar terms is wrong. – Andy Ross Sep 04 '09 at 23:01
  • 1
    DST in local time in much of the world has days that vary in length by an hour. If you're calculating in seconds, and dividing by 86400, I'd be careful to round, as appose to floor. – JasonWoof Sep 05 '09 at 00:36
  • Andy, beware of the year 2038 problem, etc. And you don't get dates before mid-December 1901, either. – Robert L Sep 05 '09 at 05:16
  • And I am not sane, so I muck about with calendrics. – Robert L Sep 05 '09 at 05:17
2

I would convert the two dates in Julian day and then do the difference, but I would have to check if this is a good solution with no drawbacks first. I suggest you to do the same.

Stefano Borini
  • 138,652
  • 96
  • 297
  • 431
2

Find one of the libraries that converts dates into 'number of days since some epoch' (often called a Julian day - though there are official rules for the astronomical Julian Date, and Modified Julian Date). Then convert each date into the relevant number, and take the difference. Assuming the library handles leap years, so does your answer.

For ideas on one such set of code, see 'Calendrical Calculations'.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
1

Leap year algorithm from wikipedia:

Pseudocode 1:

if year modulo 400 is 0 then leap
 else if year modulo 100 is 0 then no_leap
 else if year modulo 4 is 0 then leap
 else no_leap

Pseudocode 2:

function isLeapYear (year):
  if ((year modulo 4 is 0) and (year modulo 100 is not 0)) or (year modulo 400 is 0)
   then true
  else false
shufler
  • 926
  • 7
  • 23
1
#include <time.h>
#define SECONDS_PER_DAY (24 * 60 * 60)

time_t time_from_date(int year, unsigned month, unsigned day)
{
    return mktime(&(struct tm){
        .tm_year = year - 1900, .tm_mon = month - 1, .tm_mday = day });
}

int days_between(int year0, unsigned month0, unsigned day0,
    int year1, unsigned month1, unsigned day1)
{
    return difftime(time_from_date(year1, month1, day1),
        time_from_date(year0, month0, day0)) / SECONDS_PER_DAY;
}
Christoph
  • 164,997
  • 36
  • 182
  • 240
  • @Stefano: yes it is, as I used a compound literal with named initializers as argument to `mktime()`; for the C90 version, declare the struct in the function and set the members individually – Christoph Sep 05 '09 at 13:07
0

You don't need nested conditionals at all for this.

Hint: instead of trying to determine whether a given year is a leap year, try to determine the total number of leap days that came before it.

Robert L
  • 1,963
  • 2
  • 13
  • 11