Epoch.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 
41 
42 #include <iostream>
43 #include <iomanip>
44 #include <string>
45 #include <ctime>
46 
47 #include "gnsstkplatform.h"
48 #include "Epoch.hpp"
49 
50 #include "TimeConstants.hpp"
51 #include "TimeString.hpp"
52 
53 namespace gnsstk
54 {
55  using namespace std;
56  using namespace gnsstk::StringUtils;
57 
58  // One nanosecond tolerance.
59  const double Epoch::ONE_NSEC_TOLERANCE = 1e-9;
60  // One microsecond tolerance.
61  const double Epoch::ONE_USEC_TOLERANCE = 1e-6;
62  // One millisecond tolerance.
63  const double Epoch::ONE_MSEC_TOLERANCE = 1e-3;
64  // One second tolerance.
65  const double Epoch::ONE_SEC_TOLERANCE = 1;
66  // One minute tolerance.
67  const double Epoch::ONE_MIN_TOLERANCE = 60;
68  // One hour tolerance.
69  const double Epoch::ONE_HOUR_TOLERANCE = 3600;
70 
71  // Tolerance for time equality.
72 #ifdef _WIN32
73  double Epoch::EPOCH_TOLERANCE = ONE_USEC_TOLERANCE;
74 #else
75  double Epoch::EPOCH_TOLERANCE = ONE_NSEC_TOLERANCE;
76 #endif
77 
82 
83  std::string Epoch::PRINT_FORMAT("%02m/%02d/%04Y %02H:%02M:%02S");
84 
86  noexcept
87  {
88  tolerance = tol;
89  return *this;
90  }
91 
92  // Default constructor; initializes to current system time.
93  Epoch::Epoch(const TimeTag& tt)
94  : tolerance(EPOCH_TOLERANCE)
95  {
96  set(tt);
97  }
99  noexcept
100  : core(ct),
101  tolerance(EPOCH_TOLERANCE)
102  {}
108  short year)
109  : tolerance(EPOCH_TOLERANCE)
110  {
111  set(tt, year);
112  }
113 
114  // GPS Zcount constructor.
115  // @param z GPSZcount object to set to
116  // @param f Time frame (see #TimeFrame)
118  noexcept
119  : tolerance(EPOCH_TOLERANCE)
120  {
121  set(z);
122  }
123 
124  Epoch::operator GPSZcount() const
125  {
126  try
127  {
128  // this wants a rounded zcount
129  Epoch e = *this + 0.75;
130  GPSWeekZcount wz = get<GPSWeekZcount>();
131  return GPSZcount(wz.week, wz.zcount);
132  }
133  catch (gnsstk::InvalidParameter& ip)
134  {
135  Epoch::EpochException ee(ip);
136  GNSSTK_THROW(ee);
137  }
138  }
139 
140  // Copy constructor
141  Epoch::Epoch(const Epoch& right)
142  noexcept
143  : core(right.core),
144  tolerance(right.tolerance)
145  {}
146 
147  // Assignment operator.
148  Epoch& Epoch::operator=(const Epoch& right)
149  noexcept
150  {
151  core = right.core;
152  tolerance = right.tolerance;
153  return *this;
154  }
155 
156  // Epoch difference function.
157  // @param right Epoch to subtract from this one.
158  // @return difference in seconds.
159  double Epoch::operator-(const Epoch& right) const
160  noexcept
161  {
162  return core - right.core;
163  }
164 
165  // Add seconds to this time.
166  // @param seconds Number of seconds to increase this time by.
167  // @return The new time incremented by \c seconds.
168  Epoch Epoch::operator+(double seconds) const
169  {
170  return Epoch(*this).addSeconds(seconds);
171  }
172 
173  // Subtract seconds from this time.
174  // @param seconds Number of seconds to decrease this time by.
175  // @return The new time decremented by \c seconds.
176  Epoch Epoch::operator-(double seconds) const
177  {
178  return Epoch(*this).addSeconds(-seconds);
179  }
180 
181  // Add seconds to this time.
182  // @param seconds Number of seconds to increase this time by.
183  Epoch& Epoch::operator+=(double seconds)
184  {
185  return addSeconds(seconds);
186  }
187 
188  // Subtract seconds from this time.
189  // @param sec Number of seconds to decrease this time by.
190  Epoch& Epoch::operator-=(double seconds)
191  {
192  return addSeconds(-seconds);
193  }
194 
195  // Add seconds to this object.
196  // @param seconds Number of seconds to add
197  Epoch& Epoch::addSeconds(double seconds)
198  {
199  try
200  {
201  core.addSeconds(seconds);
202  return *this;
203  }
204  catch( InvalidRequest& ir )
205  {
206  Epoch::EpochException ee(ir);
207  GNSSTK_THROW(ee);
208  }
209  }
210 
211  // Add (integer) seconds to this object.
212  // @param seconds Number of seconds to add.
213  Epoch& Epoch::addSeconds(long seconds)
214  {
215  try
216  {
217  core.addSeconds(seconds);
218  return *this ;
219  }
220  catch( InvalidRequest& ir )
221  {
222  Epoch::EpochException ee(ir);
223  GNSSTK_THROW(ee);
224  }
225  }
226 
227  // Add (integer) milliseconds to this object.
228  // @param msec Number of milliseconds to add.
230  {
231  try
232  {
233  core.addMilliseconds(msec);
234  return *this;
235  }
236  catch( InvalidRequest& ir )
237  {
238  Epoch::EpochException ee(ir);
239  GNSSTK_THROW(ee);
240  }
241  }
242 
243  // Add (integer) microseconds to this object.
244  // @param usec Number of microseconds to add.
246  {
247  try
248  {
249  long ms = usec / 1000; // whole milliseconds
250  usec -= ms * 1000; // leftover microseconds
251  core.addMilliseconds(ms);
252  // us * 1ms/1000us * 1s/1000ms
253  core.addSeconds(static_cast<double>(usec) * 1e-6);
254  return *this;
255  }
256  catch( InvalidRequest& ir )
257  {
258  Epoch::EpochException ee(ir);
259  GNSSTK_THROW(ee);
260  }
261  }
262 
263  // Equality operator.
264  bool Epoch::operator==(const Epoch &right) const
265  noexcept
266  {
267  // use the smaller of the two tolerances for comparison
268  return (ABS(operator-(right)) <=
269  ((tolerance > right.tolerance) ? right.tolerance : tolerance));
270  }
271 
272  // Inequality operator.
273  bool Epoch::operator!=(const Epoch &right) const
274  noexcept
275  {
276  return !(operator==(right));
277  }
278 
279  // Comparison operator (less-than).
280  bool Epoch::operator<(const Epoch &right) const
281  noexcept
282  {
283  return (operator-(right) <
284  -((tolerance > right.tolerance) ? right.tolerance : tolerance));
285  }
286 
287  // Comparison operator (greater-than).
288  bool Epoch::operator>(const Epoch &right) const
289  noexcept
290  {
291  return (operator-(right) >
292  ((tolerance > right.tolerance) ? right.tolerance : tolerance));
293  }
294 
295  // Comparison operator (less-than or equal-to).
296  bool Epoch::operator<=(const Epoch &right) const
297  noexcept
298  {
299  return !(operator>(right));
300  }
301 
302  // Comparison operator (greater-than or equal-to).
303  bool Epoch::operator>=(const Epoch &right) const
304  noexcept
305  {
306  return !(operator<(right));
307  }
308 
309  Epoch::operator CommonTime() const
310  noexcept
311  {
312  return core;
313  }
314 
316  {
317  try
318  {
319  core = tt;
320  return *this;
321  }
322  catch(InvalidParameter& ip)
323  {
324  EpochException ee(ip);
325  GNSSTK_THROW(ee);
326  }
327  }
328 
329  Epoch& Epoch::set(const WeekSecond& tt, short year)
330  {
331  WeekSecond& ws = const_cast<WeekSecond&>(tt);
332  ws.adjustToYear(year);
333  set(ws);
334  return *this;
335  }
336 
338  noexcept
339  {
340  core = c;
341  return *this;
342  }
343 
344  // Set the object's time using the given GPSZcount.
345  // System time is used to disambiguate which 1024 week 'zone'
346  // is appropriate.
347  // @param z the GPSZcount object to set to
348  // @param f Time frame (see #TimeFrame)
349  // @return a reference to this object.
351  {
352  try
353  {
354  GPSWeekZcount wz(core);
355  wz.week = z.getWeek();
356  wz.zcount = z.getZcount();
357  core = wz;
358  return *this ;
359  }
360  catch(Exception& exc)
361  {
362  EpochException ee(exc);
363  GNSSTK_THROW(ee);
364  }
365  }
366 
368  {
369  try
370  {
371  long myDAY, mySOD, ctDAY, ctSOD;
372  double myFSOD, ctFSOD;
373  core.get(myDAY, mySOD, myFSOD);
374  ct.get(ctDAY, ctSOD, ctFSOD);
375  core.set(myDAY, ctSOD, ctFSOD);
376  return *this;
377  }
378  catch(InvalidParameter& ip)
379  {
380  EpochException ee(ip);
381  GNSSTK_THROW(ee);
382  }
383  }
384 
386  {
387  try
388  {
389  long myDAY, mySOD, ctDAY, ctSOD;
390  double myFSOD, ctFSOD;
391  core.get(myDAY, mySOD, myFSOD);
392  ct.get(ctDAY, ctSOD, ctFSOD);
393  core.set(ctDAY, mySOD, myFSOD);
394  return *this;
395  }
396  catch(InvalidParameter& ip)
397  {
398  EpochException ee(ip);
399  GNSSTK_THROW(ee);
400  }
401  }
402 
403  // set using local time
405  {
406  time_t t;
407  time(&t);
408  struct tm *ltod;
409  ltod = localtime(&t);
410  return set(CivilTime(1900 + ltod->tm_year,
411  ltod->tm_mon + 1,
412  ltod->tm_mday,
413  ltod->tm_hour,
414  ltod->tm_min,
415  ltod->tm_sec));
416  }
417 
418  Epoch& Epoch::scanf(const string& str,
419  const string& fmt)
420  {
421  try
422  {
423  scanTime( core, str, fmt );
424  return *this;
425  }
426  catch (StringException& se)
427  {
429  }
430  }
431 
432  // Format this time into a string.
433  string Epoch::printf(const string& fmt) const
434  {
435  try
436  {
437  return printTime( core, fmt );
438  }
439  catch (StringException& se)
440  {
442  }
443  }
444 
445  // Stream output for Epoch objects. Typically used for debugging.
446  // @param s stream to append formatted Epoch to.
447  // @param t Epoch to append to stream \c s.
448  // @return reference to \c s.
449  ostream& operator<<( ostream& s,
450  const Epoch& e )
451  {
452  s << e.printf();
453  return s;
454  }
455 
456 } // end namespace gnsstk
gnsstk::Epoch::ONE_SEC_TOLERANCE
static const double ONE_SEC_TOLERANCE
One second tolerance.
Definition: Epoch.hpp:154
gnsstk::Epoch::setDate
Epoch & setDate(const CommonTime &ct)
Definition: Epoch.cpp:385
gnsstk::Epoch::operator<
bool operator<(const Epoch &right) const noexcept
Definition: Epoch.cpp:280
se
double se
obliquity cos, T*cos, sin coefficients
Definition: IERS2003NutationData.hpp:48
gnsstk::operator==
bool operator==(const IonexData::IonexValType &x, const IonexData::IonexValType &y)
operator == for IonexData::IonexValType
Definition: IonexData.hpp:253
TimeConstants.hpp
gnsstk::CommonTime::addSeconds
CommonTime & addSeconds(double seconds)
Definition: CommonTime.cpp:332
gnsstk::Week::adjustToYear
virtual void adjustToYear(unsigned int y)
Definition: Week.hpp:219
gnsstk::GPSWeekZcount::zcount
unsigned int zcount
Definition: GPSWeekZcount.hpp:371
gnsstk::Epoch::ONE_NSEC_TOLERANCE
static const double ONE_NSEC_TOLERANCE
One nanosecond tolerance.
Definition: Epoch.hpp:148
gnsstk::Epoch::PRINT_FORMAT
static std::string PRINT_FORMAT
This is how an Epoch is printed by default.
Definition: Epoch.hpp:169
gnsstk::Epoch::operator==
bool operator==(const Epoch &right) const noexcept
Definition: Epoch.cpp:264
gnsstk::Epoch::core
CommonTime core
Definition: Epoch.hpp:699
gnsstk::scanTime
void scanTime(TimeTag &btime, const string &str, const string &fmt)
Definition: TimeString.cpp:93
gnsstk::Epoch::addMicroSeconds
Epoch & addMicroSeconds(long usec)
Definition: Epoch.cpp:245
gnsstk::Epoch::operator-
double operator-(const Epoch &right) const noexcept
Definition: Epoch.cpp:159
gnsstk::BEGINNING_OF_TIME
const Epoch BEGINNING_OF_TIME(CommonTime::BEGINNING_OF_TIME)
Earliest representable Epoch.
const
#define const
Definition: getopt.c:43
gnsstk::Epoch::operator+
Epoch operator+(double sec) const
Definition: Epoch.cpp:168
example6.year
year
Definition: example6.py:64
gnsstk::GPSZcount::getZcount
long getZcount() const
GPS Z-count accessor.
Definition: GPSZcount.hpp:117
gnsstk::Epoch::printf
std::string printf(const std::string &fmt=PRINT_FORMAT) const
Definition: Epoch.cpp:433
gnsstk::Epoch::EPOCH_TOLERANCE
static double EPOCH_TOLERANCE
Default tolerance for time equality in days.
Definition: Epoch.hpp:161
gnsstkplatform.h
gnsstk::Epoch::operator-=
Epoch & operator-=(double sec)
Definition: Epoch.cpp:190
gnsstk::Epoch::setTolerance
Epoch & setTolerance(double tol) noexcept
Definition: Epoch.cpp:85
gnsstk::Epoch::operator=
Epoch & operator=(const Epoch &right) noexcept
Assignment operator.
Definition: Epoch.cpp:148
gnsstk::Epoch::operator<=
bool operator<=(const Epoch &right) const noexcept
Definition: Epoch.cpp:296
gnsstk::CommonTime::BEGINNING_OF_TIME
static const GNSSTK_EXPORT CommonTime BEGINNING_OF_TIME
earliest representable CommonTime
Definition: CommonTime.hpp:102
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
gnsstk::Epoch::ONE_MIN_TOLERANCE
static const double ONE_MIN_TOLERANCE
One minute tolerance.
Definition: Epoch.hpp:156
gnsstk::Exception
Definition: Exception.hpp:151
gnsstk::Epoch::ONE_HOUR_TOLERANCE
static const double ONE_HOUR_TOLERANCE
One hour tolerance.
Definition: Epoch.hpp:158
gnsstk::CommonTime::END_OF_TIME
static const GNSSTK_EXPORT CommonTime END_OF_TIME
latest representable CommonTime
Definition: CommonTime.hpp:104
gnsstk::GPSWeek::week
int week
Definition: GPSWeek.hpp:271
gnsstk::Epoch::Epoch
Epoch(const TimeTag &tt=SystemTime())
Definition: Epoch.cpp:93
gnsstk::Epoch::setLocalTime
Epoch & setLocalTime()
Definition: Epoch.cpp:404
gnsstk::TimeTag
Definition: TimeTag.hpp:58
gnsstk::Epoch::scanf
Epoch & scanf(const std::string &str, const std::string &fmt)
Definition: Epoch.cpp:418
gnsstk::GPSZcount
Definition: GPSZcount.hpp:75
gnsstk::Epoch::addMilliSeconds
Epoch & addMilliSeconds(long msec)
Definition: Epoch.cpp:229
gnsstk::GPSZcount::getWeek
short getWeek() const
GPS week accessor.
Definition: GPSZcount.hpp:113
example4.time
time
Definition: example4.py:103
gnsstk::operator<<
std::ostream & operator<<(std::ostream &s, const ObsEpoch &oe) noexcept
Definition: ObsEpochMap.cpp:54
gnsstk::CommonTime
Definition: CommonTime.hpp:84
gnsstk::Epoch::year
short year() const
Get year.
Definition: Epoch.hpp:746
gnsstk::Epoch::ONE_USEC_TOLERANCE
static const double ONE_USEC_TOLERANCE
One microsecond tolerance.
Definition: Epoch.hpp:150
gnsstk::CommonTime::get
void get(long &day, long &sod, double &fsod, TimeSystem &timeSystem) const
Definition: CommonTime.cpp:213
gnsstk::END_OF_TIME
const Epoch END_OF_TIME(CommonTime::END_OF_TIME)
Latest Representable Epoch.
GNSSTK_RETHROW
#define GNSSTK_RETHROW(exc)
Definition: Exception.hpp:369
gnsstk::WeekSecond
Definition: WeekSecond.hpp:60
gnsstk::GPSWeekZcount
Definition: GPSWeekZcount.hpp:55
gnsstk::Epoch::operator!=
bool operator!=(const Epoch &right) const noexcept
Definition: Epoch.cpp:273
ABS
#define ABS(x)
Definition: MathBase.hpp:73
gnsstk::CivilTime
Definition: CivilTime.hpp:55
gnsstk::operator<
bool operator<(const IonexData::IonexValType &x, const IonexData::IonexValType &y)
operator < for IonexData::IonexValType
Definition: IonexData.hpp:265
gnsstk::StringUtils
Definition: IonexStoreStrategy.cpp:44
gnsstk::CommonTime::addMilliseconds
CommonTime & addMilliseconds(long ms)
Definition: CommonTime.cpp:371
gnsstk::printTime
std::string printTime(const CommonTime &t, const std::string &fmt)
Definition: TimeString.cpp:64
std
Definition: Angle.hpp:142
Epoch.hpp
gnsstk::Epoch::ONE_MSEC_TOLERANCE
static const double ONE_MSEC_TOLERANCE
One millisecond tolerance.
Definition: Epoch.hpp:152
gnsstk::Epoch
Definition: Epoch.hpp:123
GNSSTK_THROW
#define GNSSTK_THROW(exc)
Definition: Exception.hpp:366
gnsstk::Epoch::set
Epoch & set(const TimeTag &tt=SystemTime())
Definition: Epoch.cpp:315
gnsstk::Epoch::operator+=
Epoch & operator+=(double sec)
Definition: Epoch.cpp:183
TimeString.hpp
gnsstk::Epoch::operator>
bool operator>(const Epoch &right) const noexcept
Definition: Epoch.cpp:288
gnsstk::CommonTime::set
CommonTime & set(long day, long sod, double fsod=0.0, TimeSystem timeSystem=TimeSystem::Unknown)
Definition: CommonTime.cpp:87
gnsstk::Epoch::setTime
Epoch & setTime(const CommonTime &ct)
Definition: Epoch.cpp:367
gnsstk::Epoch::operator>=
bool operator>=(const Epoch &right) const noexcept
Definition: Epoch.cpp:303
gnsstk::Epoch::addSeconds
Epoch & addSeconds(double seconds)
Definition: Epoch.cpp:197


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