FOUND Coverage Report


src/
File: common/time/time.cpp
Date: 2025-11-20 21:57:26
Lines:
43/43
100.0%
Functions:
8/8
100.0%
Branches:
2/2
100.0%

Line Branch Exec Source
1 #include "common/time/time.hpp"
2
3 #include <ctime>
4
5 #include "common/decimal.hpp"
6
7 // NOTE: We use a .cpp file instead of just putting these into
8 // the header file as inlined functions since these functions
9 // can get more complex and can be modified with different
10 // spacecraft requirements. For instance, the functions that
11 // obtain UTC and UT1 time can be modified to use an onboard
12 // atomic clock or GPS reciever.
13
14 // NOTE: To use approximations for getting the Julian Datetime
15 // and GMST for the current time, define the macro FAST
16 // #define FAST
17
18 namespace found {
19
20 18 DateTime getUTCTime() {
21 18 std::time_t now = std::time(nullptr);
22 18 std::tm* utc_time = std::gmtime(&now);
23
24 return {
25 now,
26 18 static_cast<int>(utc_time->tm_year + 1900), // tm_year is years since 1900
27 18 static_cast<int>(utc_time->tm_mon + 1), // tm_mon is months since January (0-11)
28 18 static_cast<int>(utc_time->tm_mday), // tm_mday is day of the month (1-31)
29 18 static_cast<int>(utc_time->tm_hour), // tm_hour is hours since midnight (0-23)
30 18 static_cast<int>(utc_time->tm_min), // tm_min is minutes after the hour (0-59)
31 18 static_cast<int>(utc_time->tm_sec) // tm_sec is seconds after the minute (0-60)
32 18 };
33 }
34
35 17 DateTime getUT1Time() {
36 // TODO: For simplicity, we return an approximation of UT1,
37 // whose formula is UT1 = UTC + Delta UT1. This should be
38 // replaced with either a table lookup or some curve function.
39 //
40 // Based on https://maia.usno.navy.mil/ser7/ser7.dat, which
41 // provides Delta UT1 values from 2025 to 2026, the average is
42 // 0.087497 seconds, so we use that as a rough approximation.
43 17 DateTime now = getUTCTime();
44 17 now.epochs += AVG_DELTA_UT1; // Adjust for UT1, this is a rough approximation
45 17 now.second += AVG_DELTA_UT1; // Adjust seconds accordingly
46 17 return now;
47 }
48
49 9 decimal getJulianDateTime(DateTime &time) {
50 // Purportedly, the Julian date is also
51 // time.epochs / 86400.0 + 2440587.5, but
52 // this is apparently just an approximation.
53
54 // The below formula is from https://aa.usno.navy.mil/faq/JD_formula
55 9 int64_t A = 367 * time.year;
56 9 int64_t B = (7 * (time.year + (time.month + 9) / 12)) / 4;
57 9 int64_t C = (275 * time.month) / 9;
58 9 decimal D = time.day + 1721013.5;
59 9 decimal julianDate = A - B + C + D;
60
61 9 return julianDate + time.hour / DECIMAL(24.0) +
62 18 time.minute / DECIMAL(1440.0) + time.second / DECIMAL(86400.0) -
63
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2 times.
9 DECIMAL(0.5) * (100 * time.year + time.month > 190002.5 ? 1 : -1) +
64 9 DECIMAL(0.5);
65 }
66
67 1 decimal getCurrentJulianDateTime() {
68 #ifdef FAST
69 // If FAST is defined, we use the approximation
70 return getJulianDateTime(std::time(nullptr));
71 #else
72 1 DateTime time = getUT1Time();
73 1 return getJulianDateTime(time);
74 #endif
75 }
76
77 2 decimal getJulianDateTime(std::time_t epochs) {
78 2 return epochs / 86400.0 + 2440587.5;
79 }
80
81 3 decimal getGreenwichMeanSiderealTime(DateTime &time) {
82 // Purportedly, the Greenwich Mean Sidereal Time (GMST)
83 // is also 15(18.697374558 + 24.06570982441908 * (JDT - 2451545.0))
84 // in degrees
85
86 // The below formula is from http://tiny.cc/4wal001
87 3 decimal JDT = getJulianDateTime(time);
88 3 decimal D_tt = JDT - DECIMAL(2451545.0); // Julian date - J2000.0
89 3 decimal t = D_tt / DECIMAL(36525.0); // Julian centuries since J2000.0
90
91 3 return DECIMAL(280.46061837) +
92 3 DECIMAL(360.98564736629) * D_tt +
93 3 (DECIMAL(0.000387933) * t * t) -
94 3 (t * t * t / DECIMAL(38710000.0));
95 }
96
97 1 decimal getCurrentGreenwichMeanSiderealTime() {
98 #ifdef FAST
99 // If FAST is defined, we use the approximation
100 return getGreenwichMeanSiderealTime(std::time(nullptr));
101 #else
102 1 DateTime time = getUT1Time();
103 1 return getGreenwichMeanSiderealTime(time);
104 #endif
105 }
106
107 1 decimal getGreenwichMeanSiderealTime(std::time_t epochs) {
108 1 return 15 * (DECIMAL(18.697374558) + DECIMAL(24.06570982441908) * (getJulianDateTime(epochs) - DECIMAL(2451545.0)));
109 }
110
111 } // namespace found
112