HOPS
HOPS class reference
MHO_Clock.hh
Go to the documentation of this file.
1 #ifndef MHO_Clock_HH__
2 #define MHO_Clock_HH__
3 
4 #include <chrono>
5 #include <iomanip>
6 #include <math.h>
7 #include <sstream>
8 #include <string>
9 
10 //these can someday be replaced with STL versions in C++20
11 #include "date/date.h"
12 
13 //utc/tai/gps clocks and leap-second lookup come from the table in hops_leap_seconds.hh
14 //(we don't link libdate-tz), or use the host tzdata via the H. Hinnant date library
15 //Leap seconds have been discontinued as of May 20, 2027:
16 //see https://www.bipm.org/en/cgpm-2026/resolution-5
17 #include "hops_leap_seconds.hh"
18 
19 #include "MHO_Message.hh"
20 #include "MHO_Tokenizer.hh"
21 #include "legacy_hops_date.hh"
22 
23 #define J2000_TAI_EPOCH "2000-01-01 11:59:27.816"
24 #define ISO8601_UTC_FORMAT "%FT%TZ"
25 #define HOPS_TIMESTAMP_PREFIX "HOPS-J2000"
26 #define HOPS_TIME_DELIM "|"
27 #define HOPS_TIME_UNIT "ns"
28 #define NANOSEC_TO_SEC 1e-9
29 #define SEC_TO_NANOSEC 1000000000
30 #define JD_TO_SEC 86400.0
31 #define MINUTE_TO_SEC 60.0
32 #define HOUR_TO_SEC 3600.0
33 
34 namespace hops
35 {
36 
49 {
50  public:
51  using duration = std::chrono::nanoseconds;
52  using rep = duration::rep;
53  using period = duration::period;
54  using time_point = std::chrono::time_point< hops_clock, std::chrono::nanoseconds >;
55  static const bool is_steady = false;
56 
63  static time_point now();
64 
71  template< typename Duration >
72  static std::chrono::time_point< hops::utc_clock, typename std::common_type< Duration, std::chrono::nanoseconds >::type >
73  to_utc(const std::chrono::time_point< hops_clock, Duration >&) NOEXCEPT;
74 
81  template< typename Duration >
82  static std::chrono::time_point< hops_clock, typename std::common_type< Duration, std::chrono::nanoseconds >::type >
83  from_utc(const std::chrono::time_point< hops::utc_clock, Duration >&) NOEXCEPT;
84 
91  template< typename Duration >
92  static std::chrono::time_point< hops::tai_clock, typename std::common_type< Duration, std::chrono::nanoseconds >::type >
93  to_tai(const std::chrono::time_point< hops_clock, Duration >&) NOEXCEPT;
94 
101  template< typename Duration >
102  static std::chrono::time_point< hops_clock, typename std::common_type< Duration, std::chrono::nanoseconds >::type >
103  from_tai(const std::chrono::time_point< hops::tai_clock, Duration >&) NOEXCEPT;
104 
111  template< typename Duration >
112  static std::chrono::time_point< hops::gps_clock, typename std::common_type< Duration, std::chrono::nanoseconds >::type >
113  to_gps(const std::chrono::time_point< hops_clock, Duration >&) NOEXCEPT;
114 
121  template< typename Duration >
122  static std::chrono::time_point< hops_clock, typename std::common_type< Duration, std::chrono::nanoseconds >::type >
123  from_gps(const std::chrono::time_point< hops::gps_clock, Duration >&) NOEXCEPT;
124 
131  template< typename Duration >
132  static std::chrono::time_point< std::chrono::system_clock,
133  typename std::common_type< Duration, std::chrono::nanoseconds >::type >
134  to_sys(const std::chrono::time_point< hops_clock, Duration >&) NOEXCEPT;
135 
142  template< typename Duration >
143  static std::chrono::time_point< hops_clock, typename std::common_type< Duration, std::chrono::nanoseconds >::type >
144  from_sys(const std::chrono::time_point< std::chrono::system_clock, Duration >&) NOEXCEPT;
145 
152  template< typename Duration >
153  static std::chrono::time_point< date::local_t, typename std::common_type< Duration, std::chrono::nanoseconds >::type >
154  to_local(const std::chrono::time_point< hops_clock, Duration >&) NOEXCEPT;
155 
162  template< typename Duration >
163  static std::chrono::time_point< hops_clock, typename std::common_type< Duration, std::chrono::nanoseconds >::type >
164  from_local(const std::chrono::time_point< date::local_t, Duration >&) NOEXCEPT;
165 
173  static time_point from_iso8601_format(const std::string& timestamp);
174 
182  static std::string to_iso8601_format(const time_point& tp);
183 
191  static time_point from_hops_format(const std::string& timestamp);
192 
200  static std::string to_hops_format(const time_point& tp);
201 
210 
219 
228  static time_point from_vdif_format(int& vdif_epoch, int& vdif_seconds);
229 
239  static void to_vdif_format(const time_point& tp, int& vdif_epoch, int& vdif_second);
240 
250  static time_point from_mjd(const time_point& mjd_epoch, double epoch_offset, double mjd);
251 
261  static double to_mjd(const time_point& mjd_epoch, double epoch_offset, const time_point& tp);
262 
270  static time_point from_vex_format(const std::string& timestamp);
271 
280  static std::string to_vex_format(const time_point& tp, bool truncate_to_nearest_second = false);
281 
291  static time_point from_year_fpday(int year, double floating_point_days);
292 
302  static void to_year_fpday(const time_point& tp, int& year, double& floating_point_days);
303 
308  {
309  using namespace std::chrono;
310  //J2000 TAI epoch = 2000-01-01 11:59:27.816 (TAI wall-clock reading,
311  //see J2000_TAI_EPOCH). Build the tai_time numerically from calendar
312  //arithmetic (date::sys_days does not invovle leap seconds, no tz.h)
313  date::sys_days tai_epoch_day{date::year{1958} / date::month{1} / date::day{1}};
314  date::sys_days j2000_day{date::year{2000} / date::month{1} / date::day{1}};
315  auto days_since_tai_epoch = j2000_day - tai_epoch_day; //date::days
316  auto tod = hours{11} + minutes{59} + seconds{27} + milliseconds{816};
317  hops::tai_time< nanoseconds > j2000_tai_epoch{duration_cast< nanoseconds >(days_since_tai_epoch) +
318  duration_cast< nanoseconds >(tod)};
319  return time_point_cast< nanoseconds >(hops::tai_clock::to_utc(j2000_tai_epoch));
320  }
321 
326 
330  static std::chrono::seconds get_leap_seconds_between(const time_point& t_start, const time_point& t_end)
331  {
332  auto t_start_utc = to_utc(t_start);
333  auto t_end_utc = to_utc(t_end);
334  auto lp_info0 = hops::get_leap_second_info(t_start_utc);
335  auto lp_info1 = hops::get_leap_second_info(t_end_utc);
336  int delta = lp_info1.elapsed.count() - lp_info0.elapsed.count();
337  return std::chrono::seconds(delta);
338  }
339 
340  static bool is_vex_timestamp(const std::string& s);
341 
342  private:
343  static date::days day_of_year(date::sys_days sd)
344  {
345  using namespace date;
346  auto y = date::year_month_day{sd}.year();
347  return sd - date::sys_days{y / jan / 0};
348  }
349 
350  static date::sys_days get_year_month_day(date::year y, date::days ord_day)
351  {
352  using namespace date;
353  return date::sys_days{y / jan / 0} + ord_day;
354  }
355 
356  struct vex_date
357  {
358  int year;
359  int day_of_year;
360  int hours;
361  int minutes;
362  double seconds;
363  };
364 
365  static vex_date extract_vex_date(const std::string& timestamp);
366 
367  static std::string vex_date_to_iso8601_string(vex_date vdate);
368 
369  static vex_date vex_date_from_legacy(const legacy_hops_date& legacy_date);
370 
371  static std::string remove_trailing_zeros(std::string value)
372  {
373  std::size_t nzeros_on_end = 0;
374  for(auto rit = value.rbegin(); rit != value.rend(); rit++)
375  {
376  if(*rit != '0')
377  {
378  break;
379  }
380  nzeros_on_end++;
381  }
382  std::size_t useful_length = value.size() - nzeros_on_end;
383  std::string ret_val;
384  for(std::size_t i = 0; i < useful_length; i++)
385  {
386  ret_val.push_back(value[i]);
387  }
388  return ret_val;
389  }
390 };
391 
395 template< class Duration > using hops_time = std::chrono::time_point< hops_clock, Duration >;
396 
403 template< class Duration >
406 {
407 
408  using CD = typename std::common_type< Duration, std::chrono::nanoseconds >::type;
410  return hops::utc_time< CD >(t.time_since_epoch() + hops_epoch_start.time_since_epoch());
411 }
412 
419 template< class Duration >
422 {
423  using CD = typename std::common_type< Duration, std::chrono::nanoseconds >::type;
425  return hops_time< CD >(t.time_since_epoch() - hops_epoch_start.time_since_epoch());
426 }
427 
434 template< class Duration >
437 {
439 }
440 
447 template< class Duration >
450 {
452 }
453 
460 template< class Duration >
463 {
465 }
466 
473 template< class Duration >
476 {
478 }
479 
486 template< class Duration >
487 inline date::sys_time< typename std::common_type< Duration, std::chrono::nanoseconds >::type >
489 {
491 }
492 
499 template< class Duration >
501 hops_clock::from_sys(const date::sys_time< Duration >& t) NOEXCEPT
502 {
504 }
505 
512 {
513  return from_utc(hops::utc_clock::now());
514 }
515 
522 template< class Duration >
523 inline date::local_time< typename std::common_type< Duration, std::chrono::nanoseconds >::type >
525 {
526  using CD = typename std::common_type< Duration, std::chrono::nanoseconds >::type;
527  hops::utc_time< CD > hops_epoch_start = std::chrono::time_point_cast< CD >(get_hops_epoch_utc());
528  hops::utc_time< CD > ut_time{t.time_since_epoch() +
529  std::chrono::time_point_cast< Duration >(hops_epoch_start).time_since_epoch()};
530  return hops::utc_clock::to_local(ut_time);
531 }
532 
539 template< class Duration >
541 hops_clock::from_local(const date::local_time< Duration >& t) NOEXCEPT
542 {
543  using CD = typename std::common_type< Duration, std::chrono::nanoseconds >::type;
545  hops::utc_time< CD > hops_epoch_start = std::chrono::time_point_cast< CD >(get_hops_epoch_utc());
546  return hops_time< CD >{t2.time_since_epoch() -
547  std::chrono::time_point_cast< Duration >(hops_epoch_start).time_since_epoch()};
548 }
549 
561 template< class CharT, class Traits, class Duration >
562 std::basic_ostream< CharT, Traits >& to_stream(std::basic_ostream< CharT, Traits >& os, const CharT* fmt,
563  const hops_time< Duration >& t)
564 {
565  const std::string abbrev("HOPS");
566  CONSTDATA std::chrono::seconds offset{0};
567  return date::to_stream(os, fmt, hops_clock::to_local(t), &abbrev, &offset);
568 }
569 
583 template< class Duration, class CharT, class Traits, class Alloc = std::allocator< CharT > >
584 std::basic_istream< CharT, Traits >&
585 from_stream(std::basic_istream< CharT, Traits >& is, const CharT* fmt, hops_time< Duration >& tp,
586  std::basic_string< CharT, Traits, Alloc >* abbrev = nullptr, std::chrono::minutes* offset = nullptr)
587 {
588  date::local_time< Duration > lp;
589  from_stream(is, fmt, lp, abbrev, offset);
590  if(!is.fail())
591  tp = hops_clock::from_local(lp);
592  return is;
593 }
594 
595 template< class CharT, class Traits, class Duration >
596 std::basic_ostream< CharT, Traits >& operator<<(std::basic_ostream< CharT, Traits >& os, const hops_time< Duration >& t)
597 {
598  const CharT fmt[] = {'%', 'F', 'T', '%', 'T', 'Z', CharT{}};
599  return to_stream(os, fmt, t);
600 }
601 
608 inline hops_clock::time_point hops_clock::from_iso8601_format(const std::string& timestamp)
609 {
610  using namespace date;
611  using namespace std::chrono;
612  std::string frmt = ISO8601_UTC_FORMAT;
613  std::istringstream ss(timestamp);
614  std::istream tmp_stream(ss.rdbuf());
616  from_stream(tmp_stream, frmt.c_str(), hops_tp);
617  return hops_tp;
618 }
619 
626 inline std::string hops_clock::to_iso8601_format(const time_point& tp)
627 {
628  std::stringstream ss;
629  ss << tp;
630  return ss.str();
631 }
632 
639 inline hops_clock::time_point hops_clock::from_hops_format(const std::string& timestamp)
640 {
641  using namespace date;
642  using namespace std::chrono;
643 
644  MHO_Tokenizer tokenizer;
645  tokenizer.SetDelimiter(std::string(HOPS_TIME_DELIM));
646  std::vector< std::string > tokens;
647  tokenizer.SetString(&timestamp);
648  tokenizer.GetTokens(&tokens);
649  if(tokens.size() == 3)
650  {
651  std::string hops_prefix = tokens[0];
652  std::string unit = tokens[1];
653  std::string nanosecond_count = tokens[2];
654  if(hops_prefix == std::string(HOPS_TIMESTAMP_PREFIX) && unit == std::string(HOPS_TIME_UNIT))
655  {
656  std::stringstream ss;
657  ss << nanosecond_count;
658  int64_t ns;
659  ss >> ns;
660  return time_point(std::chrono::nanoseconds(ns));
661  }
662  }
663  msg_error("utility", "hops timestamp string not understood or supported, returning epoch start. " << eom);
664  return time_point(std::chrono::nanoseconds(0));
665 }
666 
673 inline std::string hops_clock::to_hops_format(const time_point& tp)
674 {
675  std::stringstream ss;
676  ss << HOPS_TIMESTAMP_PREFIX;
677  ss << HOPS_TIME_DELIM;
678  ss << HOPS_TIME_UNIT;
679  ss << HOPS_TIME_DELIM;
680  ss << tp.time_since_epoch().count();
681  return ss.str();
682 }
683 
685 {
686  vex_date vdate = vex_date_from_legacy(ldate);
687  std::string vex_as_iso8601 = vex_date_to_iso8601_string(vdate);
688  return hops_clock::from_iso8601_format(vex_as_iso8601);
689 }
690 
692 {
693  using namespace date;
694  using namespace std::chrono;
695 
696  //convert the time point to sys time, and extract the date
697  auto sys_tp = hops_clock::to_sys(tp);
698  auto dp = date::sys_days(floor< date::days >(sys_tp));
699 
700  //get all of the date information
701  date::year_month_day ymd{dp};
702  auto year = ymd.year();
703 
704  //get the ordinal day of the year
705  auto ordinal_day = day_of_year(dp);
706 
707  //get the time
708  date::hh_mm_ss< std::chrono::nanoseconds > time{floor< std::chrono::nanoseconds >(sys_tp - dp)};
709  auto hours = time.hours();
710  auto mins = time.minutes();
711  auto secs = time.seconds();
712  auto nanos = time.subseconds();
713 
714  legacy_hops_date ldate;
715  ldate.year = (int)year;
716  ldate.day = ordinal_day.count();
717  ldate.hour = hours.count();
718  ldate.minute = mins.count();
719  //note there may be loss of precision when converting to/from the actual legacy struct (single precision)
720  ldate.second = (double)secs.count() + ((double)(nanos.count())) * NANOSEC_TO_SEC;
721 
722  return ldate;
723 }
724 
725 //needed for ad_hoc flag files (time-stamps are given in floating-point days)
726 inline hops_clock::time_point hops_clock::from_year_fpday(int year, double floating_point_days)
727 {
728  int integer_days = (int)floating_point_days;
729  double fractional_day = floating_point_days - integer_days;
730  int integer_hours = (int)24 * fractional_day;
731  double fractional_hour = 24 * fractional_day - integer_hours;
732  int integer_minutes = (int)60 * fractional_hour;
733  double fractional_seconds = (60 * fractional_hour - integer_minutes) * 60;
734 
735  //we co-opt the legacy date format to handle this format
736  legacy_hops_date ldate;
737  ldate.year = (short)year;
738  ldate.day = (short)integer_days + 1; //note: ordinal day count starts at 1
739  ldate.hour = (short)integer_hours;
740  ldate.minute = (short)integer_minutes;
741  ldate.second = fractional_seconds;
742 
743  return from_legacy_hops_date(ldate);
744 }
745 
746 inline void hops_clock::to_year_fpday(const hops_clock::time_point& tp, int& year, double& floating_point_days)
747 {
748  using namespace date;
749  using namespace std::chrono;
750 
751  //convert the time point to sys time, and extract the date
752  auto sys_tp = hops_clock::to_sys(tp);
753  auto dp = date::sys_days(floor< date::days >(sys_tp));
754 
755  //get all of the date information
756  date::year_month_day ymd{dp};
757  auto year_value = ymd.year();
758  //get the ordinal day of the year
759  auto ordinal_day = day_of_year(dp); //note: count starts at 1
760  int integer_days = ordinal_day.count() - 1;
761  //get the time and convert to fractional day
762  date::hh_mm_ss< std::chrono::nanoseconds > time{floor< std::chrono::nanoseconds >(sys_tp - dp)};
763  int ihours = time.hours().count();
764  int imins = time.minutes().count();
765  int isecs = time.seconds().count();
766  int inanos = time.subseconds().count();
767 
768  double frac_day = (inanos * NANOSEC_TO_SEC + isecs + MINUTE_TO_SEC * imins + HOUR_TO_SEC * ihours) / (JD_TO_SEC);
769  floating_point_days = integer_days + frac_day;
770  year = static_cast< int >(year_value);
771 }
772 
773 inline hops_clock::time_point hops_clock::from_mjd(const time_point& mjd_epoch, double epoch_offset, double mjd)
774 {
775  double delta = (mjd - epoch_offset);
776  delta *= JD_TO_SEC;
777  std::chrono::duration< double > duration_seconds(delta);
778 
779  auto mjd_epoch_utc = to_utc(mjd_epoch);
780  auto utc_time_point = mjd_epoch_utc + std::chrono::duration_cast< std::chrono::nanoseconds >(duration_seconds);
781  auto hops_time_point = from_utc(utc_time_point);
782  return hops_time_point;
783 }
784 
785 inline double hops_clock::to_mjd(const time_point& mjd_epoch, double epoch_offset, const time_point& tp)
786 {
787  auto mjd_epoch_utc = to_utc(mjd_epoch);
788  auto tp_utc = to_utc(tp);
789 
790  double delta = (tp_utc - mjd_epoch_utc).count();
791  delta *= NANOSEC_TO_SEC; //convert to seconds
792  delta /= JD_TO_SEC; //convert to days
793  delta += epoch_offset; //subtract epoch offset
794  return delta;
795 }
796 
797 inline hops_clock::time_point hops_clock::from_vex_format(const std::string& timestamp)
798 {
799  vex_date vdate = hops_clock::extract_vex_date(timestamp);
800  //convert the vex date info to an ISO-8601-style year-month-day type format
801  std::string vex_as_iso8601 = vex_date_to_iso8601_string(vdate);
802  return hops_clock::from_iso8601_format(vex_as_iso8601);
803 }
804 
805 inline std::string hops_clock::to_vex_format(const time_point& tp, bool truncate_to_nearest_second)
806 {
807  using namespace date;
808  using namespace std::chrono;
809 
810  //convert the time point to sys time, and extract the date
811  auto sys_tp = hops_clock::to_sys(tp);
812  auto dp = date::sys_days(floor< date::days >(sys_tp));
813 
814  //get all of the date information
815  date::year_month_day ymd{dp};
816  auto year = ymd.year();
817  // auto month = ymd.month();
818  // auto day = ymd.day();
819 
820  //get the ordinal day of the year
821  auto ordinal_day = day_of_year(dp);
822 
823  //get the time
824  date::hh_mm_ss< std::chrono::nanoseconds > time{floor< std::chrono::nanoseconds >(sys_tp - dp)};
825  auto hours = time.hours();
826  auto mins = time.minutes();
827  auto secs = time.seconds();
828  auto nanos = time.subseconds();
829 
830  //we need to make sure that year, day-of-year, hour, minute, and integer sec
831  //are all preprended with the proper number of zeros
832 
833  std::stringstream ss;
834  ss << year;
835  ss << "y";
836  ss << std::setfill('0') << std::setw(3) << ordinal_day.count();
837  ss << "d";
838  ss << std::setfill('0') << std::setw(2) << hours.count();
839  ss << "h";
840  ss << std::setfill('0') << std::setw(2) << mins.count();
841  ss << "m";
842  ss << std::setfill('0') << std::setw(2) << secs.count();
843 
844  if(!truncate_to_nearest_second)
845  {
846  std::stringstream nss;
847  nss << std::setfill('0') << std::setw(9) << nanos.count();
848  std::string snano_sec;
849  nss >> snano_sec;
850  std::string trimmed_nanosec = remove_trailing_zeros(snano_sec);
851  if(trimmed_nanosec.size() != 0)
852  {
853  ss << ".";
854  ss << trimmed_nanosec;
855  }
856  }
857  ss << "s";
858 
859  return ss.str();
860 }
861 
862 inline void hops_clock::to_vdif_format(const time_point& tp, int& vdif_epoch, int& vdif_seconds)
863 {
864  using namespace date;
865  using namespace std::chrono;
866 
867  //convert the time point to sys time, and extract the date
868  auto sys_tp = hops_clock::to_sys(tp);
869  auto dp = date::sys_days(floor< date::days >(sys_tp));
870 
871  //get all of the date information so we can figure out the epoch
872  date::year_month_day ymd{dp};
873  auto year = ymd.year();
874  auto month = ymd.month();
875  auto day = ymd.day();
876 
877  //(we have two 6 month epochs per year, and count from start of century)
878  int iyear = static_cast< int >(year);
879  unsigned int imonth = static_cast< unsigned int >(month);
880  int epoch = (iyear % 100) * 2;
881 
882  //now figure out if we are using the Jan 1st epoch, or the July 1st epoch
883  if(imonth < 7)
884  {
885  imonth = 1;
886  }
887  else
888  {
889  epoch += 1;
890  imonth = 7;
891  }
892  //set the day to the first of the month
893  day = date::day(1);
894  int hours = 0;
895  int minutes = 0;
896  int integer_sec = 0;
897 
898  //now figure out the epoch date
899  //may want to eliminate string conversion in favor of something faster
900  std::stringstream ss;
901  ss << iyear;
902  ss << "-";
903  ss << std::setfill('0') << std::setw(2) << imonth;
904  ss << "-";
905  ss << std::setfill('0') << std::setw(2) << static_cast< unsigned int >(day);
906  ss << "T";
907  ss << std::setfill('0') << std::setw(2) << hours;
908  ss << ":";
909  ss << std::setfill('0') << std::setw(2) << minutes;
910  ss << ":";
911  ss << std::setfill('0') << std::setw(2) << integer_sec;
912  ss << "Z";
913  std::string epoch_iso8601 = ss.str();
914  auto epoch_tp = from_iso8601_format(epoch_iso8601);
915  int secs = std::chrono::duration_cast< std::chrono::seconds >(tp - epoch_tp).count();
916 
917  vdif_epoch = epoch;
918  vdif_seconds = secs;
919 }
920 
921 inline hops_clock::time_point hops_clock::from_vdif_format(int& vdif_epoch, int& vdif_seconds)
922 {
923  using namespace date;
924  using namespace std::chrono;
925 
926  int start_year = 2000;
927  int n_years = std::floor(vdif_epoch / 2);
928  int iyear = start_year + n_years;
929 
930  unsigned int imonth = 1;
931  if(vdif_epoch % 2 == 1)
932  {
933  imonth = 7;
934  } //second half of the year
935  unsigned int iday = 1;
936  int hours = 0;
937  int minutes = 0;
938  int integer_sec = 0;
939 
940  //now figure out the epoch date
941  //may want to eliminate string conversion in favor of something faster
942  std::stringstream ss;
943  ss << iyear;
944  ss << "-";
945  ss << std::setfill('0') << std::setw(2) << imonth;
946  ss << "-";
947  ss << std::setfill('0') << std::setw(2) << iday;
948  ss << "T";
949  ss << std::setfill('0') << std::setw(2) << hours;
950  ss << ":";
951  ss << std::setfill('0') << std::setw(2) << minutes;
952  ss << ":";
953  ss << std::setfill('0') << std::setw(2) << integer_sec;
954  ss << "Z";
955  std::string epoch_iso8601 = ss.str();
956  std::cout << "epoch = " << epoch_iso8601 << std::endl;
957  auto epoch_tp = from_iso8601_format(epoch_iso8601);
958  auto tp = epoch_tp + std::chrono::seconds(vdif_seconds);
959  return tp;
960 }
961 
962 inline hops_clock::vex_date hops_clock::extract_vex_date(const std::string& timestamp)
963 {
964  vex_date vdate;
965  if(timestamp.size() == 0)
966  {
967  msg_error("utilities", "cannot extract vex date from empty string." << eom);
968  return vdate;
969  }
970 
971  MHO_Tokenizer tokenizer;
972  std::vector< std::string > tokens;
973  std::stringstream ss;
974  std::string rest;
975  std::string syear, sord_day, shour, smin, ssec;
976 
977  tokenizer.SetDelimiter(std::string("y"));
978  tokenizer.SetString(&timestamp);
979  tokenizer.GetTokens(&tokens);
980 
981  syear = tokens[0];
982  ss << syear;
983  ss >> vdate.year;
984  rest = tokens[1];
985 
986  tokenizer.SetDelimiter(std::string("d"));
987  tokenizer.SetString(&rest);
988  tokenizer.GetTokens(&tokens);
989 
990  sord_day = tokens[0];
991  ss.str(std::string());
992  ss.clear();
993  ss << sord_day;
994  ss >> vdate.day_of_year;
995  rest = tokens[1];
996 
997  tokenizer.SetDelimiter(std::string("h"));
998  tokenizer.SetString(&rest);
999  tokenizer.GetTokens(&tokens);
1000 
1001  shour = tokens[0];
1002  ss.str(std::string());
1003  ss.clear();
1004  ss << shour;
1005  ss >> vdate.hours;
1006  rest = tokens[1];
1007 
1008  tokenizer.SetDelimiter(std::string("m"));
1009  tokenizer.SetString(&rest);
1010  tokenizer.GetTokens(&tokens);
1011 
1012  smin = tokens[0];
1013  ss.str(std::string());
1014  ss.clear();
1015  ss << smin;
1016  ss >> vdate.minutes;
1017  rest = tokens[1];
1018 
1019  tokenizer.SetDelimiter(std::string("s"));
1020  tokenizer.SetString(&rest);
1021  tokenizer.GetTokens(&tokens);
1022 
1023  ssec = tokens[0];
1024  ss.str(std::string());
1025  ss.clear();
1026  ss << std::setprecision(15) << ssec;
1027  ss >> vdate.seconds;
1028 
1029  return vdate;
1030 }
1031 
1032 inline std::string hops_clock::vex_date_to_iso8601_string(hops_clock::vex_date vdate)
1033 {
1034  using namespace date;
1035  using namespace std::chrono;
1036 
1037  std::stringstream ss;
1038  ss << vdate.year;
1039  ss << "-";
1040 
1041  date::year y(vdate.year);
1042  date::days ord_day(vdate.day_of_year);
1043  date::sys_days ymd = get_year_month_day(y, ord_day);
1044 
1045  //convert day-of-year to month-day
1046  auto month = date::year_month_day{ymd}.month();
1047  auto mday = date::year_month_day{ymd}.day();
1048  ss << std::setfill('0') << std::setw(2) << (unsigned)month;
1049  ss << "-";
1050  ss << std::setfill('0') << std::setw(2) << (unsigned)mday;
1051 
1052  ss << "T";
1053  ss << std::setfill('0') << std::setw(2) << vdate.hours;
1054  ss << ":";
1055  ss << std::setfill('0') << std::setw(2) << vdate.minutes;
1056  ss << ":";
1057 
1058  //nss << std::setprecision(9) << vdate.seconds;
1059 
1060  double intpart;
1061  double frac = modf(vdate.seconds, &intpart);
1062  int integer_sec = intpart;
1063 
1064  ss << std::setfill('0') << std::setw(2) << integer_sec;
1065 
1066  //now convert the fraction part into integer nano seconds
1067  //zero-pad to 9 digits so leading zeros (e.g. 256 ns -> "000000256") are
1068  //preserved; without this the round-trip through to_vex_format/ISO8601
1069  //rescales sub-second precision by powers of 10
1070  int integer_nanosec = frac * SEC_TO_NANOSEC;
1071  std::stringstream nss;
1072  nss << std::setfill('0') << std::setw(9) << integer_nanosec;
1073  std::string nanoseconds_value = nss.str();
1074  std::string trimmed_int_nanosec = remove_trailing_zeros(nanoseconds_value);
1075  if(trimmed_int_nanosec.size() != 0)
1076  {
1077  ss << ".";
1078  ss << trimmed_int_nanosec;
1079  }
1080  ss << "Z";
1081  return ss.str();
1082 }
1083 
1084 inline hops_clock::vex_date hops_clock::vex_date_from_legacy(const legacy_hops_date& legacy_date)
1085 {
1086  hops_clock::vex_date vdate;
1087  vdate.year = legacy_date.year;
1088  vdate.day_of_year = legacy_date.day;
1089  vdate.hours = legacy_date.hour;
1090  vdate.minutes = legacy_date.minute;
1091  vdate.seconds = legacy_date.second;
1092  return vdate;
1093 }
1094 
1095 // checks if a string is a valid VEX timestamp like: 2026y180d12h15m08s
1096 // note that the seconds field may include an optional fractional part like: 08.0145s
1097 inline bool hops_clock::is_vex_timestamp(const std::string& s)
1098 {
1099  std::size_t pos = 0; //location in the string
1100 
1101  //spec out the 4 fixed width fields (seconds portion may vary)
1102  const int digit_counts[4] = {4, 3, 2, 2};
1103  const char separators[4] = {'y', 'd', 'h', 'm'};
1104 
1105  //loop over year, day, hours, minutes
1106  for(int field = 0; field < 4; field++)
1107  {
1108  for(int i = 0; i < digit_counts[field]; i++) //check the digits
1109  {
1110  if(pos >= s.size() || !std::isdigit(static_cast< unsigned char >(s[pos])))
1111  {
1112  return false;
1113  }
1114  pos++;
1115  }
1116  //reached the end, so check the separator value (y,d,h,m)
1117  if(pos >= s.size() || s[pos] != separators[field])
1118  {
1119  return false;
1120  }
1121  pos++;
1122  }
1123 
1124  //now check the seconds, first 2 digits
1125  for(int i = 0; i < 2; ++i)
1126  {
1127  if(pos >= s.size() || !std::isdigit(static_cast< unsigned char >(s[pos])))
1128  {
1129  return false;
1130  }
1131  pos++;
1132  }
1133 
1134  //now check the (optional) fractional part: '.' followed by at least one digit
1135  if(pos < s.size() && s[pos] == '.')
1136  {
1137  pos++; // consume '.', then next char must be a digit
1138  if(pos >= s.size() || !std::isdigit(static_cast< unsigned char >(s[pos])))
1139  {
1140  return false;
1141  }
1142  //chew through the rest until we reach anything not a digit
1143  while(pos < s.size() && std::isdigit(static_cast< unsigned char >(s[pos])))
1144  {
1145  ++pos;
1146  }
1147  }
1148 
1149  //better only have the trailing 's' left
1150  if(pos >= s.size() || s[pos] != 's')
1151  {
1152  return false;
1153  }
1154  pos++;
1155 
1156  //must have consumed the entire string with nothing leftover
1157  return pos == s.size();
1158 }
1159 
1160 } // namespace hops
1161 
1162 #endif
#define ISO8601_UTC_FORMAT
Definition: MHO_Clock.hh:24
#define MINUTE_TO_SEC
Definition: MHO_Clock.hh:31
#define SEC_TO_NANOSEC
Definition: MHO_Clock.hh:29
#define HOPS_TIMESTAMP_PREFIX
Definition: MHO_Clock.hh:25
#define HOPS_TIME_DELIM
Definition: MHO_Clock.hh:26
#define NANOSEC_TO_SEC
Definition: MHO_Clock.hh:28
#define HOPS_TIME_UNIT
Definition: MHO_Clock.hh:27
#define JD_TO_SEC
Definition: MHO_Clock.hh:30
#define HOUR_TO_SEC
Definition: MHO_Clock.hh:32
#define msg_error(xKEY, xCONTENT)
Definition: MHO_Message.hh:247
Class MHO_Tokenizer.
Definition: MHO_Tokenizer.hh:24
void SetString(const std::string *aString)
Definition: MHO_Tokenizer.cc:65
void GetTokens(std::vector< std::string > *tokens)
Definition: MHO_Tokenizer.cc:75
void SetDelimiter(const std::string &aDelim)
Definition: MHO_Tokenizer.cc:70
a clock for hops-time stamps, measures time in (UTC) nanoseconds since J2000 epoch....
Definition: MHO_Clock.hh:49
static std::chrono::time_point< hops::tai_clock, typename std::common_type< Duration, std::chrono::nanoseconds >::type > to_tai(const std::chrono::time_point< hops_clock, Duration > &) NOEXCEPT
Converts a time point from hops_clock to TAI (International Atomic Time).
static std::chrono::time_point< hops::utc_clock, typename std::common_type< Duration, std::chrono::nanoseconds >::type > to_utc(const std::chrono::time_point< hops_clock, Duration > &) NOEXCEPT
Converts a time point to UTC using hops_clock and Duration.
static time_point get_hops_epoch()
returns the hops_clock epoch as a hops_clock time_point
Definition: MHO_Clock.hh:325
static std::chrono::time_point< hops_clock, typename std::common_type< Duration, std::chrono::nanoseconds >::type > from_gps(const std::chrono::time_point< hops::gps_clock, Duration > &) NOEXCEPT
Converts GPS time to Hops clock time.
duration::period period
Definition: MHO_Clock.hh:53
static void to_vdif_format(const time_point &tp, int &vdif_epoch, int &vdif_second)
Converts a hops_clock time_point to a VDIF (epoch, second) timestamp.
Definition: MHO_Clock.hh:862
static time_point now()
Returns current time as a time_point using hops_clock's epoch.
Definition: MHO_Clock.hh:511
std::chrono::time_point< hops_clock, std::chrono::nanoseconds > time_point
Definition: MHO_Clock.hh:54
static std::chrono::time_point< hops_clock, typename std::common_type< Duration, std::chrono::nanoseconds >::type > from_utc(const std::chrono::time_point< hops::utc_clock, Duration > &) NOEXCEPT
Converts UTC time point to hops_clock time point.
static std::chrono::time_point< std::chrono::system_clock, typename std::common_type< Duration, std::chrono::nanoseconds >::type > to_sys(const std::chrono::time_point< hops_clock, Duration > &) NOEXCEPT
Converts a time point from hops_clock to system clock.
static bool is_vex_timestamp(const std::string &s)
Definition: MHO_Clock.hh:1097
static std::chrono::time_point< date::local_t, typename std::common_type< Duration, std::chrono::nanoseconds >::type > to_local(const std::chrono::time_point< hops_clock, Duration > &) NOEXCEPT
Converts a global time point to local time.
static std::string to_iso8601_format(const time_point &tp)
Converts a time_point to ISO8601 formatted string.
Definition: MHO_Clock.hh:626
static time_point from_year_fpday(int year, double floating_point_days)
Converts a year + floating point day since start of the year to a hops_clock time_point,...
Definition: MHO_Clock.hh:726
std::chrono::nanoseconds duration
Definition: MHO_Clock.hh:51
static std::chrono::time_point< hops_clock, typename std::common_type< Duration, std::chrono::nanoseconds >::type > from_sys(const std::chrono::time_point< std::chrono::system_clock, Duration > &) NOEXCEPT
Converts a system time point to UTC and returns the corresponding hops clock time.
static hops::utc_time< std::chrono::nanoseconds > get_hops_epoch_utc()
returns the hops_clock epoch as a utc_time time_point
Definition: MHO_Clock.hh:307
static time_point from_hops_format(const std::string &timestamp)
Converts a timestamp string in HOPS format to a time_point object.
Definition: MHO_Clock.hh:639
static time_point from_iso8601_format(const std::string &timestamp)
Converts an ISO8601 formatted timestamp string to a hops_clock time_point object.
Definition: MHO_Clock.hh:608
duration::rep rep
Definition: MHO_Clock.hh:52
static double to_mjd(const time_point &mjd_epoch, double epoch_offset, const time_point &tp)
Converts a hops_clock time_point to a Modified Julian date (floating point day) timestamp,...
Definition: MHO_Clock.hh:785
static time_point from_vdif_format(int &vdif_epoch, int &vdif_seconds)
Converts a VDIF (epoch, second) timestamp to a hops_clock time_point.
Definition: MHO_Clock.hh:921
static std::string to_vex_format(const time_point &tp, bool truncate_to_nearest_second=false)
Converts a hops_clock time_point to VEX-style formatted string (e.g. 2019y106d18h30m15s)
Definition: MHO_Clock.hh:805
static std::string to_hops_format(const time_point &tp)
Converts a time_point to HOPS format string.
Definition: MHO_Clock.hh:673
static time_point from_vex_format(const std::string &timestamp)
Converts a VEX-style formatted string (e.g. 2019y106d18h30m15s) to a hops_clock time_point.
Definition: MHO_Clock.hh:797
static std::chrono::seconds get_leap_seconds_between(const time_point &t_start, const time_point &t_end)
calculates the number of leap seconds inserted between two hops time points (UTC based clock)
Definition: MHO_Clock.hh:330
static time_point from_mjd(const time_point &mjd_epoch, double epoch_offset, double mjd)
Converts a Modified Julian date (floating point epoch and day) timestamp to a hops_clock time_point.
Definition: MHO_Clock.hh:773
static std::chrono::time_point< hops::gps_clock, typename std::common_type< Duration, std::chrono::nanoseconds >::type > to_gps(const std::chrono::time_point< hops_clock, Duration > &) NOEXCEPT
Converts a time point to GPS clock time.
static std::chrono::time_point< hops_clock, typename std::common_type< Duration, std::chrono::nanoseconds >::type > from_tai(const std::chrono::time_point< hops::tai_clock, Duration > &) NOEXCEPT
Converts a TAI time point to UTC and returns the corresponding Hops clock time.
static const bool is_steady
Definition: MHO_Clock.hh:55
static legacy_hops_date to_legacy_hops_date(const time_point &tp)
Converts a hops_clock time_point to a legacy hops data struct.
Definition: MHO_Clock.hh:691
static void to_year_fpday(const time_point &tp, int &year, double &floating_point_days)
Converts a hops_clock time_point to a floating point day since start of the year needed for ad_hoc fl...
Definition: MHO_Clock.hh:746
static time_point from_legacy_hops_date(legacy_hops_date &ldate)
Converts a legacy hops data struct to a hops_clock time_point.
Definition: MHO_Clock.hh:684
static std::chrono::time_point< hops_clock, typename std::common_type< Duration, std::chrono::nanoseconds >::type > from_local(const std::chrono::time_point< date::local_t, Duration > &) NOEXCEPT
Calculates time difference between input local time and Hops epoch in UTC.
char * field(char *, char *, int)
Definition: fourmer.c:236
Self-contained (link-free) reimplementation of the small slice of the date library's tz facilities th...
short day
Definition: mk4_typedefs.h:17
short year
Definition: mk4_typedefs.h:16
Definition: mk4_typedefs.h:15
t
Definition: picking_aedit.py:14
Definition: MHO_AdhocFlagging.hh:18
std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, const hops_time< Duration > &t)
Definition: MHO_Clock.hh:596
short hour
Definition: legacy_hops_date.hh:20
std::basic_istream< CharT, Traits > & from_stream(std::basic_istream< CharT, Traits > &is, const CharT *fmt, hops_time< Duration > &tp, std::basic_string< CharT, Traits, Alloc > *abbrev=nullptr, std::chrono::minutes *offset=nullptr)
Reads time and abbreviation from stream using given format, updates hops_time if successful.
Definition: MHO_Clock.hh:585
leap_second_info get_leap_second_info(const utc_time< Duration > &ut)
Leap-second lookup for a utc_time, matching date::get_leap_second_info.
Definition: hops_leap_seconds.hh:164
std::chrono::time_point< gps_clock, Duration > gps_time
Definition: hops_leap_seconds.hh:159
short minute
Definition: legacy_hops_date.hh:21
short day
Definition: legacy_hops_date.hh:19
std::chrono::time_point< utc_clock, Duration > utc_time
Definition: hops_leap_seconds.hh:157
std::chrono::time_point< hops_clock, Duration > hops_time
Class hops_time.
Definition: MHO_Clock.hh:395
std::chrono::time_point< tai_clock, Duration > tai_time
Definition: hops_leap_seconds.hh:158
std::basic_ostream< CharT, Traits > & to_stream(std::basic_ostream< CharT, Traits > &os, const CharT *fmt, const hops_time< Duration > &t)
Converts hops_time to stream format using given fmt and outputs to os.
Definition: MHO_Clock.hh:562
double second
Definition: legacy_hops_date.hh:22
short year
Definition: legacy_hops_date.hh:18
A struct to avoid name collisions between the mk4utils 'data' struct and the 'date' header library.
Definition: legacy_hops_date.hh:17
struct token_struct * tokens
Definition: parse_control_file.c:26
static utc_time< Duration > to_utc(const gps_time< Duration > &t)
Definition: hops_leap_seconds.hh:292
static gps_time< Duration > from_utc(const utc_time< Duration > &t)
Definition: hops_leap_seconds.hh:297
static utc_time< Duration > to_utc(const tai_time< Duration > &t)
Definition: hops_leap_seconds.hh:270
static tai_time< Duration > from_utc(const utc_time< Duration > &t)
Definition: hops_leap_seconds.hh:275
static date::sys_time< typename std::common_type< Duration, std::chrono::seconds >::type > to_sys(const utc_time< Duration > &ut)
Definition: hops_leap_seconds.hh:231
static utc_time< typename std::common_type< Duration, std::chrono::seconds >::type > from_local(const date::local_time< Duration > &lt)
Definition: hops_leap_seconds.hh:248
static date::local_time< typename std::common_type< Duration, std::chrono::seconds >::type > to_local(const utc_time< Duration > &ut)
Definition: hops_leap_seconds.hh:240
static utc_time< std::chrono::nanoseconds > now()
Definition: hops_leap_seconds.hh:253
static utc_time< typename std::common_type< Duration, std::chrono::seconds >::type > from_sys(const date::sys_time< Duration > &st)
Definition: hops_leap_seconds.hh:221
Definition: vex.h:175