TimeRange.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 "TimeRange.hpp"
40 #include "TimeString.hpp"
41 
42 using namespace std;
43 using namespace gnsstk;
44 
45 
46 namespace gnsstk
47 {
48  using namespace std;
49 
50  TimeRange::TimeRange():
52  end(CommonTime::END_OF_TIME),
53  includeStartTime(true),
54  includeEndTime(true)
55  {}
56 
58  const CommonTime& startDT,
59  const CommonTime& endDT,
60  const bool startInclusive,
61  const bool endInclusive )
62  {
63  try
64  {
65  init( startDT, endDT, startInclusive, endInclusive );
66  }
67  catch (TimeRangeException tre)
68  {
69  tre.addText("Initializing from two CommonTime values.\n");
70  GNSSTK_RETHROW(tre);
71  }
72  }
73 
75  const bool startInclusive,
76  const bool endInclusive )
77  {
78  try
79  {
80  init( dtPair.first,
81  dtPair.second,
82  startInclusive,
83  endInclusive );
84  }
85  catch (TimeRangeException tre)
86  {
87  tre.addText("Initializing from a CommonTime pair.\n");
88  GNSSTK_RETHROW(tre);
89  }
90  }
91 
92  // Common initialization method used by two constructors
93  void TimeRange::init( const CommonTime& startDT,
94  const CommonTime& endDT,
95  const bool startInclusive,
96  const bool endInclusive )
97  {
98  if (endDT<startDT)
99  {
100  string tform = "%02m/%02d/%4Y %02H:%02M:%02S %Q";
101  TimeRangeException tre;
102  tre.addText("TimeRange() - Start time must be before end time.\n");
103  string ts = " start: ";
104  ts += printTime(startDT,tform);
105  ts += "\n";
106  tre.addText(ts);
107  ts = " end: ";
108  ts += printTime(endDT,tform);
109  ts += "\n";
110  tre.addText(ts);
111  GNSSTK_THROW(tre);
112  }
113 
114  start = startDT;
115  end = endDT;
116  includeStartTime = startInclusive;
117  includeEndTime = endInclusive;
118  }
119 
120  void TimeRange::set( const CommonTime& startDT,
121  const CommonTime& endDT,
122  const bool startInclusive,
123  const bool endInclusive )
124  {
125  try
126  {
127  init( startDT, endDT, startInclusive, endInclusive );
128  }
129  catch (TimeRangeException tre)
130  {
131  tre.addText("Called from TimeRange::set().\n");
132  GNSSTK_RETHROW(tre);
133  }
134  }
135 
136 
138  {
139  start = tr.start;
140  end = tr.end;
143  }
144 
145 
146  bool TimeRange::inRange( const CommonTime& testDT ) const
147  {
148  // First, test for cases that don't involve equality
149  // as it is expected most cases will fall in this category.
150  if (testDT<start) return false;
151  if (end<testDT) return false;
152  if (start<testDT &&
153  testDT < end) return true;
154 
155  // Last, test for the cases in which the test time is
156  // on a boundary condition.
157  // To reach this code, it is already proven that
158  // testDT must be equal to either start or end; however,
159  // we haven't proven which.
160  if (testDT==start && includeStartTime==true) return true;
161  if (testDT==end && includeEndTime==true) return true;
162  return false;
163  }
164 
165  // True if all members are equal
166  bool TimeRange::operator==(const TimeRange& right) const
167  {
168  if (start==right.start &&
169  end==right.end &&
171  includeEndTime==right.includeEndTime ) return true;
172  return false;
173  }
174 
175  bool TimeRange::operator<(const TimeRange& right) const
176  {
177  // If both are inclusive, then simply return true
178  // if left start < right start.
180  {
181  if (start<right.start)
182  {
183  return true;
184  }
185  return false;
186  }
187 
188  // If right is NOT inclusive, that implies left
189  // IS inclusive. In this case, return true if
190  // left is <= right
191  if (!right.includeStartTime)
192  {
193  if (start<=right.start)
194  {
195  return true;
196  }
197  }
198  return false;
199  }
200 
201  // True if start/end of this object are both prior
202  // to start of "right"
203  // Note that the constructor verifies start<end.
204  bool TimeRange::isPriorTo( const TimeRange& right ) const
205  {
206  if (end < right.start) return true;
207 
208  // Now handle two edge cases. These are where
209  // the edges of "this" and right align such that
210  // [ this ) [ this ]
211  // [ right ] - or - ( right ]
212  // In these cases, "this" qualifies as being prior to right.
213  if (end==right.start &&
214  (!includeEndTime || !right.includeStartTime) ) return true;
215 
216  return false;
217  }
218 
219  // True if this.start <= right.end and
220  // this.end >= right.start
221  bool TimeRange::overlaps( const TimeRange& right ) const
222  {
223  // Check the simple case first
224  if ( start<right.end &&
225  end>right.start ) return true;
226 
227  // Then check edge cases. There are two picky edge
228  // cases. One where "this" leads right, both have boundaries
229  // included and the end of "this" is aligned with the end of "right"
230  // [ this ]
231  // [ right ]
232  // The second is similar, but reversed.
233  // [ this ]
234  // [ right ]
235  //
236  if ( includeEndTime &&
237  right.includeStartTime &&
238  end==right.start ) return true;
239 
240  if ( includeStartTime &&
241  right.includeEndTime &&
242  start==right.end ) return true;
243 
244  return false;
245  }
246 
247  // True if this.start >= right.start and
248  // this.end <= right.end
249  bool TimeRange::isSubsetOf( const TimeRange& right ) const
250  {
251  if (start>=right.start &&
252  end<=right.end ) return true;
253  return false;
254  }
255 
256  // True if start/end of this object are both
257  // after the end of "right"
258  bool TimeRange::isAfter( const TimeRange& right ) const
259  {
260  if (start > right.end) return true;
261 
262  // Now handle two edge cases. These are where
263  // the edges of "this" and right align such that
264  // [ this ] ( this ]
265  // [ right ) - or - [ right ]
266  // In these cases, "this" qualifies as being after right.
267  if (start==right.end &&
268  (!includeStartTime || !right.includeEndTime) ) return true;
269 
270  return false;
271  }
272 
273  // Formatted input
274  TimeRange& TimeRange::setToString( const string& str,
275  const string& fmt)
276  {
277  // Ignore leading whitespace.
278  // See if first non-whitespace character is '[' or '('
279  std::string whitespace(" \t\n");
280  std::string::size_type n = str.find_first_not_of(whitespace);
281  // Helps when given invalid strings
282  if (n == string::npos)
283  {
284  TimeRangeException tre;
285  tre.addText("TimeRange:Failure finding first non-whitespace character");
286  GNSSTK_THROW(tre);
287  }
288  includeStartTime = true; // default case
289  string leadChar = str.substr(n,1);
290 
291 // std::cout << "leadChar :'" << leadChar << "'" << endl;
292  if (leadChar.compare("[")==0)
293  {
294  n++; // Already set inclusion flag,
295  // before continuing to parse the line.
296  }
297  else if (leadChar.compare("(")==0)
298  {
299  includeStartTime = false;
300  n++;
301  }
302 
303  // Find separating comma indicating the end of the first CommonTime
304  std::string::size_type endFirstTime = str.find(",", n);
305  std::string firstTime = str.substr(n, endFirstTime-n);
306  StringUtils::stripLeading(firstTime);
307 // std::cout << "firstTime:'" << firstTime << "'" << endl;
308 
309  try
310  {
311  mixedScanTime(start, firstTime, fmt);
312  }
313  catch(InvalidRequest& exc)
314  {
315  TimeRangeException tre(exc);
316  tre.addText("TimeRange:Failure converting first time");
317  GNSSTK_THROW(tre);
318  }
319  catch(std::exception& exc)
320  {
321  TimeRangeException tre(exc.what());
322  tre.addText("TimeRange:Failure converting first time");
323  GNSSTK_THROW(tre);
324  }
325 
326 // std::cout << "Finished first time convert:"
327 // << start.printf("%02m/%02m/%4Y %02H:%02M:%02S") << std::endl;
328 
329  // Find optional end inclusion definition.
330  std::string::size_type endCharPos = str.find_first_of("])", endFirstTime+1);
331  std::string::size_type endSecondTime = endCharPos;
332  includeEndTime = true;
333  if (endCharPos!=std::string::npos)
334  {
335  endSecondTime--;
336  string endChar = str.substr(endCharPos,1);
337 // std::cout << "endChar: '" << endChar << "'" << endl;
338  if (endChar.compare(")")==0)
339  {
340  includeEndTime = false;
341  }
342  }
343 
344  std::string secondTime = str.substr( endFirstTime+1, endSecondTime-endFirstTime );
345  StringUtils::stripLeading(secondTime);
347  try
348  {
349  mixedScanTime(end, secondTime, fmt);
350  }
351  catch(InvalidRequest& exc)
352  {
353  TimeRangeException tre(exc);
354  tre.addText("TimeRange:Failure converting second time");
355  GNSSTK_THROW(tre);
356  }
357  catch(std::exception& exc)
358  {
359  TimeRangeException tre(exc.what());
360  tre.addText("TimeRange:Failure converting second time");
361  GNSSTK_THROW(tre);
362  }
363 
364  // The end time must be greater than the start time.
365  if (end<start)
366  {
367  TimeRangeException tre("Ending time is prior to beginning time");
368 
369  GNSSTK_THROW(tre);
370  }
371 
372  return *this;
373  }
374 
375  // Formatted output
376  std::string TimeRange::printf(const std::string formatArg) const
377  {
378  string out;
379 
380  if (includeStartTime) out += "[";
381  else out += "(";
382  out += printTime(start, formatArg);
383  out += ", ";
384  out += printTime(end, formatArg);
385 
386  char includeEnd = ']';
387  if (!includeEndTime) includeEnd = ')';
388  out += includeEnd;
389 
390  return out;
391  }
392 
393 
394  std::string TimeRange::dump(const std::string formatArg ) const
395  {
396 // char includeStart = '[';
397 // if (!includeStartTime) includeStart = '(';
398  string out;
399 
400  if (includeStartTime) out += "[Start:";
401  else out += "(Start:";
402  out += printTime(start, formatArg);
403  out += ", End: ";
404  out += printTime(end, formatArg);
405 
406  char includeEnd = ']';
407  if (!includeEndTime) includeEnd = ')';
408  out += includeEnd;
409 
410  return out;
411  }
412 }
gnsstk::TimeRange::operator==
bool operator==(const TimeRange &right) const
Equivalence means all members are identical.
Definition: TimeRange.cpp:166
gnsstk::BEGINNING_OF_TIME
const Epoch BEGINNING_OF_TIME(CommonTime::BEGINNING_OF_TIME)
Earliest representable Epoch.
gnsstk::TimeRange::start
CommonTime start
Definition: TimeRange.hpp:156
gnsstk::TimeRange::init
void init(const CommonTime &startDT, const CommonTime &endDT, const bool startInclusive=true, const bool endInclusive=true)
Definition: TimeRange.cpp:93
gnsstk::TimeRange::operator<
bool operator<(const TimeRange &right) const
Definition: TimeRange.cpp:175
gnsstk::TimeRange::overlaps
bool overlaps(const TimeRange &right) const
Definition: TimeRange.cpp:221
gnsstk::TimeRange::printf
std::string printf(const std::string formatArg="%02m/%02d/%02y %02H:%02M:%02S") const
Definition: TimeRange.cpp:376
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
gnsstk::StringUtils::stripLeading
std::string & stripLeading(std::string &s, const std::string &aString, std::string::size_type num=std::string::npos)
Definition: StringUtils.hpp:1426
gnsstk::TimeRange::inRange
bool inRange(const CommonTime &testDT) const
Definition: TimeRange.cpp:146
gnsstk::TimeRange
Definition: TimeRange.hpp:59
gnsstk::TimeRange::isSubsetOf
bool isSubsetOf(const TimeRange &right) const
Definition: TimeRange.cpp:249
gnsstk::mixedScanTime
void mixedScanTime(CommonTime &t, const string &str, const string &fmt)
Definition: TimeString.cpp:432
gnsstk::TimeRange::includeStartTime
bool includeStartTime
Definition: TimeRange.hpp:158
gnsstk::TimeRange::isAfter
bool isAfter(const TimeRange &right) const
Definition: TimeRange.cpp:258
gnsstk::TimeRange::isPriorTo
bool isPriorTo(const TimeRange &right) const
Definition: TimeRange.cpp:204
gnsstk::TimeRange::includeEndTime
bool includeEndTime
Definition: TimeRange.hpp:159
gnsstk::CommonTime
Definition: CommonTime.hpp:84
gnsstk::TimeRange::set
void set(const CommonTime &startDT, const CommonTime &endDT, const bool startInclusive=true, const bool endInclusive=true)
Definition: TimeRange.cpp:120
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::TimeRange::dump
std::string dump(const std::string formatArg="%02m/%02d/%02y %02H:%02M:%02S") const
Dump method.
Definition: TimeRange.cpp:394
gnsstk::TimeRange::TimeRange
TimeRange()
Definition: TimeRange.cpp:50
gnsstk::TimeRange::end
CommonTime end
Definition: TimeRange.hpp:157
gnsstk::printTime
std::string printTime(const CommonTime &t, const std::string &fmt)
Definition: TimeString.cpp:64
std
Definition: Angle.hpp:142
gnsstk::TimeRange::DTPair
std::pair< CommonTime, CommonTime > DTPair
Definition: TimeRange.hpp:65
GNSSTK_THROW
#define GNSSTK_THROW(exc)
Definition: Exception.hpp:366
TimeRange.hpp
gnsstk::TimeRange::setToString
TimeRange & setToString(const std::string &str, const std::string &fmt)
Definition: TimeRange.cpp:274
TimeString.hpp


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