2 * ntp_calgps.h - calendar for GPS/GNSS based clocks
4 * Written by Juergen Perlinger (perlinger@ntp.org) for the NTP project.
5 * The contents of 'html/copyright.html' apply.
7 * --------------------------------------------------------------------
9 * This module implements stuff often used with GPS/GNSS receivers
16 #include "ntp_types.h"
18 #include "ntp_calendar.h"
20 /* GPS week calendar (extended weeks)
21 * We use weeks based on 1899-31-12, which was the last Sunday before
22 * the begin of the NTP epoch. (Which is equivalent to saying 1900-01-01
25 * We simply pre-calculate the offsets and cycle shifts for the real GPS
26 * calendar, which starts at 1980-01-06, to simplyfy some expressions.
28 * This has a fringe benefit that should not be overlooked: Since week zero
29 * is around 1900, and we should never have to deal with dates before
30 * 1970 or 1980, a week number of zero can be easily used to indicate
31 * an invalid week time stamp.
33 #define GPSNTP_WSHIFT 4175 /* weeks 1899-31-12 --> 1980-01-06 */
34 #define GPSNTP_WCYCLE 79 /* above, modulo 1024 */
35 #define GPSNTP_DSHIFT 1 /* day number of 1900-01-01 in week */
38 uint32_t weeks; /* weeks since GPS epoch */
39 int32_t wsecs; /* seconds since week start */
40 uint32_t frac; /* fractional seconds */
42 typedef struct gpsdatum TGpsDatum;
43 typedef struct gpsdatum const TcGpsDatum;
45 /* NTP date/time in split representation */
47 uint32_t days; /* since NTP epoch */
48 int32_t secs; /* since midnight, denorm is ok */
49 uint32_t frac; /* fractional seconds */
51 typedef struct ntpdatum TNtpDatum;
52 typedef struct ntpdatum const TcNtpDatum;
55 * GPS week/sec calendar functions
57 * see the implementation for details, especially the
58 * 'gpscal_from_weektime{1,2}()'
62 gpscal_fix_gps_era(TcGpsDatum *);
65 gpscal_add_offset(TGpsDatum *datum, l_fp offset);
68 gpscal_from_calendar_ex(TcCivilDate*, l_fp fofs, int/*BOOL*/ warp);
70 static inline TGpsDatum
71 gpscal_from_calendar(TcCivilDate *pCiv, l_fp fofs) {
72 return gpscal_from_calendar_ex(pCiv, fofs, TRUE);
75 extern TGpsDatum /* see source for semantic of the 'fofs' value! */
76 gpscal_from_gpsweek(uint16_t w, int32_t s, l_fp fofs);
79 gpscal_from_weektime1(int32_t wsecs, l_fp fofs, l_fp pivot);
82 gpscal_from_weektime2(int32_t wsecs, l_fp fofs, TcGpsDatum *pivot);
85 gpscal_to_calendar(TCivilDate*, TcGpsDatum*);
88 gpscal_from_gpsntp(TcNtpDatum*);
91 ntpfp_from_gpsdatum(TcGpsDatum *);
94 * NTP day/sec calendar functions
96 * see the implementation for details, especially the
97 * 'gpscal_from_daytime{1,2}()'
100 gpsntp_fix_gps_era(TcNtpDatum *);
103 gpsntp_add_offset(TNtpDatum *datum, l_fp offset);
106 gpsntp_from_calendar_ex(TcCivilDate*, l_fp fofs, int/*BOOL*/ warp);
108 static inline TNtpDatum
109 gpsntp_from_calendar(TcCivilDate * pCiv, l_fp fofs) {
110 return gpsntp_from_calendar_ex(pCiv, fofs, TRUE);
114 gpsntp_from_daytime1_ex(TcCivilDate *dt, l_fp fofs, l_fp pivot, int/*BOOL*/ warp);
116 static inline TNtpDatum
117 gpsntp_from_daytime1(TcCivilDate *dt, l_fp fofs, l_fp pivot) {
118 return gpsntp_from_daytime1_ex(dt, fofs, pivot, TRUE);
122 gpsntp_from_daytime2_ex(TcCivilDate *dt, l_fp fofs, TcNtpDatum *pivot, int/*BOOL*/ warp);
124 static inline TNtpDatum
125 gpsntp_from_daytime2(TcCivilDate *dt, l_fp fofs, TcNtpDatum *pivot) {
126 return gpsntp_from_daytime2_ex(dt, fofs, pivot, TRUE);
130 gpsntp_from_gpscal_ex(TcGpsDatum*, int/*BOOL*/ warp);
132 static inline TNtpDatum
133 gpsntp_from_gpscal(TcGpsDatum *wd) {
134 return gpsntp_from_gpscal_ex(wd, FALSE);
138 gpsntp_to_calendar(TCivilDate*, TcNtpDatum*);
141 ntpfp_from_ntpdatum(TcNtpDatum*);
147 /* apply fudge to time stamp: *SUBTRACT* the given offset from an l_fp*/
149 ntpfp_with_fudge(l_fp lfp, double ofs);
151 #endif /*!defined(NTP_CALGPS_H)*/