File: gpstolfp.c

package info (click to toggle)
ntpsec 1.2.3%2Bdfsg1-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,960 kB
  • sloc: ansic: 62,004; python: 32,017; sh: 1,553; yacc: 1,293; makefile: 190; javascript: 138
file content (103 lines) | stat: -rw-r--r-- 1,930 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * Copyright Frank Kardel <kardel@ntp.org>
 * Copyright the NTPsec project contributors
 * SPDX-License-Identifier: BSD-3-Clause
 */
#include "config.h"
#include "ntp_fp.h"
#include "ntp_calendar.h"
#include "gpstolfp.h"

#define GPSORIGIN       2524953600u  /* GPS origin - NTP origin in seconds */

void
gpstolfp(
	 int weeks,
	 int days,
	 unsigned long  seconds,
	 l_fp * lfp
	 )
{
	if (weeks < GPSWRAP)
	{
		weeks += GPSWEEKS;
	}

	/* convert to NTP time, note no fractional seconds */
	*lfp = lfptouint((uint64_t)weeks * SECSPERWEEK
			 + (uint64_t)days * SECSPERDAY
			 + (uint64_t)seconds
			 + GPSORIGIN);
	setlfpfrac(*lfp, 0);
}


void
gpsweekadj(
	unsigned int * week,
	unsigned int build_week
	)
{
	/* adjust for rollover */
	while (*week < build_week) {
		*week += GPSWEEKS;
	}
}


void
gpstocal(
	unsigned int week,
	unsigned long int TOW,
	int UTC_offset,
	struct calendar * out
	)
{
	time64_t t;

	t = (time64_t)((int64_t)GPSORIGIN - UTC_offset);
	t += (time64_t)week * SECSPERWEEK;
	t += (time64_t)TOW;

	ntpcal_ntp64_to_date(out, t);
}


void
caltogps(
	const struct calendar * in,
	int UTC_offset,
	unsigned int * week,
	unsigned long int * TOW
	)
{
	time64_t t;

	t = ntpcal_dayjoin(ntpcal_date_to_rd(in) - DAY_NTP_STARTS,
	                             ntpcal_date_to_daysec(in));
	t -= (uint64_t)((int64_t)GPSORIGIN - UTC_offset);
	*week = (unsigned int)(t / SECSPERWEEK);
	if (NULL != TOW) {
		*TOW = (unsigned long int)(t % SECSPERWEEK);
	}
}

/*
 * History:
 *
 * gpstolfp.c,v
 * Revision 4.8  2005/04/16 17:32:10  kardel
 * update copyright
 *
 * Revision 4.7  2004/11/14 15:29:41  kardel
 * support PPSAPI, upgrade Copyright to Berkeley style
 *
 * Revision 4.3  1999/02/28 11:42:44  kardel
 * (GPSWRAP): update GPS rollover to 990 weeks
 *
 * Revision 4.2  1998/07/11 10:05:25  kardel
 * Release 4.0.73d reconciliation
 *
 * Revision 4.1  1998/06/28 16:47:15  kardel
 * added gpstolfp() function
 */