]> pd.if.org Git - pdclib/blob - functions/_PDCLIB/_PDCLIB_is_leap.c
ISO week calculation.
[pdclib] / functions / _PDCLIB / _PDCLIB_is_leap.c
1 /* _PDCLIB_is_leap( int )
2
3    This file is part of the Public Domain C Library (PDCLib).
4    Permission is granted to use, modify, and / or redistribute at will.
5 */
6
7 #include "_PDCLIB_int.h"
8
9 #ifndef REGTEST
10
11 int _PDCLIB_is_leap( int year_offset )
12 {
13     /* year given as offset from 1900, matching tm.tm_year in <time.h> */
14     long long year = year_offset + 1900ll;
15     return ( ( year % 4 ) == 0 && ( ( year % 25 ) != 0 || ( year % 400 ) == 0 ) );
16 }
17
18 #endif
19
20 #ifdef TEST
21
22 #include "_PDCLIB_test.h"
23
24 int main( void )
25 {
26 #ifndef REGTEST
27     /* 1901 not leap */
28     TESTCASE( ! _PDCLIB_is_leap( 1 ) );
29     /* 1904 leap */
30     TESTCASE( _PDCLIB_is_leap( 4 ) );
31     /* 1900 not leap */
32     TESTCASE( ! _PDCLIB_is_leap( 0 ) );
33     /* 2000 leap */
34     TESTCASE( _PDCLIB_is_leap( 100 ) );
35 #endif
36     return TEST_RESULTS;
37 }
38
39 #endif