TimeConverters.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 "TimeConstants.hpp"
41 #include <math.h>
42 
43 namespace gnsstk
44 {
45 
46  // These two routines convert 'integer JD' and calendar time; they were
47  // derived from Sinnott, R. W. "Bits and Bytes" Sky & Telescope Magazine,
48  // Vol 82, p. 183, August 1991, and The Astronomical Almanac, published
49  // by the U.S. Naval Observatory.
50  // NB range of applicability of this routine is from 0JD (4713BC)
51  // to approx 3442448JD (4713AD).
52  void convertJDtoCalendar( long jd,
53  int& iyear,
54  int& imonth,
55  int& iday )
56  {
57  long L, M, N, P, Q;
58  if(jd > 2299160) // after Oct 4, 1582
59  {
60  L = jd + 68569;
61  M = (4 * L) / 146097;
62  L = L - ((146097 * M + 3) / 4);
63  N = (4000 * (L + 1)) / 1461001;
64  L = L - ((1461 * N) / 4) + 31;
65  P = (80 * L) / 2447;
66  iday = int(L - (2447 * P) / 80);
67  L = P / 11;
68  imonth = int(P + 2 - 12 * L);
69  iyear = int(100 * (M - 49) + N + L);
70  }
71  else
72  {
73  P = jd + 1402;
74  Q = (P - 1) / 1461;
75  L = P - 1461 * Q;
76  M = (L - 1) / 365 - L / 1461;
77  N = L - 365 * M + 30;
78  P = (80 * N) / 2447;
79  iday = int(N - (2447 * P) / 80);
80  N = P / 11;
81  imonth = int(P + 2 - 12 * N);
82  iyear = int(4 * Q + M + N - 4716);
83  if(iyear <= 0)
84  {
85  --iyear;
86  }
87  }
88  // catch century/non-400 non-leap years
89  if(iyear > 1599 &&
90  !(iyear % 100) &&
91  (iyear % 400) &&
92  imonth == 2 &&
93  iday == 29)
94  {
95  imonth = 3;
96  iday = 1;
97  }
98  }
99 
100  long convertCalendarToJD( int yy,
101  int mm,
102  int dd )
103  {
104  if(yy == 0)
105  --yy; // there is no year 0
106 
107  if(yy < 0)
108  ++yy;
109 
110  long jd;
111  double y = static_cast<double>( yy );
112  double m = static_cast<double>( mm );
113  //double d = static_cast<double>( dd );
114 
115  // In the conversion from the Julian Calendar to the Gregorian
116  // Calendar the day after October 4, 1582 was October 15, 1582.
117  //
118  // if the date is before October 15, 1582
119  if(yy < 1582 || (yy == 1582 && (mm < 10 || (mm == 10 && dd < 15))))
120  {
121  jd = 1729777 + dd + 367 * yy
122  - static_cast<long>(7 * ( y + 5001 +
123  static_cast<long>((m - 9) / 7)) / 4)
124  + static_cast<long>(275 * m / 9);
125  }
126  else // after Oct 4, 1582
127  {
128  jd = 1721029 + dd + 367 * yy
129  - static_cast<long>(7 * (y + static_cast<long>((m + 9) / 12)) / 4)
130  - static_cast<long>(3 * (static_cast<long>((y + (m - 9) / 7) / 100)
131  + 1) / 4)
132  + static_cast<long>(275 * m / 9);
133 
134  // catch century/non-400 non-leap years
135  if( (! (yy % 100) &&
136  (yy % 400) &&
137  mm > 2 &&
138  mm < 9) ||
139  (!((yy - 1) % 100) &&
140  ((yy - 1) % 400) &&
141  mm == 1))
142  {
143  --jd;
144  }
145  }
146  return jd;
147  }
148 
149  void convertSODtoTime( double sod,
150  int& hh,
151  int& mm,
152  double& sec )
153  {
154  // Get us to within one day.
155  if (sod < 0)
156  {
157  sod += (1 +
158  static_cast<unsigned long>(sod / SEC_PER_DAY)) * SEC_PER_DAY ;
159  }
160  else if (sod >= SEC_PER_DAY)
161  {
162  sod -= static_cast<unsigned long>(sod / SEC_PER_DAY) * SEC_PER_DAY ;
163  }
164 
165  double temp; // variable to hold the integer part of sod
166  sod = modf(sod, &temp); // sod holds the fraction, temp the integer
167  long seconds = static_cast<long>(temp); // get temp into a real integer
168 
169  hh = seconds / 3600 ;
170  mm = (seconds % 3600) / 60 ;
171  sec = double(seconds % 60) + sod ;
172 
173  }
174 
175  double convertTimeToSOD( int hh,
176  int mm,
177  double sec )
178  {
179  return (sec + 60. * (mm + 60. * hh));
180  }
181 
182 } // namespace
TimeConstants.hpp
gnsstk::SEC_PER_DAY
const long SEC_PER_DAY
Seconds per day.
Definition: TimeConstants.hpp:63
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
example4.temp
temp
Definition: example4.py:35
y
page HOWTO subpage DoxygenGuide Documenting Your Code page DoxygenGuide Documenting Your Code todo Flesh out this document section doctips Tips for Documenting When defining make sure that the prototype is identical between the cpp and hpp including both the namespaces and the parameter names for you have std::string as the return type in the hpp file and string as the return type in the cpp Doxygen may get confused and autolink to the cpp version with no documentation If you don t use the same parameter names between the cpp and hpp that will also confuse Doxygen Don t put type information in return or param documentation It doesn t really add anything and will often cause Doxygen to complain and not produce the documentation< br > use note Do not put a comma after a param name unless you mean to document multiple parameters< br/> the output stream</code >< br/> y
Definition: DOCUMENTING.dox:15
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
TimeConverters.hpp
gnsstk::TrackingCode::P
@ P
Legacy GPS precise code.
example6.sod
sod
Definition: example6.py:103
gnsstk::convertCalendarToJD
long convertCalendarToJD(int yy, int mm, int dd)
Definition: TimeConverters.cpp:100
gnsstk::convertTimeToSOD
double convertTimeToSOD(int hh, int mm, double sec)
Definition: TimeConverters.cpp:175


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