calyearstart.c revision 290001
1/*
2 * calyearstart - determine the NTP time at midnight of January 1 in
3 *		  the year of the given date.
4 */
5#include <config.h>
6#include <sys/types.h>
7
8#include "ntp_types.h"
9#include "ntp_calendar.h"
10#include "ntp_stdlib.h"
11#include "ntp_assert.h"
12
13/*
14 * Juergen Perlinger, 2010-05-02
15 *
16 * Redone in terms of the calendar functions. It's rather simple:
17 * - expand the NTP time stamp
18 * - split into days and seconds since midnight, dropping the partial day
19 * - get full number of days before year start in NTP epoch
20 * - convert to seconds, truncated to 32 bits.
21 */
22u_int32
23calyearstart(u_int32 ntptime, const time_t *pivot)
24{
25	u_int32      ndays; /* elapsed days since NTP starts */
26	vint64       vlong;
27	ntpcal_split split;
28
29	vlong = ntpcal_ntp_to_ntp(ntptime, pivot);
30	split = ntpcal_daysplit(&vlong);
31	ndays = ntpcal_rd_to_ystart(split.hi + DAY_NTP_STARTS)
32	      - DAY_NTP_STARTS;
33
34	return (u_int32)(ndays * SECSPERDAY);
35}
36
37/*
38 * calmonthstart - get NTP time at midnight of the first day of the
39 * current month.
40 */
41u_int32
42calmonthstart(u_int32 ntptime, const time_t *pivot)
43{
44	u_int32      ndays; /* elapsed days since NTP starts */
45	vint64       vlong;
46	ntpcal_split split;
47
48	vlong = ntpcal_ntp_to_ntp(ntptime, pivot);
49	split = ntpcal_daysplit(&vlong);
50	ndays = ntpcal_rd_to_mstart(split.hi + DAY_NTP_STARTS)
51	      - DAY_NTP_STARTS;
52
53	return (u_int32)(ndays * SECSPERDAY);
54}
55
56/*
57 * calweekstart - get NTP time at midnight of the last monday on or
58 * before the current date.
59 */
60u_int32
61calweekstart(u_int32 ntptime, const time_t *pivot)
62{
63	u_int32      ndays; /* elapsed days since NTP starts */
64	vint64       vlong;
65	ntpcal_split split;
66
67	vlong = ntpcal_ntp_to_ntp(ntptime, pivot);
68	split = ntpcal_daysplit(&vlong);
69	ndays = ntpcal_weekday_le(split.hi + DAY_NTP_STARTS, CAL_MONDAY)
70	      - DAY_NTP_STARTS;
71
72	return (u_int32)(ndays * SECSPERDAY);
73}
74
75/*
76 * caldaystart - get NTP time at midnight of the current day.
77 */
78u_int32
79caldaystart(u_int32 ntptime, const time_t *pivot)
80{
81	vint64       vlong;
82	ntpcal_split split;
83
84	vlong = ntpcal_ntp_to_ntp(ntptime, pivot);
85	split = ntpcal_daysplit(&vlong);
86
87	return ntptime - split.lo;
88}
89