UnixTime.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 "UnixTime.hpp"
42 #include "TimeConstants.hpp"
43 
44 namespace gnsstk
45 {
47  {
48  tv.tv_sec = right.tv.tv_sec ;
49  tv.tv_usec = right.tv.tv_usec;
50  timeSystem = right.timeSystem;
51  return *this;
52  }
53 
55  {
56  try
57  {
58  CommonTime ct;
59  return ct.set( ( MJD_JDAY + UNIX_MJD + tv.tv_sec / SEC_PER_DAY ),
60  ( tv.tv_sec % SEC_PER_DAY ),
61  ( static_cast<double>( tv.tv_usec ) * 1e-6 ),
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 = UnixTime(0, 0, TimeSystem::Any);
77  static const CommonTime MAX_CT = UnixTime(2147483647, 999999,
79 
80  // Only check the min/max range if we're using 32-bit time_t.
81  // If we're using 64-bit, the time range of struct timeval
82  // will be well beyond what CommonTime can represent.
83  if ((sizeof(tv.tv_sec) <= 4) && ((ct < MIN_CT) || (ct > MAX_CT)))
84  {
85  InvalidRequest ir("Unable to convert given CommonTime to UnixTime.");
86  GNSSTK_THROW(ir);
87  }
88 
89  long jday, sod;
90  double fsod;
91  ct.get( jday, sod, fsod, timeSystem );
92 
93  tv.tv_sec = (jday - MJD_JDAY - UNIX_MJD) * SEC_PER_DAY + sod;
94 
95  // round to the nearest microsecond
96  tv.tv_usec = static_cast<time_t>( fsod * 1e6 + 0.5 ) ;
97 
98  if (tv.tv_usec >= 1000000)
99  {
100  tv.tv_usec -= 1000000;
101  ++tv.tv_sec;
102  }
103  }
104 
105  std::string UnixTime::printf( const std::string& fmt ) const
106  {
107  try
108  {
110  std::string rv( fmt );
111 
112  rv = formattedPrint(rv, getFormatPrefixInt() + "U",
113  "Ulu", tv.tv_sec);
114  rv = formattedPrint(rv, getFormatPrefixInt() + "u",
115  "ulu", tv.tv_usec);
116  rv = formattedPrint(rv, getFormatPrefixInt() + "P",
117  "Ps", StringUtils::asString(timeSystem).c_str() );
118  return rv;
119  }
120  catch( gnsstk::StringUtils::StringException& se )
121  {
122  GNSSTK_RETHROW( se );
123  }
124  }
125 
126  std::string UnixTime::printError( const std::string& fmt ) const
127  {
128  try
129  {
131  std::string rv( fmt );
132 
133  rv = formattedPrint(rv, getFormatPrefixInt() + "U",
134  "Us", getError().c_str());
135  rv = formattedPrint(rv, getFormatPrefixInt() + "u",
136  "us", getError().c_str());
137  rv = formattedPrint( rv, getFormatPrefixInt() + "P",
138  "Ps", getError().c_str());
139  return rv;
140  }
141  catch( gnsstk::StringUtils::StringException& se )
142  {
143  GNSSTK_RETHROW( se );
144  }
145  }
146 
147  bool UnixTime::setFromInfo( const IdToValue& info )
148  {
149  using namespace gnsstk::StringUtils;
150 
151  for( IdToValue::const_iterator i = info.begin(); i != info.end(); i++ )
152  {
153  switch( i->first )
154  {
155  case 'U':
156  tv.tv_sec = asInt( i->second );
157  break;
158 
159  case 'u':
160  tv.tv_usec = asInt( i->second );
161  break;
162 
163  case 'P':
165  break;
166 
167  default:
168  // do nothing
169  break;
170  };
171  }
172 
173  return true;
174  }
175 
176  bool UnixTime::isValid() const
177  {
178  UnixTime temp;
179  temp.convertFromCommonTime( convertToCommonTime() );
180  if( *this == temp )
181  {
182  return true;
183  }
184  return false;
185  }
186 
188  {
189  tv.tv_sec = tv.tv_usec = 0;
191  }
192 
193  bool UnixTime::operator==( const UnixTime& right ) const
194  {
196  if ((timeSystem != TimeSystem::Any &&
197  right.timeSystem != TimeSystem::Any) &&
198  timeSystem != right.timeSystem)
199  return false;
200 
201  if( tv.tv_sec == right.tv.tv_sec &&
202  abs(tv.tv_usec - right.tv.tv_usec) < CommonTime::eps )
203  {
204  return true;
205  }
206  return false;
207  }
208 
209  bool UnixTime::operator!=( const UnixTime& right ) const
210  {
211  return ( !operator==( right ) );
212  }
213 
214  bool UnixTime::operator<( const UnixTime& right ) const
215  {
217  if ((timeSystem != TimeSystem::Any &&
218  right.timeSystem != TimeSystem::Any) &&
219  timeSystem != right.timeSystem)
220  {
221  gnsstk::InvalidRequest ir("CommonTime objects not in same time system, cannot be compared");
222  GNSSTK_THROW(ir)
223  }
224 
225  if( tv.tv_sec < right.tv.tv_sec )
226  {
227  return true;
228  }
229  if( tv.tv_sec == right.tv.tv_sec &&
230  tv.tv_usec < right.tv.tv_usec )
231  {
232  return true;
233  }
234  return false;
235  }
236 
237  bool UnixTime::operator>( const UnixTime& right ) const
238  {
239  return ( !operator<=( right ) );
240  }
241 
242  bool UnixTime::operator<=( const UnixTime& right ) const
243  {
244  return ( operator<( right ) ||
245  operator==( right ) );
246  }
247 
248  bool UnixTime::operator>=( const UnixTime& 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
TimeConstants.hpp
gnsstk::UnixTime::tv
struct timeval tv
Definition: UnixTime.hpp:192
gnsstk::TimeTag::IdToValue
std::map< char, std::string > IdToValue
Definition: TimeTag.hpp:103
gnsstk::SEC_PER_DAY
const long SEC_PER_DAY
Seconds per day.
Definition: TimeConstants.hpp:63
gnsstk::UnixTime::printf
virtual std::string printf(const std::string &fmt) const
Definition: UnixTime.cpp:105
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::UnixTime::convertToCommonTime
virtual CommonTime convertToCommonTime() const
Definition: UnixTime.cpp:54
gnsstk::MJD_JDAY
const long MJD_JDAY
'Julian day' offset from MJD
Definition: TimeConstants.hpp:53
gnsstk::UnixTime::operator<=
virtual bool operator<=(const UnixTime &right) const
Definition: UnixTime.cpp:242
gnsstk::TimeSystem::Unknown
@ Unknown
unknown time frame; for legacy code compatibility
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
gnsstk::UnixTime::operator!=
virtual bool operator!=(const UnixTime &right) const
Definition: UnixTime.cpp:209
gnsstk::TimeTag::timeSystem
TimeSystem timeSystem
time system (representation) of the data
Definition: TimeTag.hpp:204
example4.temp
temp
Definition: example4.py:35
gnsstk::UnixTime::setFromInfo
virtual bool setFromInfo(const IdToValue &info)
Definition: UnixTime.cpp:147
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::UnixTime::operator>=
virtual bool operator>=(const UnixTime &right) const
Definition: UnixTime.cpp:248
gnsstk::UnixTime::operator==
virtual bool operator==(const UnixTime &right) const
Definition: UnixTime.cpp:193
gnsstk::CommonTime
Definition: CommonTime.hpp:84
gnsstk::UnixTime::operator=
UnixTime & operator=(const UnixTime &right)
Definition: UnixTime.cpp:46
UnixTime.hpp
gnsstk::CommonTime::get
void get(long &day, long &sod, double &fsod, TimeSystem &timeSystem) const
Definition: CommonTime.cpp:213
gnsstk::UnixTime::operator>
virtual bool operator>(const UnixTime &right) const
Definition: UnixTime.cpp:237
GNSSTK_RETHROW
#define GNSSTK_RETHROW(exc)
Definition: Exception.hpp:369
gnsstk::UnixTime
Definition: UnixTime.hpp:67
gnsstk::UnixTime::reset
virtual void reset()
Reset this object to the default state.
Definition: UnixTime.cpp:187
gnsstk::StringUtils
Definition: IonexStoreStrategy.cpp:44
gnsstk::UnixTime::printError
virtual std::string printError(const std::string &fmt) const
Definition: UnixTime.cpp:126
example6.sod
sod
Definition: example6.py:103
gnsstk::UnixTime::isValid
virtual bool isValid() const
Returns true if this object's members are valid, false otherwise.
Definition: UnixTime.cpp:176
GNSSTK_THROW
#define GNSSTK_THROW(exc)
Definition: Exception.hpp:366
gnsstk::UnixTime::UnixTime
UnixTime(int sec=0, int usec=0, TimeSystem ts=TimeSystem::Unknown)
Definition: UnixTime.hpp:80
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::UnixTime::convertFromCommonTime
virtual void convertFromCommonTime(const CommonTime &ct)
Definition: UnixTime.cpp:71
gnsstk::UnixTime::operator<
virtual bool operator<(const UnixTime &right) const
Definition: UnixTime.cpp:214
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:42