TimeTag.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 "TimeTag.hpp"
40 #include "StringUtils.hpp"
41 
42 namespace gnsstk
43 {
44  void TimeTag::scanf( const std::string& str,
45  const std::string& fmt )
46  {
47  try
48  {
49  // Get the mapping of character (from fmt) to value (from str).
50  IdToValue info;
51  getInfo( str, fmt, info );
52 
53  // Attempt to set this object using the IdToValue object
54  if( !setFromInfo( info ) )
55  {
56  gnsstk::InvalidRequest ir( "Incomplete time specification." );
57  GNSSTK_THROW( ir );
58  }
59  }
60  catch( gnsstk::StringUtils::StringException& se )
61  {
62  GNSSTK_RETHROW( se );
63  }
64  }
65 
66  void TimeTag::getInfo( const std::string& str,
67  const std::string& fmt,
68  IdToValue& info )
69  {
70  // Copy the arguments to strings we can modify.
71  std::string f = fmt;
72  std::string s = str;
73 
74  try
75  {
76  using namespace gnsstk::StringUtils;
77 
78  // Parse strings... As we process each part, it's removed from both
79  // strings so when we reach 0, we're done
80  while( !s.empty() && !f.empty() )
81  {
82  // Remove everything in f and s up to the first % in f
83  // (these parts of the strings must be identical or this will
84  // break after it tries to remove it!)
85  while ( !s.empty() &&
86  !f.empty() &&
87  ( f[0] != '%' ) )
88  {
89  // remove that character now and other whitespace
90  s.erase(0,1);
91  f.erase(0,1);
92  }
93 
94  // check just in case we hit the end of either string...
95  if ( s.empty() || f.empty() )
96  break;
97 
98  // lose the '%' in f...
99  f.erase( 0, 1 );
100 
101  std::string::size_type fieldLength = std::string::npos;
102  char delimiter = 0;
103 
104  if (false)
105  std::cout << "--------------" << std::endl
106  << "f:\"" << f << "\"" << std::endl
107  << "s:\"" << s << "\"" << std::endl;
108 
109  if( !isalpha( f[0] ) )
110  {
111  // If the format string is %03f, get '3' as the field length.
112  // This is where we have a specified field length so we should
113  // not throw away any more characters
114  fieldLength = asInt( f );
115 
116  // remove everything else up to the next character
117  // (in "%03f", that would be 'f')
118  while ( !f.empty() && !isalpha( f[0] ) )
119  f.erase( 0, 1 );
120 
121  if ( f.empty() )
122  break;
123  }
124  else
125  {
126  // When there is no field width specified, there must be a
127  // delimiter for the field.
128  if ( f.size() > 1 )
129  {
130  if ( f[1] != '%' )
131  {
132  delimiter = f[1];
133  stripLeading(s);
134  fieldLength = s.find( delimiter, 0 );
135  }
136  else
137  {
138  // if the there is no delimiter character and the
139  // next field is another part of the time to
140  // parse, assume the length of this field is 1
141  fieldLength = 1;
142  }
143  }
144  }
145 
146  // Copy the next string to be removed.
147  std::string value( s.substr( 0, fieldLength ) );
148 
149  // based on char at f[0], we know what to do...
150  info[ f[0] ] = value;
151 
152  // remove the part of str that we processed
153  stripLeading( s, value, 1 );
154 
155  // remove the character we processed from fmt
156  f.erase( 0, 1 );
157 
158  // And remove the delimiter if one was used
159  if (delimiter != 0)
160  {
161  f.erase(0,1);
162  s.erase(0,1);
163  }
164  } // end of while( (s.size() > 0) && (f.size() > 0) )
165  }
166  catch( gnsstk::StringUtils::StringException& se )
167  {
168  GNSSTK_RETHROW( se );
169  }
170  // make sure the format string was fully processed.
171  if (!f.empty())
172  {
173  gnsstk::StringUtils::StringException
174  exc("Failed to process time string");
175  GNSSTK_THROW(exc);
176  }
177  }
178 
179 
180  bool TimeTag ::
182  {
183  // We have to add an offset and the only way to do that is to
184  // change to CommonTime and back.
186  bool rv = ct.changeTimeSystem(timeSys, conv);
187  if (rv)
189  return rv;
190  }
191 
192 
193  bool TimeTag ::
195  {
196  // We have to add an offset and the only way to do that is to
197  // change to CommonTime and back.
199  bool rv = ct.changeTimeSystem(timeSys);
200  if (rv)
202  return rv;
203  }
204 
205 
206  void TimeTag ::
208  {
209  if ((ts1 != TimeSystem::Any) &&
210  (ts2 != TimeSystem::Any) &&
211  (ts1 != ts2))
212  {
213  InvalidRequest exc("TimeTag objects not in same time system,"
214  " cannot be compared");
215  GNSSTK_THROW(exc);
216  }
217  }
218 
219 } // namespace
220 
221 std::ostream& operator<<( std::ostream& s,
222  const gnsstk::TimeTag& t )
223 {
224  s << t.printf( t.getDefaultFormat() );
225  return s;
226 }
gnsstk::StringUtils::asInt
long asInt(const std::string &s)
Definition: StringUtils.hpp:713
se
double se
obliquity cos, T*cos, sin coefficients
Definition: IERS2003NutationData.hpp:48
gnsstk::TimeTag::convertFromCommonTime
virtual void convertFromCommonTime(const CommonTime &ct)=0
gnsstk::TimeTag::IdToValue
std::map< char, std::string > IdToValue
Definition: TimeTag.hpp:103
StringUtils.hpp
gnsstk::TimeTag::getDefaultFormat
virtual std::string getDefaultFormat() const =0
Return a string containing the default format to use in printing.
gnsstk::TimeSystem::Any
@ Any
wildcard; allows comparison with any other type
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::TimeSystemConverter
Definition: TimeSystemConverter.hpp:58
gnsstk::TimeTag
Definition: TimeTag.hpp:58
gnsstk::TimeTag::scanf
virtual void scanf(const std::string &str, const std::string &fmt)
Definition: TimeTag.cpp:44
gnsstk::operator<<
std::ostream & operator<<(std::ostream &s, const ObsEpoch &oe) noexcept
Definition: ObsEpochMap.cpp:54
gnsstk::CommonTime
Definition: CommonTime.hpp:84
gnsstk::TimeSystem
TimeSystem
Definition of various time systems.
Definition: TimeSystem.hpp:51
gnsstk::TimeTag::convertToCommonTime
virtual CommonTime convertToCommonTime() const =0
TimeTag.hpp
GNSSTK_RETHROW
#define GNSSTK_RETHROW(exc)
Definition: Exception.hpp:369
gnsstk::StringUtils
Definition: IonexStoreStrategy.cpp:44
gnsstk::TimeTag::changeTimeSystem
bool changeTimeSystem(TimeSystem timeSys, TimeSystemConverter *conv)
Definition: TimeTag.cpp:181
gnsstk::TimeTag::getInfo
static void getInfo(const std::string &str, const std::string &fmt, IdToValue &info)
Definition: TimeTag.cpp:66
gnsstk::TimeTag::printf
virtual std::string printf(const std::string &fmt) const =0
GNSSTK_THROW
#define GNSSTK_THROW(exc)
Definition: Exception.hpp:366
gnsstk::TimeTag::setFromInfo
virtual bool setFromInfo(const IdToValue &info)=0
gnsstk::CommonTime::changeTimeSystem
bool changeTimeSystem(TimeSystem timeSystem, TimeSystemConverter *conv)
Definition: CommonTime.cpp:190
gnsstk::TimeTag::checkTimeSystem
static void checkTimeSystem(TimeSystem ts1, TimeSystem ts2)
Definition: TimeTag.cpp:207


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