PageRenderTime 26ms CodeModel.GetById 13ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

/contrib/ntp/libntp/ymd2yd.c

https://bitbucket.org/freebsd/freebsd-head/
C | 37 lines | 25 code | 5 blank | 7 comment | 11 complexity | d789ca103b91677e335e20b5de660752 MD5 | raw file
 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 */
13static int day1tab[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
14static int day2tab[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
15
16int
17ymd2yd(
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}