xref: /freebsd/contrib/ntp/libntp/ymd2yd.c (revision 4f29da19bd44f0e99f021510460a81bf754c21d2)
1 /*
2  * ymd2yd - compute the date in the year from y/m/d
3  */
4 
5 #include "ntp_fp.h"
6 #include "ntp_unixtime.h"
7 #include "ntp_stdlib.h"
8 
9 /*
10  * Tables to compute the day of year from yyyymmdd timecode.
11  * Viva la leap.
12  */
13 static int day1tab[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
14 static int day2tab[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
15 
16 int
17 ymd2yd(
18 	int y,
19 	int m,
20 	int d
21 	)
22 {
23 	int i, *t;
24 
25 	if (m < 1 || m > 12 || d < 1)
26 		return (-1);
27 
28 	if (((y%4 == 0) && (y%100 != 0)) || (y%400 == 0))
29 		t = day2tab;	/* leap year */
30 	else
31 		t = day1tab;	/* not a leap year */
32 	if (d > t[m - 1])
33 		return (-1);
34 	for (i = 0; i < m - 1; i++)
35 		d += t[i];
36 	return d;
37 }
38