TimeConverters_T.cpp
Go to the documentation of this file.
1 //==============================================================================
2 //
3 // This file is part of GNSSTk, the ARL:UT GNSS Toolkit.
4 //
5 // The GNSSTk is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation; either version 3.0 of the License, or
8 // any later version.
9 //
10 // The GNSSTk is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with GNSSTk; if not, write to the Free Software Foundation,
17 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18 //
19 // This software was developed by Applied Research Laboratories at the
20 // University of Texas at Austin.
21 // Copyright 2004-2022, The Board of Regents of The University of Texas System
22 //
23 //==============================================================================
24 
25 //==============================================================================
26 //
27 // This software was developed by Applied Research Laboratories at the
28 // University of Texas at Austin, under contract to an agency or agencies
29 // within the U.S. Department of Defense. The U.S. Government retains all
30 // rights to use, duplicate, distribute, disclose, or release this software.
31 //
32 // Pursuant to DoD Directive 523024
33 //
34 // DISTRIBUTION STATEMENT A: This software has been approved for public
35 // release, distribution is unlimited.
36 //
37 //==============================================================================
38 
39 #include "TimeConverters.hpp"
40 #include "TestUtil.hpp"
41 #include <iostream>
42 #include <fstream>
43 #include <cmath>
44 using namespace gnsstk;
45 
46 
48 {
49  public:
50  xTimeConverters(){eps = 1e-12;}// Default Constructor, set the precision value
51  ~xTimeConverters() {} // Default Desructor
52 
53 //==========================================================================================================================
54 // Julian Date (JD) to Calendar Date Tests
55 //==========================================================================================================================
57  {
58  TestUtil testFramework( "TimeConverters", "convertJDtoCalendar", __FILE__, __LINE__ );
59 
60  int year, month, day;
61  int inputJD[8] = {2453971, 2299159, 2342032, 2377095, 1721118, 1721424, 1648549, 1719657};
62  int expectedYear[8] = { 2006, 1582, 1700, 1796, -1, 1, -200, -5};
63  int expectedMonth[8] = { 8, 10, 3, 2, 3, 1, 6, 3};
64  int expectedDay[8] = { 23, 3, 1, 29, 1, 1, 25, 1};
65 
66 
67  for (int i = 0; i < 8; i++)
68  {
69  convertJDtoCalendar(inputJD[i],year,month,day);
70  //---------------------------------------------------------------------
71  //Was the correct calendar day found for the above JD?
72  //---------------------------------------------------------------------
73  testFramework.assert(expectedYear[i] == year , "The year from the JD conversion was not correct", __LINE__);
74  testFramework.assert(expectedMonth[i] == month, "The month from the JD conversion was not correct", __LINE__);
75  testFramework.assert(expectedDay[i] == day , "The day from the JD conversion was not correct", __LINE__);
76  }
77 
78  return testFramework.countFails();
79  }
80 
81 
82 //==========================================================================================================================
83 // Calendar to JD tests
84 //==========================================================================================================================
86  {
87  TestUtil testFramework( "TimeConverters", "convertCalendarToJD", __FILE__, __LINE__ );
88 
89 
90  long JD;
91  int expectedJD[8] = {2453971, 2299159, 2342032, 2377095, 1721118, 1721424, 1648549, 1719657};
92  int inputYear[8] = { 2006, 1582, 1700, 1796, -1, 1, -200, -5};
93  int inputMonth[8] = { 8, 10, 3, 2, 3, 1, 6, 3};
94  int inputDay[8] = { 23, 3, 1, 29, 1, 1, 25, 1};
95 
96  for (int i = 0 ; i < 8; i++)
97  {
98  JD = convertCalendarToJD(inputYear[i],inputMonth[i],inputDay[i]);
99  //---------------------------------------------------------------------
100  //Was the correct JD found for the above Calendar day?
101  //---------------------------------------------------------------------
102  testFramework.assert(expectedJD[i] == JD, "The JD found from the calendar-JD conversion was not correct", __LINE__);
103  }
104 
105  return testFramework.countFails();
106  }
107 
108 
109 //==========================================================================================================================
110 // Seconds of Day (SOD) to Time Tests
111 //==========================================================================================================================
113  {
114  TestUtil testFramework( "TimeConverters", "convertSODToTime", __FILE__, __LINE__ );
115 
116 
117  int hour;
118  int minute;
119  double second, relativeError;
120  double inputSOD[3] = { -0.1, 86401.11, 12345.67};
121  int expectedHour[3] = { 23, 0, 3};
122  int expectedMinute[3] = { 59, 0, 25};
123  double expectedSecond[3] = { 59.9, 1.11, 45.67};
124 
125  for (int i = 0 ; i < 3; i++)
126  {
127  convertSODtoTime(inputSOD[i],hour,minute,second);
128  //---------------------------------------------------------------------
129  //Was the correct Time found for the above SOD?
130  //---------------------------------------------------------------------
131  relativeError = fabs(expectedSecond[i]-second)/fabs(expectedSecond[i]);
132  testFramework.assert(expectedHour[i] == hour , "The SOD to Time conversion found an incorrect hour" , __LINE__ );
133  testFramework.assert(expectedMinute[i] == minute, "The SOD to Time conversion found an incorrect minute", __LINE__ );
134  testFramework.assert(relativeError < eps , "The SOD to Time conversion found an incorrect second", __LINE__ );
135  }
136 
137  return testFramework.countFails();
138  }
139 
140 
141 //==========================================================================================================================
142 // Time to SOD Tests
143 //==========================================================================================================================
145  {
146 
147 
148  TestUtil testFramework( "TimeConverters", "convertTimeToSOD", __FILE__, __LINE__ );
149  int hour, minute;
150  double second, SOD, relativeError;;
151  double expectedSOD[3] = {4230.5, 86399.99, 12345.67};
152  int inputHour[3] = { 1, 23, 3};
153  int inputMinute[3] = { 10, 59, 25};
154  double inputSecond[3] = { 30.5, 59.99, 45.67};
155 
156  for (int i = 0 ; i < 3; i++)
157  {
158  SOD = convertTimeToSOD(inputHour[i],inputMinute[i],inputSecond[i]);
159  //---------------------------------------------------------------------
160  //Was the correct SOD found for the above Time?
161  //---------------------------------------------------------------------
162  relativeError = fabs(expectedSOD[i]-SOD)/fabs(expectedSOD[i]);
163  testFramework.assert(relativeError < eps, "The Time to SOD conversion found an incorrect SOD", __LINE__ );
164  }
165 
166  return testFramework.countFails();
167  }
168  private:
169  double eps;
170 };
171 
172 int main() //Main function to initialize and run all tests above
173 {
174  int check, errorCounter = 0;
175  xTimeConverters testClass;
176 
177  check = testClass.JDtoCalendarTest();
178  errorCounter += check;
179 
180  check = testClass.CalendartoJDTest();
181  errorCounter += check;
182 
183  check = testClass.SODtoTimeTest();
184  errorCounter += check;
185 
186  check = testClass.TimetoSODTest();
187  errorCounter += check;
188 
189  std::cout << "Total Errors for " << __FILE__<<": "<< errorCounter << std::endl;
190 
191  return errorCounter; //Return the total number of errors
192 }
xTimeConverters::CalendartoJDTest
int CalendartoJDTest()
Definition: TimeConverters_T.cpp:85
gnsstk::TestUtil::countFails
int countFails(void)
Definition: TestUtil.hpp:771
example6.day
day
Definition: example6.py:66
gnsstk::TestUtil::assert
void assert(bool testExpression, const std::string &testMsg, const int lineNumber)
Definition: TestUtil.hpp:607
xTimeConverters::SODtoTimeTest
int SODtoTimeTest()
Definition: TimeConverters_T.cpp:112
example6.year
year
Definition: example6.py:64
xTimeConverters::~xTimeConverters
~xTimeConverters()
Definition: TimeConverters_T.cpp:51
xTimeConverters::TimetoSODTest
int TimetoSODTest()
Definition: TimeConverters_T.cpp:144
example6.hour
hour
Definition: example6.py:67
example6.minute
minute
Definition: example6.py:68
xTimeConverters::eps
double eps
Definition: TimeConverters_T.cpp:169
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
example6.second
second
Definition: example6.py:69
TestUtil.hpp
gnsstk::convertJDtoCalendar
void convertJDtoCalendar(long jd, int &iyear, int &imonth, int &iday)
Definition: TimeConverters.cpp:52
gnsstk::convertSODtoTime
void convertSODtoTime(double sod, int &hh, int &mm, double &sec)
Definition: TimeConverters.cpp:149
xTimeConverters::xTimeConverters
xTimeConverters()
Definition: TimeConverters_T.cpp:50
xTimeConverters
Definition: TimeConverters_T.cpp:47
TimeConverters.hpp
xTimeConverters::JDtoCalendarTest
int JDtoCalendarTest()
Definition: TimeConverters_T.cpp:56
main
int main()
Definition: TimeConverters_T.cpp:172
gnsstk::convertCalendarToJD
long convertCalendarToJD(int yy, int mm, int dd)
Definition: TimeConverters.cpp:100
example6.month
month
Definition: example6.py:65
gnsstk::convertTimeToSOD
double convertTimeToSOD(int hh, int mm, double sec)
Definition: TimeConverters.cpp:175
gnsstk::TestUtil
Definition: TestUtil.hpp:265


gnsstk
Author(s):
autogenerated on Wed Oct 25 2023 02:40:42