154359Sroberto/*
254359Sroberto * calyearstart - determine the NTP time at midnight of January 1 in
354359Sroberto *		  the year of the given date.
454359Sroberto */
5285612Sdelphij#include <config.h>
654359Sroberto#include <sys/types.h>
754359Sroberto
854359Sroberto#include "ntp_types.h"
954359Sroberto#include "ntp_calendar.h"
1054359Sroberto#include "ntp_stdlib.h"
11285612Sdelphij#include "ntp_assert.h"
1254359Sroberto
13285612Sdelphij/*
14285612Sdelphij * Juergen Perlinger, 2010-05-02
15285612Sdelphij *
16285612Sdelphij * Redone in terms of the calendar functions. It's rather simple:
17285612Sdelphij * - expand the NTP time stamp
18285612Sdelphij * - split into days and seconds since midnight, dropping the partial day
19285612Sdelphij * - get full number of days before year start in NTP epoch
20285612Sdelphij * - convert to seconds, truncated to 32 bits.
21285612Sdelphij */
22285612Sdelphiju_int32
23285612Sdelphijcalyearstart(u_int32 ntptime, const time_t *pivot)
2454359Sroberto{
25285612Sdelphij	u_int32      ndays; /* elapsed days since NTP starts */
26285612Sdelphij	vint64       vlong;
27285612Sdelphij	ntpcal_split split;
2854359Sroberto
29285612Sdelphij	vlong = ntpcal_ntp_to_ntp(ntptime, pivot);
30285612Sdelphij	split = ntpcal_daysplit(&vlong);
31285612Sdelphij	ndays = ntpcal_rd_to_ystart(split.hi + DAY_NTP_STARTS)
32285612Sdelphij	      - DAY_NTP_STARTS;
33285612Sdelphij
34285612Sdelphij	return (u_int32)(ndays * SECSPERDAY);
3554359Sroberto}
36285612Sdelphij
37285612Sdelphij/*
38285612Sdelphij * calmonthstart - get NTP time at midnight of the first day of the
39285612Sdelphij * current month.
40285612Sdelphij */
41285612Sdelphiju_int32
42285612Sdelphijcalmonthstart(u_int32 ntptime, const time_t *pivot)
43285612Sdelphij{
44285612Sdelphij	u_int32      ndays; /* elapsed days since NTP starts */
45285612Sdelphij	vint64       vlong;
46285612Sdelphij	ntpcal_split split;
47285612Sdelphij
48285612Sdelphij	vlong = ntpcal_ntp_to_ntp(ntptime, pivot);
49285612Sdelphij	split = ntpcal_daysplit(&vlong);
50285612Sdelphij	ndays = ntpcal_rd_to_mstart(split.hi + DAY_NTP_STARTS)
51285612Sdelphij	      - DAY_NTP_STARTS;
52285612Sdelphij
53285612Sdelphij	return (u_int32)(ndays * SECSPERDAY);
54285612Sdelphij}
55285612Sdelphij
56285612Sdelphij/*
57285612Sdelphij * calweekstart - get NTP time at midnight of the last monday on or
58285612Sdelphij * before the current date.
59285612Sdelphij */
60285612Sdelphiju_int32
61285612Sdelphijcalweekstart(u_int32 ntptime, const time_t *pivot)
62285612Sdelphij{
63285612Sdelphij	u_int32      ndays; /* elapsed days since NTP starts */
64285612Sdelphij	vint64       vlong;
65285612Sdelphij	ntpcal_split split;
66285612Sdelphij
67285612Sdelphij	vlong = ntpcal_ntp_to_ntp(ntptime, pivot);
68285612Sdelphij	split = ntpcal_daysplit(&vlong);
69285612Sdelphij	ndays = ntpcal_weekday_le(split.hi + DAY_NTP_STARTS, CAL_MONDAY)
70285612Sdelphij	      - DAY_NTP_STARTS;
71285612Sdelphij
72285612Sdelphij	return (u_int32)(ndays * SECSPERDAY);
73285612Sdelphij}
74285612Sdelphij
75285612Sdelphij/*
76285612Sdelphij * caldaystart - get NTP time at midnight of the current day.
77285612Sdelphij */
78285612Sdelphiju_int32
79285612Sdelphijcaldaystart(u_int32 ntptime, const time_t *pivot)
80285612Sdelphij{
81285612Sdelphij	vint64       vlong;
82285612Sdelphij	ntpcal_split split;
83285612Sdelphij
84285612Sdelphij	vlong = ntpcal_ntp_to_ntp(ntptime, pivot);
85285612Sdelphij	split = ntpcal_daysplit(&vlong);
86285612Sdelphij
87285612Sdelphij	return ntptime - split.lo;
88285612Sdelphij}
89