CivilTime.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 
40 
41 #include <cmath>
42 #include "CivilTime.hpp"
43 #include "TimeConverters.hpp"
44 
45 namespace gnsstk
46 {
48  const char * CivilTime::MonthNames[] =
49  {
50  "Error",
51  "January","February", "March", "April",
52  "May", "June","July", "August",
53  "September", "October", "November", "December"
54  };
55 
57  const char * CivilTime::MonthAbbrevNames[] =
58  {
59  "err", "Jan", "Feb", "Mar", "Apr", "May", "Jun",
60  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
61  };
62 
64  {
65  year = right.year;
66  month = right.month;
67  day = right.day;
68  hour = right.hour;
69  minute = right.minute;
70  second = right.second;
71  timeSystem = right.timeSystem;
72  return *this;
73  }
74 
76  {
77  try
78  {
79  // get the julian day
80  long jday = convertCalendarToJD( year, month, day );
81  // get the second of day
82  double sod = convertTimeToSOD( hour, minute, second );
83  // make a CommonTime with jd, whole sod, and
84  // fractional second of day
85  CommonTime ct;
86  return ct.set( jday, static_cast<long>(sod) ,
87  (sod - static_cast<long>(sod)),
88  timeSystem );
89  }
90  catch (InvalidParameter& ip)
91  {
92  InvalidRequest ir(ip);
93  GNSSTK_THROW(ir);
94  }
95  }
96 
98  {
99  long jday, sod;
100  double fsod;
101  // get the julian day, second of day, and fractional second of day
102  ct.get( jday, sod, fsod, timeSystem );
103  // convert the julian day to calendar "year/month/day of month"
104  convertJDtoCalendar( jday, year, month, day );
105  // convert the (whole) second of day to "hour/minute/second"
106  convertSODtoTime( static_cast<double>( sod ), hour, minute, second );
107  // add the fractional second of day to "second"
108  second += fsod;
109  }
110 
111  std::string CivilTime::printf( const std::string& fmt ) const
112  {
113  try
114  {
116  std::string rv = fmt;
117 
118  rv = formattedPrint( rv, getFormatPrefixInt() + "Y",
119  "Yd", year );
120  rv = formattedPrint( rv, getFormatPrefixInt() + "y",
121  "yd", static_cast<short>( year % 100 ) );
122  rv = formattedPrint( rv, getFormatPrefixInt() + "m",
123  "mu", month );
124  rv = formattedPrint( rv, getFormatPrefixInt() + "b",
125  "bs", MonthAbbrevNames[month] );
126  rv = formattedPrint( rv, getFormatPrefixInt() + "B",
127  "Bs", MonthNames[month] );
128  rv = formattedPrint( rv, getFormatPrefixInt() + "d",
129  "du", day );
130  rv = formattedPrint( rv, getFormatPrefixInt() + "H",
131  "Hu", hour );
132  rv = formattedPrint( rv, getFormatPrefixInt() + "M",
133  "Mu", minute );
134  rv = formattedPrint( rv, getFormatPrefixInt() + "S",
135  "Su", static_cast<short>( second ) );
136  rv = formattedPrint( rv, getFormatPrefixFloat() + "f",
137  "ff", second );
138  rv = formattedPrint( rv, getFormatPrefixInt() + "P",
139  "Ps", StringUtils::asString(timeSystem).c_str() );
140  return rv;
141  }
142  catch( gnsstk::StringUtils::StringException& exc )
143  {
144  GNSSTK_RETHROW( exc );
145  }
146  }
147 
148  std::string CivilTime::printError( const std::string& fmt) const
149  {
150  try
151  {
153  std::string rv = fmt;
154 
155  rv = formattedPrint( rv, getFormatPrefixInt() + "Y",
156  "Ys", getError().c_str() );
157  rv = formattedPrint( rv, getFormatPrefixInt() + "y",
158  "ys", getError().c_str() );
159  rv = formattedPrint( rv, getFormatPrefixInt() + "m",
160  "ms", getError().c_str() );
161  rv = formattedPrint( rv, getFormatPrefixInt() + "b",
162  "bs", getError().c_str() );
163  rv = formattedPrint( rv, getFormatPrefixInt() + "B",
164  "Bs", getError().c_str() );
165  rv = formattedPrint( rv, getFormatPrefixInt() + "d",
166  "ds", getError().c_str() );
167  rv = formattedPrint( rv, getFormatPrefixInt() + "H",
168  "Hs", getError().c_str() );
169  rv = formattedPrint( rv, getFormatPrefixInt() + "M",
170  "Ms", getError().c_str() );
171  rv = formattedPrint( rv, getFormatPrefixInt() + "S",
172  "Ss", getError().c_str() );
173  rv = formattedPrint( rv, getFormatPrefixFloat() + "f",
174  "fs", getError().c_str() );
175  rv = formattedPrint( rv, getFormatPrefixInt() + "P",
176  "Ps", getError().c_str() );
177  return rv;
178  }
179  catch( gnsstk::StringUtils::StringException& exc )
180  {
181  GNSSTK_RETHROW( exc );
182  }
183  }
184 
185  bool CivilTime::setFromInfo( const IdToValue& info )
186  {
187  using namespace gnsstk::StringUtils;
188 
189  for( IdToValue::const_iterator i = info.begin(); i != info.end(); i++ )
190  {
191  switch( i->first )
192  {
193  case 'Y':
194  year = asInt( i->second );
195  break;
196 
197  case 'y':
198  // match the POSIX strptime() function:
199  /* Year within century. When a century is not
200  * otherwise specified, values in the range 69-99
201  * refer to years in the twentieth century (1969 to
202  * 1999 inclusive); values in the range 00-68 refer
203  * to years in the twenty-first century (2000 to
204  * 2068 inclusive). */
205  if( i->second.length() > 2)
206  return false;
207  year = asInt( i->second );
208  if (year >= 69)
209  year += 1900;
210  else
211  year += 2000;
212  break;
213 
214  case 'm':
215  month = asInt( i->second );
216  break;
217 
218  case 'b':
219  month = monthAbbrev( i->second );
220  if (month < 1)
221  return false;
222  break;
223 
224  case 'B':
225  month = monthLong( i->second );
226  if (month < 1)
227  return false;
228  break;
229 
230  case 'd':
231  day = asInt( i->second );
232  break;
233 
234  case 'H':
235  hour = asInt( i->second );
236  break;
237 
238  case 'M':
239  minute = asInt( i->second );
240  break;
241 
242  case 'S':
243  case 'f':
244  second = asDouble( i->second );
245  if (i->first == 'S')
246  second = floor(second);
247  break;
248 
249  case 'P':
251  break;
252 
253  default:
254  // do nothing
255  break;
256  };
257  }
258 
259  return true;
260  }
261 
262  bool CivilTime::isValid() const
263  {
264  CivilTime temp;
265  temp.convertFromCommonTime( convertToCommonTime() );
266  if( *this == temp )
267  {
268  return true;
269  }
270  return false;
271  }
272 
274  {
275  year = 0;
276  month = day = 1;
277  hour = minute = 0;
278  second = 0.0;
280  }
281 
282  int CivilTime::monthAbbrev(const std::string& amonStr)
283  {
285  for (unsigned i = 1; i <= 12; i++)
286  {
287  if (lowerCase(amonStr) == lowerCase(MonthAbbrevNames[i]))
288  return i;
289  }
290  return 0;
291  }
292 
293  int CivilTime::monthLong(const std::string& monStr)
294  {
296  for (unsigned i = 1; i <= 12; i++)
297  {
298  if (lowerCase(monStr) == lowerCase(MonthNames[i]))
299  return i;
300  }
301  return 0;
302  }
303 
304  bool CivilTime::operator==( const CivilTime& right ) const
305  {
307  if ((timeSystem != TimeSystem::Any &&
308  right.timeSystem != TimeSystem::Any) &&
309  timeSystem != right.timeSystem)
310  return false;
311 
312  if( year == right.year &&
313  month == right.month &&
314  day == right.day &&
315  hour == right.hour &&
316  minute == right.minute &&
317  fabs(second - right.second) < CommonTime::eps )
318  {
319  return true;
320  }
321  return false;
322  }
323 
324  bool CivilTime::operator!=( const CivilTime& right ) const
325  {
326  return ( !operator==( right ) );
327  }
328 
329  bool CivilTime::operator<( const CivilTime& right ) const
330  {
332  if ((timeSystem != TimeSystem::Any &&
333  right.timeSystem != TimeSystem::Any) &&
334  timeSystem != right.timeSystem)
335  {
336  gnsstk::InvalidRequest ir("CommonTime objects not in same time system, cannot be compared");
337  GNSSTK_THROW(ir);
338  }
339 
340  if( year < right.year )
341  {
342  return true;
343  }
344  if( year > right.year )
345  {
346  return false;
347  }
348  if( month < right.month )
349  {
350  return true;
351  }
352  if( month > right.month )
353  {
354  return false;
355  }
356  if( day < right.day )
357  {
358  return true;
359  }
360  if( day > right.day )
361  {
362  return false;
363  }
364  if( hour < right.hour )
365  {
366  return true;
367  }
368  if( hour > right.hour )
369  {
370  return false;
371  }
372  if( minute < right.minute )
373  {
374  return true;
375  }
376  if( minute > right.minute )
377  {
378  return false;
379  }
380  if( second < right.second )
381  {
382  return true;
383  }
384 
385  return false;
386  }
387 
388  bool CivilTime::operator>( const CivilTime& right ) const
389  {
390  return ( !operator<=( right ) );
391  }
392 
393  bool CivilTime::operator<=( const CivilTime& right ) const
394  {
395  return ( operator<( right ) || operator==( right ) );
396  }
397 
398  bool CivilTime::operator>=( const CivilTime& right ) const
399  {
400  return ( !operator<( right ) );
401  }
402 }
403 
404 std::ostream& operator<<( std::ostream& s,
405  const gnsstk::CivilTime& cit )
406 {
407  s << cit.printf("%02m/%02d/%04Y %02H:%02M:%02S %P");
408  return s;
409 }
gnsstk::TimeTag::getFormatPrefixInt
static std::string getFormatPrefixInt()
Definition: TimeTag.hpp:152
gnsstk::CivilTime::operator=
CivilTime & operator=(const CivilTime &right)
Definition: CivilTime.cpp:63
gnsstk::CivilTime::monthLong
static int monthLong(const std::string &monStr)
Definition: CivilTime.cpp:293
gnsstk::StringUtils::asInt
long asInt(const std::string &s)
Definition: StringUtils.hpp:713
gnsstk::CommonTime::eps
static const GNSSTK_EXPORT double eps
Default tolerance for time equality in days.
Definition: CommonTime.hpp:107
gnsstk::CivilTime::operator<=
bool operator<=(const CivilTime &right) const
Definition: CivilTime.cpp:393
gnsstk::CivilTime::year
int year
Definition: CivilTime.hpp:198
gnsstk::CivilTime::printError
virtual std::string printError(const std::string &fmt) const
Definition: CivilTime.cpp:148
gnsstk::TimeTag::IdToValue
std::map< char, std::string > IdToValue
Definition: TimeTag.hpp:103
gnsstk::TimeSystem::Any
@ Any
wildcard; allows comparison with any other type
gnsstk::StringUtils::asString
std::string asString(IonexStoreStrategy e)
Convert a IonexStoreStrategy to a whitespace-free string name.
Definition: IonexStoreStrategy.cpp:46
gnsstk::CivilTime::day
int day
Definition: CivilTime.hpp:200
gnsstk::TimeTag::getError
static std::string getError()
This returns the default error string for the TimeTag classes.
Definition: TimeTag.hpp:161
gnsstk::CivilTime::MonthAbbrevNames
static const char * MonthAbbrevNames[]
Short month names for converstion from numbers to strings.
Definition: CivilTime.hpp:126
gnsstk::CivilTime::convertFromCommonTime
virtual void convertFromCommonTime(const CommonTime &ct)
Definition: CivilTime.cpp:97
gnsstk::TimeSystem::Unknown
@ Unknown
unknown time frame; for legacy code compatibility
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
gnsstk::TimeTag::timeSystem
TimeSystem timeSystem
time system (representation) of the data
Definition: TimeTag.hpp:204
example4.temp
temp
Definition: example4.py:35
gnsstk::CivilTime::convertToCommonTime
virtual CommonTime convertToCommonTime() const
Definition: CivilTime.cpp:75
gnsstk::convertJDtoCalendar
void convertJDtoCalendar(long jd, int &iyear, int &imonth, int &iday)
Definition: TimeConverters.cpp:52
gnsstk::CivilTime::monthAbbrev
static int monthAbbrev(const std::string &amonStr)
Definition: CivilTime.cpp:282
gnsstk::TimeTag::getFormatPrefixFloat
static std::string getFormatPrefixFloat()
Definition: TimeTag.hpp:157
gnsstk::StringUtils::asTimeSystem
TimeSystem asTimeSystem(const std::string &s)
Convert a string representation of TimeSystem to an enum.
Definition: TimeSystem.cpp:324
gnsstk::CivilTime::printf
virtual std::string printf(const std::string &fmt) const
Definition: CivilTime.cpp:111
gnsstk::convertSODtoTime
void convertSODtoTime(double sod, int &hh, int &mm, double &sec)
Definition: TimeConverters.cpp:149
gnsstk::CivilTime::operator==
bool operator==(const CivilTime &right) const
Definition: CivilTime.cpp:304
gnsstk::operator<<
std::ostream & operator<<(std::ostream &s, const ObsEpoch &oe) noexcept
Definition: ObsEpochMap.cpp:54
gnsstk::CommonTime
Definition: CommonTime.hpp:84
CivilTime.hpp
gnsstk::CommonTime::get
void get(long &day, long &sod, double &fsod, TimeSystem &timeSystem) const
Definition: CommonTime.cpp:213
TimeConverters.hpp
gnsstk::StringUtils::asDouble
double asDouble(const std::string &s)
Definition: StringUtils.hpp:705
GNSSTK_RETHROW
#define GNSSTK_RETHROW(exc)
Definition: Exception.hpp:369
gnsstk::CivilTime::operator!=
bool operator!=(const CivilTime &right) const
Definition: CivilTime.cpp:324
gnsstk::CivilTime::minute
int minute
Definition: CivilTime.hpp:202
gnsstk::CivilTime::isValid
virtual bool isValid() const
Returns true if this object's members are valid, false otherwise.
Definition: CivilTime.cpp:262
gnsstk::CivilTime
Definition: CivilTime.hpp:55
gnsstk::StringUtils
Definition: IonexStoreStrategy.cpp:44
gnsstk::CivilTime::reset
virtual void reset()
Reset this object to the default state.
Definition: CivilTime.cpp:273
gnsstk::CivilTime::operator<
bool operator<(const CivilTime &right) const
Definition: CivilTime.cpp:329
example6.sod
sod
Definition: example6.py:103
gnsstk::CivilTime::operator>=
bool operator>=(const CivilTime &right) const
Definition: CivilTime.cpp:398
gnsstk::CivilTime::month
int month
Definition: CivilTime.hpp:199
gnsstk::CivilTime::operator>
bool operator>(const CivilTime &right) const
Definition: CivilTime.cpp:388
gnsstk::CivilTime::second
double second
Definition: CivilTime.hpp:203
GNSSTK_THROW
#define GNSSTK_THROW(exc)
Definition: Exception.hpp:366
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::StringUtils::formattedPrint
std::string formattedPrint(const std::string &fmt, const std::string &pat, const std::string &rep, T to)
Definition: StringUtils.hpp:2020
gnsstk::CivilTime::MonthNames
static const char * MonthNames[]
Long month names for converstion from numbers to strings.
Definition: CivilTime.hpp:123
gnsstk::CivilTime::setFromInfo
virtual bool setFromInfo(const IdToValue &info)
Definition: CivilTime.cpp:185
gnsstk::StringUtils::lowerCase
std::string & lowerCase(std::string &s)
Definition: StringUtils.hpp:2108
gnsstk::CivilTime::hour
int hour
Definition: CivilTime.hpp:201
gnsstk::CommonTime::set
CommonTime & set(long day, long sod, double fsod=0.0, TimeSystem timeSystem=TimeSystem::Unknown)
Definition: CommonTime.cpp:87


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