PosixTime.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 "PosixTime.hpp"
42 #include "TimeConstants.hpp"
43 
44 namespace gnsstk
45 {
47  {
48  ts.tv_sec = right.ts.tv_sec ;
49  ts.tv_nsec = right.ts.tv_nsec;
50  timeSystem = right.timeSystem;
51  return *this;
52  }
53 
55  {
56  try
57  {
58  CommonTime ct;
59  return ct.set( ( MJD_JDAY + UNIX_MJD + ts.tv_sec / SEC_PER_DAY ),
60  ( ts.tv_sec % SEC_PER_DAY ),
61  ( static_cast<double>( ts.tv_nsec ) * 1e-9 ),
62  timeSystem );
63  }
64  catch (InvalidParameter& ip)
65  {
66  InvalidRequest ir(ip);
67  GNSSTK_THROW(ip);
68  }
69  }
70 
72  {
74  static const CommonTime MIN_CT = PosixTime(0, 0, TimeSystem::Any);
77  static const CommonTime MAX_CT = PosixTime(2147483647, 999999,
79 
80  if ( ct < MIN_CT || ct > MAX_CT )
81  {
82  InvalidRequest ir("Unable to convert given CommonTime to PosixTime.");
83  GNSSTK_THROW(ir);
84  }
85 
86  long jday, sod;
87  double fsod;
88  ct.get( jday, sod, fsod, timeSystem );
89 
90  ts.tv_sec = (jday - MJD_JDAY - UNIX_MJD) * SEC_PER_DAY + sod;
91 
92  // round to the nearest nanosecond
93  ts.tv_nsec = static_cast<time_t>( fsod * 1e9 + 0.5 ) ;
94 
95  if (ts.tv_nsec >= 1000000000)
96  {
97  ts.tv_nsec -= 1000000000;
98  ++ts.tv_sec;
99  }
100  }
101 
102  std::string PosixTime::printf( const std::string& fmt ) const
103  {
104  try
105  {
107  std::string rv( fmt );
108 
109  rv = formattedPrint(rv, getFormatPrefixInt() + "W",
110  "Wlu", ts.tv_sec);
111  rv = formattedPrint(rv, getFormatPrefixInt() + "N",
112  "Nlu", ts.tv_nsec);
113  rv = formattedPrint(rv, getFormatPrefixInt() + "P",
114  "Ps", StringUtils::asString(timeSystem).c_str() );
115  return rv;
116  }
117  catch( gnsstk::StringUtils::StringException& se )
118  {
119  GNSSTK_RETHROW( se );
120  }
121  }
122 
123  std::string PosixTime::printError( const std::string& fmt ) const
124  {
125  try
126  {
128  std::string rv( fmt );
129 
130  rv = formattedPrint(rv, getFormatPrefixInt() + "W",
131  "Ws", getError().c_str());
132  rv = formattedPrint(rv, getFormatPrefixInt() + "N",
133  "Ns", getError().c_str());
134  rv = formattedPrint( rv, getFormatPrefixInt() + "P",
135  "Ps", getError().c_str());
136  return rv;
137  }
138  catch( gnsstk::StringUtils::StringException& se )
139  {
140  GNSSTK_RETHROW( se );
141  }
142  }
143 
144  bool PosixTime::setFromInfo( const IdToValue& info )
145  {
146  using namespace gnsstk::StringUtils;
147 
148  for( IdToValue::const_iterator i = info.begin(); i != info.end(); i++ )
149  {
150  switch( i->first )
151  {
152  case 'W':
153  ts.tv_sec = asInt( i->second );
154  break;
155 
156  case 'N':
157  ts.tv_nsec = asInt( i->second );
158  break;
159 
160  case 'P':
162  break;
163 
164  default:
165  // do nothing
166  break;
167  };
168  }
169 
170  return true;
171  }
172 
173  bool PosixTime::isValid() const
174  {
175  PosixTime temp;
176  temp.convertFromCommonTime( convertToCommonTime() );
177  if( *this == temp )
178  {
179  return true;
180  }
181  return false;
182  }
183 
185  {
186  ts.tv_sec = ts.tv_nsec = 0;
188  }
189 
190  bool PosixTime::operator==( const PosixTime& right ) const
191  {
194  if ((timeSystem != TimeSystem::Any &&
195  right.timeSystem != TimeSystem::Any) &&
196  timeSystem != right.timeSystem)
197  return false;
198 
199  if( ts.tv_sec == right.ts.tv_sec &&
200  abs(ts.tv_nsec - right.ts.tv_nsec) < CommonTime::eps )
201  {
202  return true;
203  }
204  return false;
205  }
206 
207  bool PosixTime::operator!=( const PosixTime& right ) const
208  {
209  return ( !operator==( right ) );
210  }
211 
212  bool PosixTime::operator<( const PosixTime& right ) const
213  {
216  if ((timeSystem != TimeSystem::Any &&
217  right.timeSystem != TimeSystem::Any) &&
218  timeSystem != right.timeSystem)
219  {
220  gnsstk::InvalidRequest ir("CommonTime objects not in same time system,"
221  " cannot be compared");
222  GNSSTK_THROW(ir);
223  }
224 
225  if( ts.tv_sec < right.ts.tv_sec )
226  {
227  return true;
228  }
229  if( ts.tv_sec == right.ts.tv_sec &&
230  ts.tv_nsec < right.ts.tv_nsec )
231  {
232  return true;
233  }
234  return false;
235  }
236 
237  bool PosixTime::operator>( const PosixTime& right ) const
238  {
239  return ( !operator<=( right ) );
240  }
241 
242  bool PosixTime::operator<=( const PosixTime& right ) const
243  {
244  return ( operator<( right ) ||
245  operator==( right ) );
246  }
247 
248  bool PosixTime::operator>=( const PosixTime& right ) const
249  {
250  return ( !operator<( right ) );
251  }
252 
253 } // namespace
gnsstk::TimeTag::getFormatPrefixInt
static std::string getFormatPrefixInt()
Definition: TimeTag.hpp:152
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::CommonTime::eps
static const GNSSTK_EXPORT double eps
Default tolerance for time equality in days.
Definition: CommonTime.hpp:107
gnsstk::PosixTime::printError
virtual std::string printError(const std::string &fmt) const
Definition: PosixTime.cpp:123
TimeConstants.hpp
gnsstk::TimeTag::IdToValue
std::map< char, std::string > IdToValue
Definition: TimeTag.hpp:103
gnsstk::PosixTime::PosixTime
PosixTime(int sec=0, int nsec=0, TimeSystem tsys=TimeSystem::Unknown)
Definition: PosixTime.hpp:75
gnsstk::SEC_PER_DAY
const long SEC_PER_DAY
Seconds per day.
Definition: TimeConstants.hpp:63
gnsstk::PosixTime::setFromInfo
virtual bool setFromInfo(const IdToValue &info)
Definition: PosixTime.cpp:144
gnsstk::PosixTime::operator>
virtual bool operator>(const PosixTime &right) const
Definition: PosixTime.cpp:237
gnsstk::PosixTime::convertToCommonTime
virtual CommonTime convertToCommonTime() const
Definition: PosixTime.cpp:54
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::MJD_JDAY
const long MJD_JDAY
'Julian day' offset from MJD
Definition: TimeConstants.hpp:53
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
PosixTime.hpp
gnsstk::PosixTime::ts
struct timespec ts
Definition: PosixTime.hpp:187
gnsstk::UNIX_MJD
const long UNIX_MJD
Modified Julian Date of UNIX epoch (Jan. 1, 1970).
Definition: TimeConstants.hpp:55
gnsstk::StringUtils::asTimeSystem
TimeSystem asTimeSystem(const std::string &s)
Convert a string representation of TimeSystem to an enum.
Definition: TimeSystem.cpp:324
gnsstk::PosixTime::operator!=
virtual bool operator!=(const PosixTime &right) const
Definition: PosixTime.cpp:207
gnsstk::PosixTime::printf
virtual std::string printf(const std::string &fmt) const
Definition: PosixTime.cpp:102
gnsstk::PosixTime::operator=
PosixTime & operator=(const PosixTime &right)
Definition: PosixTime.cpp:46
gnsstk::CommonTime
Definition: CommonTime.hpp:84
gnsstk::PosixTime::operator==
virtual bool operator==(const PosixTime &right) const
Definition: PosixTime.cpp:190
gnsstk::CommonTime::get
void get(long &day, long &sod, double &fsod, TimeSystem &timeSystem) const
Definition: CommonTime.cpp:213
gnsstk::PosixTime::operator<=
virtual bool operator<=(const PosixTime &right) const
Definition: PosixTime.cpp:242
GNSSTK_RETHROW
#define GNSSTK_RETHROW(exc)
Definition: Exception.hpp:369
gnsstk::PosixTime::operator<
virtual bool operator<(const PosixTime &right) const
Definition: PosixTime.cpp:212
gnsstk::StringUtils
Definition: IonexStoreStrategy.cpp:44
example6.sod
sod
Definition: example6.py:103
gnsstk::PosixTime::isValid
virtual bool isValid() const
Returns true if this object's members are valid, false otherwise.
Definition: PosixTime.cpp:173
gnsstk::PosixTime::convertFromCommonTime
virtual void convertFromCommonTime(const CommonTime &ct)
Definition: PosixTime.cpp:71
gnsstk::PosixTime
Definition: PosixTime.hpp:62
GNSSTK_THROW
#define GNSSTK_THROW(exc)
Definition: Exception.hpp:366
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::PosixTime::reset
virtual void reset()
Reset this object to the default state.
Definition: PosixTime.cpp:184
gnsstk::PosixTime::operator>=
virtual bool operator>=(const PosixTime &right) const
Definition: PosixTime.cpp:248
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:40