timeconvert.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 
251 #include "NewNavInc.h"
252 #include <gnsstk/BasicFramework.hpp>
253 
254 #include <gnsstk/TimeString.hpp>
255 #include <gnsstk/TimeConstants.hpp>
256 
257 #include <gnsstk/ANSITime.hpp>
258 #include <gnsstk/CivilTime.hpp>
259 #include <gnsstk/GPSWeekSecond.hpp>
260 #include <gnsstk/GPSWeekZcount.hpp>
261 #include <gnsstk/JulianDate.hpp>
262 #include <gnsstk/MJD.hpp>
263 #include <gnsstk/UnixTime.hpp>
264 #include <gnsstk/YDSTime.hpp>
265 #include <gnsstk/SystemTime.hpp>
266 
267 #include <gnsstk/CommandOptionWithCommonTimeArg.hpp>
268 #include <gnsstk/EnumIterator.hpp>
269 #include <gnsstk/BasicTimeSystemConverter.hpp>
270 #include <gnsstk/NavTimeSystemConverter.hpp>
271 #include <gnsstk/MultiFormatNavDataFactory.hpp>
272 
273 using namespace std;
274 using namespace gnsstk;
275 
277 {
278 public:
286  CommandOptionHelpTimeSystem(const char shOpt,
287  const std::string& loOpt,
288  const std::string& desc)
289  : CommandOptionHelp(gnsstk::CommandOption::noArgument, shOpt, loOpt,
290  desc)
291  {
292  }
293 
297  void printHelp(std::ostream& out, bool pretty = true) override
298  {
299  out << "Available Time Systems:" << endl;
301  {
302  cout << " " << gnsstk::StringUtils::asString(e) << endl;
303  }
304  }
305 };
306 
307 
308 class TimCvt : public BasicFramework
309 {
310 public:
311  TimCvt(char* arg0);
312 
313 protected:
314  virtual void process();
315 
316 private:
333 
337 
345 
347  string timeSpec;
350 };
351 
352 
353 TimCvt::TimCvt(char* arg0)
354  : BasicFramework(arg0, "Converts from a given input time specification"
355  " to other time formats. Include the quotation marks."
356  " All year values are four digit years.\n\nWhen"
357  " converting between time systems, GPS is the default"
358  " source time system when a time is specified, while UTC"
359  " is the default source time system when a time is not"
360  " specified."),
361  ANSITimeOption('A', "ansi", "%K", "\"ANSI-Second\""),
362  CivilTimeOption('c', "civil", "%m %d %Y %H:%M:%f",
363  "\"Month(numeric) DayOfMonth Year"
364  " Hour:Minute:Second\""),
365  RinexFileTimeOption('R', "rinex-file", "%y %m %d %H %M %S",
366  "\"Year(2-digit) Month(numeric) DayOfMonth Hour"
367  " Minute Second\""),
368  GPSEWSOption('o', "ews", "%E %G %g",
369  "\"GPSEpoch 10bitGPSweek SecondOfWeek\""),
370  GPSWSOption('f', "ws", "%F %g", "\"FullGPSWeek SecondOfWeek\""),
371  GPSWZOption('w', "wz", "%F %Z", "\"FullGPSWeek Zcount\""),
372  GPSZ29Option(0, "z29", "%E %c", "\"29bitZcount\""),
373  GPSZ32Option('Z', "z32", "%C", "\"32bitZcount\""),
374  JDOption('j', "julian", "%J", "\"JulianDate\""),
375  MJDOption('m', "mjd", "%Q", "\"ModifiedJulianDate\""),
376  UnixTimeOption('u',"unixtime", "%U %u",
377  "\"UnixSeconds UnixMicroseconds\""),
378  YDSTimeOption('y', "doy", "%Y %j %s",
379  "\"Year DayOfYear SecondsOfDay\""),
380  inputFormatOption(0, "input-format", "Time format to use on input"),
381  inputTimeOption(0, "input-time",
382  "Time to be parsed by \"input-format\" option"),
383  formatOption('F', "format", "Time format to use on output"),
384  addOption('a', "add-offset", "Add NUM seconds to specified time"),
385  subOption('s', "sub-offset",
386  "Subtract NUM seconds from specified time"),
387  timeSystemHelpOption(0, "systems", "List available time systems"),
388  srcTSOption(0, "src-sys", "Source time system when converting between"
389  " systems"),
390  tgtTSOption(0, "tgt-sys", "Target time system when converting between"
391  " systems"),
392  navOption(0, "nav", "if you see this, we failed"),
393  offsOnlyOption(0, "offset", "Only display the offset at the reference"
394  " time"),
395  offsetTgtTSOption(&tgtTSOption, &offsOnlyOption)
396 {
397  // Initialize these two items in here rather than in the
398  // initializer list to guarantee execution order and avoid seg
399  // faults.
400  ndfp = std::make_shared<gnsstk::MultiFormatNavDataFactory>();
401  // process only time offset messages
402  ndfp->setTypeFilter({NavMessageType::TimeOffset});
403  navOption.setDescription("Where to get the navigation data. Can be " +
404  ndfp->getFactoryFormats() + ".");
420 
425 
439 
442 }
443 
444 
446 {
447  CommonTime ct;
448  ct.setTimeSystem(TimeSystem::GPS);
449  CommandOption *whichOpt = mutexOption.whichOne();
450 
451  if (navOption.getCount())
452  {
453  // create our time system converter
454  CommonTime::tsConv = make_shared<NavTimeSystemConverter>();
455  NavTimeSystemConverter *ntsc = dynamic_cast<NavTimeSystemConverter*>(
456  CommonTime::tsConv.get());
457  // create our NavLibrary and associate it with the
458  // NavTimeSystemConverter
459  ntsc->navLib = make_shared<NavLibrary>();
460  // Set the NavLibrary to use the MultiFormatNavDataFactory
461  ntsc->navLib->addFactory(ndfp);
462  // load the data files.
463  std::vector<string> values(navOption.getValue());
464  for (size_t i=0; i<values.size(); i++)
465  {
466  if (!ndfp->addDataSource(values[i]))
467  {
468  cerr << "Unable to load \"" << values[i] << "\"" << endl;
469  // this could be a missing or invalid file either one.
470  exitCode = BasicFramework::EXIST_ERROR;
471  // We continue because it's possible that the user
472  // specified a valid file in addition to this invalid
473  // one. But we still want to indicate the error with
474  // the message and exit code.
475  }
476  }
477  switch (debugLevel)
478  {
479  case 0:
480  break;
481  case 1:
482  ndfp->dump(std::cerr, gnsstk::DumpDetail::OneLine);
483  break;
484  case 2:
485  ndfp->dump(std::cerr, gnsstk::DumpDetail::Brief);
486  break;
487  default:
488  ndfp->dump(std::cerr, gnsstk::DumpDetail::Full);
489  break;
490  }
491  }
492  else
493  {
494  CommonTime::tsConv = make_shared<BasicTimeSystemConverter>();
495  }
496 
497  if (whichOpt)
498  {
500  dynamic_cast<CommandOptionWithCommonTimeArg *>(whichOpt);
501  if (cta)
502  {
503  ct = cta->getTime().front();
504  if (srcTSOption.getCount())
505  {
507  srcTSOption.getValue()[0]));
508  }
509  else
510  {
511  ct.setTimeSystem(TimeSystem::GPS);
512  }
513  }
514  else // whichOpt == &inputFormatAndTimeOption
515  {
516  mixedScanTime( ct,
517  inputTimeOption.getValue().front(),
518  inputFormatOption.getValue().front() );
519  if (srcTSOption.getCount())
520  {
522  srcTSOption.getValue()[0]));
523  }
524  else
525  {
526  ct.setTimeSystem(TimeSystem::GPS);
527  }
528  }
529  }
530  else
531  {
532  ct = SystemTime();
533  ct.setTimeSystem(TimeSystem::UTC);
534  }
535 
536  int i;
537  int addOptions = addOption.getCount();
538  int subOptions = subOption.getCount();
539  for (i = 0; i < addOptions; i++)
541  for (i = 0; i < subOptions; i++)
543 
544  if (tgtTSOption.getCount())
545  {
547  tgtTSOption.getValue()[0]);
548  if (offsOnlyOption)
549  {
550  double offset;
551  if (!CommonTime::tsConv->getOffset(ct.getTimeSystem(), ts, ct, offset))
552  {
553  cerr << "Unable to change time systems" << endl;
554  exitCode = 1;
555  }
556  else
557  {
558  cout << setprecision(20) << offset << endl;
559  }
560  return;
561  }
562  else
563  {
564  if (!ct.changeTimeSystem(ts))
565  {
566  cerr << "Unable to change time systems" << endl;
567  exitCode = 1;
568  return;
569  }
570  }
571  }
572 
573  if (formatOption.getCount())
574  {
575  cout << printTime(ct, formatOption.getValue()[0]) << endl;
576  }
577  else
578  {
580  string eight(8, ' '); // eight spaces
581 
582  GPSWeekZcount wz(ct);
583  CivilTime civ(ct);
584 
585  cout << endl
586  << eight << leftJustify("Month/Day/Year H:M:S", 32)
587  << CivilTime(ct).printf("%02m/%02d/%04Y %02H:%02M:%02S") << endl
588 
589  << eight << leftJustify("Modified Julian Date", 32)
590  << setprecision(15) << MJD(ct).printf("%15.9Q") << endl
591 
592  << eight << leftJustify("GPSweek DayOfWeek SecOfWeek", 32)
593  << GPSWeekSecond(ct).printf("%G %w % 13.6g") << endl
594 
595  << eight << leftJustify("FullGPSweek Zcount", 32)
596  << wz.printf("%F % 6z") << endl
597 
598  << eight << leftJustify("Year DayOfYear SecondOfDay", 32)
599  << YDSTime(ct).printf("%Y %03j % 12.6s") << endl
600 
601  << eight << leftJustify("Unix: Second Microsecond", 32)
602  << UnixTime(ct).printf("%U % 6u") << endl
603 
604  << eight << leftJustify("Zcount: 29-bit (32-bit)", 32)
605  << wz.printf("%c (%C)") << endl
606 
607  << endl << endl;
608  }
609 
610  return;
611 }
612 
613 
614 int main(int argc, char* argv[])
615 {
616 #include "NewNavInit.h"
617  try
618  {
619  TimCvt m(argv[0]);
620  if (!m.initialize(argc, argv))
621  return m.exitCode;
622  m.run();
623  return m.exitCode;
624  }
625  catch(Exception& e)
626  {
627  cout << e << endl;
628  }
629  catch(std::exception& e)
630  {
631  cout << e.what() << endl;
632  }
633  catch(...)
634  {
635  cout << "unknown error" << endl;
636  }
637  // only reach this point if an exception was caught
638  return BasicFramework::EXCEPTION_ERROR;
639 }
TimCvt::inputFormatOption
CommandOptionWithAnyArg inputFormatOption
Definition: timeconvert.cpp:334
CommandOptionHelpTimeSystem::printHelp
void printHelp(std::ostream &out, bool pretty=true) override
Definition: timeconvert.cpp:297
TimCvt::GPSWZOption
CommandOptionWithCommonTimeArg GPSWZOption
Definition: timeconvert.cpp:322
TimCvt::offsOnlyOption
CommandOptionNoArg offsOnlyOption
Definition: timeconvert.cpp:338
TimCvt::GPSEWSOption
CommandOptionWithCommonTimeArg GPSEWSOption
Definition: timeconvert.cpp:320
TimCvt::offsetOrFormatOption
CommandOptionMutex offsetOrFormatOption
Definition: timeconvert.cpp:340
CommandOptionHelpTimeSystem
Definition: timeconvert.cpp:276
gnsstk::CommandOptionOneOf::addOption
void addOption(CommandOption *opt)
Add an option to the list of mutually exclusive options.
Definition: CommandOption.cpp:396
gnsstk::YDSTime
Definition: YDSTime.hpp:58
gnsstk::BasicFramework::debugLevel
int debugLevel
Debug level for this run of the program.
Definition: BasicFramework.hpp:455
gnsstk::CommandOptionAllOf
Definition: CommandOption.hpp:575
main
int main(int argc, char *argv[])
Definition: timeconvert.cpp:614
gnsstk::UnixTime::printf
virtual std::string printf(const std::string &fmt) const
Definition: UnixTime.cpp:105
gnsstk::NavTimeSystemConverter
Definition: NavTimeSystemConverter.hpp:87
TimCvt::timeSystemHelpOption
CommandOptionHelpTimeSystem timeSystemHelpOption
Definition: timeconvert.cpp:329
gnsstk::Exception::what
std::string what() const
Dump to a string.
Definition: Exception.cpp:193
gnsstk::CommandOptionMutex
Definition: CommandOption.hpp:607
gnsstk::CommandOption::getCount
virtual unsigned long getCount() const
Definition: CommandOption.hpp:188
gnsstk::StringUtils::asString
std::string asString(IonexStoreStrategy e)
Convert a IonexStoreStrategy to a whitespace-free string name.
Definition: IonexStoreStrategy.cpp:46
TimCvt::GPSWSOption
CommandOptionWithCommonTimeArg GPSWSOption
Definition: timeconvert.cpp:321
gnsstk::CommonTime::setTimeSystem
CommonTime & setTimeSystem(TimeSystem timeSystem)
Definition: CommonTime.hpp:195
TimCvt::mutexOption
CommandOptionMutex mutexOption
Definition: timeconvert.cpp:344
gnsstk::CommandOptionWithAnyArg
Definition: CommandOption.hpp:342
TimCvt::srcTSOption
CommandOptionWithAnyArg srcTSOption
Source time system.
Definition: timeconvert.cpp:330
gnsstk::TimeSystemIterator
EnumIterator< TimeSystem, TimeSystem::Unknown, TimeSystem::Last > TimeSystemIterator
Definition: TimeSystem.hpp:76
TimCvt::inputFormatAndTimeOption
CommandOptionAllOf inputFormatAndTimeOption
Definition: timeconvert.cpp:336
gnsstk::CommandOptionNoArg
Definition: CommandOption.hpp:295
gnsstk::CommandOptionDependent
Definition: CommandOption.hpp:641
TimCvt::process
virtual void process()
Definition: timeconvert.cpp:445
gnsstk::CommandOptionOneOf::whichOne
CommandOption * whichOne() const
Definition: CommandOption.cpp:428
TimCvt::inputTimeOption
CommandOptionWithAnyArg inputTimeOption
Definition: timeconvert.cpp:335
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
TimCvt::formatOption
CommandOptionWithAnyArg formatOption
Definition: timeconvert.cpp:339
TimCvt::RinexFileTimeOption
CommandOptionWithCommonTimeArg RinexFileTimeOption
Definition: timeconvert.cpp:319
TimCvt::TimCvt
TimCvt(char *arg0)
Definition: timeconvert.cpp:353
gnsstk::GPSWeekSecond
Definition: GPSWeekSecond.hpp:56
gnsstk::NavDataFactoryPtr
std::shared_ptr< NavDataFactory > NavDataFactoryPtr
Managed pointer to NavDataFactory.
Definition: NavDataFactory.hpp:398
gnsstk::Exception
Definition: Exception.hpp:151
TimCvt::timeSpec
string timeSpec
Definition: timeconvert.cpp:347
CommandOptionHelpTimeSystem::CommandOptionHelpTimeSystem
CommandOptionHelpTimeSystem(const char shOpt, const std::string &loOpt, const std::string &desc)
Definition: timeconvert.cpp:286
gnsstk::mixedScanTime
void mixedScanTime(CommonTime &t, const string &str, const string &fmt)
Definition: TimeString.cpp:432
gnsstk::BasicFramework
Definition: BasicFramework.hpp:387
TimCvt
Definition: timeconvert.cpp:308
TimCvt::MJDOption
CommandOptionWithCommonTimeArg MJDOption
Definition: timeconvert.cpp:326
TimCvt::stringToParse
string stringToParse
Definition: timeconvert.cpp:346
gnsstk::StringUtils::asTimeSystem
TimeSystem asTimeSystem(const std::string &s)
Convert a string representation of TimeSystem to an enum.
Definition: TimeSystem.cpp:324
TimCvt::GPSZ32Option
CommandOptionWithCommonTimeArg GPSZ32Option
Definition: timeconvert.cpp:324
TimCvt::UnixTimeOption
CommandOptionWithCommonTimeArg UnixTimeOption
Definition: timeconvert.cpp:327
gnsstk::GPSWeekZcount::printf
virtual std::string printf(const std::string &fmt) const
Definition: GPSWeekZcount.cpp:114
gnsstk::CivilTime::printf
virtual std::string printf(const std::string &fmt) const
Definition: CivilTime.cpp:111
TimCvt::ndfp
gnsstk::NavDataFactoryPtr ndfp
nav data file reader
Definition: timeconvert.cpp:349
gnsstk::SystemTime
Definition: SystemTime.hpp:54
gnsstk::CommonTime
Definition: CommonTime.hpp:84
gnsstk::CommandOption
Definition: CommandOption.hpp:115
TimCvt::subOption
CommandOptionWithNumberArg subOption
Definition: timeconvert.cpp:343
TimCvt::addOption
CommandOptionWithNumberArg addOption
Definition: timeconvert.cpp:342
TimCvt::JDOption
CommandOptionWithCommonTimeArg JDOption
Definition: timeconvert.cpp:325
gnsstk::TimeSystem
TimeSystem
Definition of various time systems.
Definition: TimeSystem.hpp:51
gnsstk::CommandOptionWithNumberArg
Definition: CommandOption.hpp:394
TimCvt::GPSZ29Option
CommandOptionWithCommonTimeArg GPSZ29Option
Definition: timeconvert.cpp:323
gnsstk::StringUtils::asDouble
double asDouble(const std::string &s)
Definition: StringUtils.hpp:705
gnsstk::BasicFramework::run
bool run() noexcept
Definition: BasicFramework.cpp:126
gnsstk::DumpDetail::Full
@ Full
Include all detailed information.
TimCvt::tgtTSOption
CommandOptionWithAnyArg tgtTSOption
Target time system.
Definition: timeconvert.cpp:331
gnsstk::UnixTime
Definition: UnixTime.hpp:67
gnsstk::GPSWeekZcount
Definition: GPSWeekZcount.hpp:55
TimCvt::CivilTimeOption
CommandOptionWithCommonTimeArg CivilTimeOption
Definition: timeconvert.cpp:318
gnsstk::CivilTime
Definition: CivilTime.hpp:55
gnsstk::CommonTime::getTimeSystem
TimeSystem getTimeSystem() const
Obtain time system info (enum).
Definition: CommonTime.cpp:288
gnsstk::printTime
std::string printTime(const CommonTime &t, const std::string &fmt)
Definition: TimeString.cpp:64
gnsstk::DumpDetail::Brief
@ Brief
Limit output to <= 5 lines of minimal information.
std
Definition: Angle.hpp:142
gnsstk::NavTimeSystemConverter::navLib
std::shared_ptr< NavLibrary > navLib
Pointer to the nav library from which we will get time offset data.
Definition: NavTimeSystemConverter.hpp:102
gnsstk::BasicFramework::exitCode
int exitCode
Definition: BasicFramework.hpp:450
gnsstk::CommandOptionWithCommonTimeArg::getTime
const std::vector< CommonTime > & getTime() const
Return the times scanned in from the command line.
Definition: CommandOptionWithCommonTimeArg.hpp:96
gnsstk::CommandOption::getValue
const std::vector< std::string > & getValue() const
Definition: CommandOption.hpp:194
gnsstk::GPSWeekSecond::printf
virtual std::string printf(const std::string &fmt) const
Definition: GPSWeekSecond.hpp:145
gnsstk::MJD::printf
virtual std::string printf(const std::string &fmt) const
Definition: MJD.cpp:95
gnsstk::CommandOptionHelp
Definition: CommandOption.hpp:735
gnsstk::BasicFramework::initialize
virtual bool initialize(int argc, char *argv[], bool pretty=true) noexcept
Definition: BasicFramework.cpp:71
gnsstk::MJD
Definition: MJD.hpp:54
gnsstk::DumpDetail::OneLine
@ OneLine
Limit output to minimal information on a single line.
gnsstk::StringUtils::leftJustify
std::string & leftJustify(std::string &s, const std::string::size_type length, const char pad=' ')
Definition: StringUtils.hpp:1582
TimCvt::offsetTgtTSOption
CommandOptionDependent offsetTgtTSOption
Definition: timeconvert.cpp:341
gnsstk::CommonTime::changeTimeSystem
bool changeTimeSystem(TimeSystem timeSystem, TimeSystemConverter *conv)
Definition: CommonTime.cpp:190
gnsstk::CommandOptionWithCommonTimeArg
Definition: CommandOptionWithCommonTimeArg.hpp:62
TimCvt::navOption
CommandOptionWithAnyArg navOption
Navigation data input files.
Definition: timeconvert.cpp:332
TimCvt::ANSITimeOption
CommandOptionWithCommonTimeArg ANSITimeOption
Definition: timeconvert.cpp:317
TimCvt::YDSTimeOption
CommandOptionWithCommonTimeArg YDSTimeOption
Definition: timeconvert.cpp:328
gnsstk::CommandOption::setMaxCount
CommandOption & setMaxCount(const unsigned long l)
Definition: CommandOption.hpp:164
gnsstk::CommandOption::setDescription
void setDescription(const std::string &desc)
Definition: CommandOption.hpp:223
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