YDSTime.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 "YDSTime.hpp"
43 #include "TimeConverters.hpp"
44 
45 namespace gnsstk
46 {
47 // YDSTime constant corresponding to CommonTime::BEGINNING_OF_TIME
48 
49 
51 
53  {
54  year = right.year;
55  doy = right.doy;
56  sod = right.sod;
57  timeSystem = right.timeSystem;
58  return *this;
59  }
60 
62  {
63  try
64  {
65  long jday = convertCalendarToJD( year, 1, 1 ) + doy - 1;
66  CommonTime ct;
67  return ct.set( jday, sod, timeSystem );
68  }
69  catch ( InvalidParameter& ip )
70  {
71  InvalidRequest ir(ip);
72  GNSSTK_THROW(ir);
73  }
74  }
75 
77  {
78  long jday = 0, secDay = 0;
79  double fsecDay = 0.0;
80  ct.get( jday, secDay, fsecDay, timeSystem );
81  sod = static_cast<double>( secDay ) + fsecDay;
82 
83  int month = 0, day = 0;
84  convertJDtoCalendar( jday, year, month, day );
85  doy = jday - convertCalendarToJD( year, 1, 1 ) + 1;
86  }
87 
88  std::string YDSTime::printf( const std::string& fmt ) const
89  {
90  try
91  {
93  std::string rv = fmt;
94 
95  rv = formattedPrint( rv, getFormatPrefixInt() + "Y",
96  "Yd", year );
97  rv = formattedPrint( rv, getFormatPrefixInt() + "y",
98  "yd", static_cast<short>(year % 100) );
99  rv = formattedPrint( rv, getFormatPrefixInt() + "j",
100  "ju", doy );
101  rv = formattedPrint( rv, getFormatPrefixFloat() + "s",
102  "sf", sod );
103  rv = formattedPrint( rv, getFormatPrefixInt() + "P",
104  "Ps", StringUtils::asString(timeSystem).c_str() );
105  return rv;
106  }
107  catch( gnsstk::StringUtils::StringException& exc)
108  {
109  GNSSTK_RETHROW( exc );
110  }
111  }
112 
113  std::string YDSTime::printError( const std::string& fmt ) const
114  {
115  try
116  {
118  std::string rv = fmt;
119 
120  rv = formattedPrint( rv, getFormatPrefixInt() + "Y",
121  "Ys", getError().c_str() );
122  rv = formattedPrint( rv, getFormatPrefixInt() + "y",
123  "ys", getError().c_str() );
124  rv = formattedPrint( rv, getFormatPrefixInt() + "j",
125  "js", getError().c_str() );
126  rv = formattedPrint( rv, getFormatPrefixFloat() + "s",
127  "ss", getError().c_str() );
128  rv = formattedPrint( rv, getFormatPrefixInt() + "P",
129  "Ps", getError().c_str() );
130  return rv;
131  }
132  catch( gnsstk::StringUtils::StringException& exc)
133  {
134  GNSSTK_RETHROW( exc );
135  }
136  }
137 
138  bool YDSTime::setFromInfo( const IdToValue& info )
139  {
140  using namespace gnsstk::StringUtils;
141 
142  for( IdToValue::const_iterator i = info.begin();
143  i != info.end(); i++ )
144  {
145  switch( i->first )
146  {
147  case 'Y':
148  year = asInt( i->second );
149  break;
150 
151  case 'y':
152  // match the POSIX strptime() function:
153  /* Year within century. When a century is not
154  * otherwise specified, values in the range 69-99
155  * refer to years in the twentieth century (1969 to
156  * 1999 inclusive); values in the range 00-68 refer
157  * to years in the twenty-first century (2000 to
158  * 2068 inclusive). */
159  if( i->second.length() > 2)
160  return false;
161  year = asInt( i->second );
162  if (year >= 69)
163  year += 1900;
164  else
165  year += 2000;
166  break;
167 
168  case 'j':
169  doy = asInt( i->second );
170  break;
171 
172  case 's':
173  sod = asDouble( i->second );
174  break;
175 
176  case 'P':
178  break;
179 
180  default:
181  // do nothing
182  break;
183  };
184  }
185 
186  return true;
187  }
188 
189  bool YDSTime::isValid() const
190  {
191  YDSTime temp;
192  temp.convertFromCommonTime( convertToCommonTime() );
193  if( *this == temp )
194  {
195  return true;
196  }
197  return false;
198  }
199 
201  {
202  year = doy = 0;
203  sod = 0.0;
205  }
206 
207  bool YDSTime::operator==( const YDSTime& right ) const
208  {
210  if ((timeSystem != TimeSystem::Any &&
211  right.timeSystem != TimeSystem::Any) &&
212  timeSystem != right.timeSystem)
213  return false;
214 
215  if( year == right.year &&
216  doy == right.doy &&
217  fabs(sod - right.sod) < CommonTime::eps )
218  {
219  return true;
220  }
221  return false;
222  }
223 
224  bool YDSTime::operator!=( const YDSTime& right ) const
225  {
226  return ( !operator==( right ) );
227  }
228 
229  bool YDSTime::operator<( const YDSTime& right ) const
230  {
232  if ((timeSystem != TimeSystem::Any &&
233  right.timeSystem != TimeSystem::Any) &&
234  timeSystem != right.timeSystem)
235  {
236  gnsstk::InvalidRequest ir("CommonTime objects not in same time system, cannot be compared");
237  GNSSTK_THROW(ir);
238  }
239 
240  if( year < right.year )
241  {
242  return true;
243  }
244  if( year > right.year )
245  {
246  return false;
247  }
248  if( doy < right.doy )
249  {
250  return true;
251  }
252  if( doy > right.doy )
253  {
254  return false;
255  }
256  if( sod < right.sod )
257  {
258  return true;
259  }
260  return false;
261  }
262 
263  bool YDSTime::operator>( const YDSTime& right ) const
264  {
265  return ( !operator<=( right ) );
266  }
267 
268  bool YDSTime::operator<=( const YDSTime& right ) const
269  {
270  return ( operator<( right ) || operator==( right ) );
271  }
272 
273  bool YDSTime::operator>=( const YDSTime& right ) const
274  {
275  return ( !operator<( right ) );
276  }
277 
278 } // namespace
279 
280 std::ostream& operator<<( std::ostream& s,
281  const gnsstk::YDSTime& yt )
282 {
283  s << yt.printf("%04Y/%03j %s %P");
284  return s;
285 }
286 
gnsstk::YDSTime::convertFromCommonTime
virtual void convertFromCommonTime(const CommonTime &ct)
Definition: YDSTime.cpp:76
gnsstk::TimeTag::getFormatPrefixInt
static std::string getFormatPrefixInt()
Definition: TimeTag.hpp:152
gnsstk::StringUtils::asInt
long asInt(const std::string &s)
Definition: StringUtils.hpp:713
YDSTime.hpp
gnsstk::YDSTime::printError
virtual std::string printError(const std::string &fmt) const
Definition: YDSTime.cpp:113
gnsstk::CommonTime::eps
static const GNSSTK_EXPORT double eps
Default tolerance for time equality in days.
Definition: CommonTime.hpp:107
example6.day
day
Definition: example6.py:66
gnsstk::YDSTime::operator==
bool operator==(const YDSTime &right) const
Definition: YDSTime.cpp:207
gnsstk::TimeTag::IdToValue
std::map< char, std::string > IdToValue
Definition: TimeTag.hpp:103
gnsstk::YDSTime
Definition: YDSTime.hpp:58
gnsstk::YDSTime::reset
virtual void reset()
Reset this object to the default state.
Definition: YDSTime.cpp:200
gnsstk::YDSTime::operator>=
bool operator>=(const YDSTime &right) const
Definition: YDSTime.cpp:273
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::TimeTag::getError
static std::string getError()
This returns the default error string for the TimeTag classes.
Definition: TimeTag.hpp:161
gnsstk::CommonTime::BEGINNING_OF_TIME
static const GNSSTK_EXPORT CommonTime BEGINNING_OF_TIME
earliest representable CommonTime
Definition: CommonTime.hpp:102
gnsstk::TimeSystem::Unknown
@ Unknown
unknown time frame; for legacy code compatibility
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
gnsstk::YDSTime::isValid
virtual bool isValid() const
Returns true if this object's members are valid, false otherwise.
Definition: YDSTime.cpp:189
gnsstk::TimeTag::timeSystem
TimeSystem timeSystem
time system (representation) of the data
Definition: TimeTag.hpp:204
example4.temp
temp
Definition: example4.py:35
gnsstk::YDSTime::operator=
YDSTime & operator=(const YDSTime &right)
Definition: YDSTime.cpp:52
gnsstk::convertJDtoCalendar
void convertJDtoCalendar(long jd, int &iyear, int &imonth, int &iday)
Definition: TimeConverters.cpp:52
gnsstk::YDSTime::doy
int doy
Definition: YDSTime.hpp:185
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::YDSTime::year
int year
Definition: YDSTime.hpp:184
gnsstk::YDSTime::BEGIN_TIME
static const GNSSTK_EXPORT YDSTime BEGIN_TIME
Definition: YDSTime.hpp:69
gnsstk::operator<<
std::ostream & operator<<(std::ostream &s, const ObsEpoch &oe) noexcept
Definition: ObsEpochMap.cpp:54
gnsstk::CommonTime
Definition: CommonTime.hpp:84
gnsstk::YDSTime::operator<
bool operator<(const YDSTime &right) const
Definition: YDSTime.cpp:229
gnsstk::YDSTime::setFromInfo
virtual bool setFromInfo(const IdToValue &info)
Definition: YDSTime.cpp:138
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::YDSTime::sod
double sod
Definition: YDSTime.hpp:186
GNSSTK_RETHROW
#define GNSSTK_RETHROW(exc)
Definition: Exception.hpp:369
gnsstk::YDSTime::convertToCommonTime
virtual CommonTime convertToCommonTime() const
Definition: YDSTime.cpp:61
gnsstk::StringUtils
Definition: IonexStoreStrategy.cpp:44
gnsstk::YDSTime::operator<=
bool operator<=(const YDSTime &right) const
Definition: YDSTime.cpp:268
GNSSTK_THROW
#define GNSSTK_THROW(exc)
Definition: Exception.hpp:366
gnsstk::convertCalendarToJD
long convertCalendarToJD(int yy, int mm, int dd)
Definition: TimeConverters.cpp:100
example6.month
month
Definition: example6.py:65
gnsstk::YDSTime::operator>
bool operator>(const YDSTime &right) const
Definition: YDSTime.cpp:263
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::YDSTime::operator!=
bool operator!=(const YDSTime &right) const
Definition: YDSTime.cpp:224
gnsstk::CommonTime::set
CommonTime & set(long day, long sod, double fsod=0.0, TimeSystem timeSystem=TimeSystem::Unknown)
Definition: CommonTime.cpp:87
gnsstk::YDSTime::printf
virtual std::string printf(const std::string &fmt) const
Definition: YDSTime.cpp:88


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