21
21
#include < chrono>
22
22
#include < cmath>
23
23
#include < cstdint>
24
- #include < iomanip>
25
24
#include < iostream>
26
25
#include < limits>
27
- #include < regex>
28
- #include < sstream>
29
26
#include < string>
30
27
#include < tuple>
31
28
#include < utility>
@@ -687,81 +684,14 @@ namespace ignition
687
684
// / \brief Convert a std::chrono::steady_clock::time_point to a string
688
685
// / \param[in] _point The std::chrono::steady_clock::time_point to convert.
689
686
// / \return A string formatted with the time_point
690
- inline std::string timePointToString (
691
- const std::chrono::steady_clock::time_point &_point)
692
- {
693
- auto duration = _point - secNsecToTimePoint (0 , 0 );
694
- auto cleanDuration = breakDownDurations<days,
695
- std::chrono::hours,
696
- std::chrono::minutes,
697
- std::chrono::seconds,
698
- std::chrono::milliseconds>(
699
- duration);
700
- std::ostringstream output_string;
701
- output_string << std::setw (2 ) << std::setfill (' 0' )
702
- << std::get<0 >(cleanDuration).count () << " "
703
- << std::setw (2 ) << std::setfill (' 0' )
704
- << std::get<1 >(cleanDuration).count () << " :"
705
- << std::setw (2 ) << std::setfill (' 0' )
706
- << std::get<2 >(cleanDuration).count () << " :"
707
- << std::setfill (' 0' ) << std::setw (6 )
708
- << std::fixed << std::setprecision (3 )
709
- << std::get<3 >(cleanDuration).count () +
710
- std::get<4 >(cleanDuration).count ()/1000.0 ;
711
- return output_string.str ();
712
- }
687
+ std::string timePointToString (
688
+ const std::chrono::steady_clock::time_point &_point);
713
689
714
690
// / \brief Convert a std::chrono::steady_clock::duration to a string
715
691
// / \param[in] _duration The std::chrono::steady_clock::duration to convert.
716
692
// / \return A string formatted with the duration
717
- inline std::string durationToString (
718
- const std::chrono::steady_clock::duration &_duration)
719
- {
720
- auto cleanDuration = breakDownDurations<days,
721
- std::chrono::hours,
722
- std::chrono::minutes,
723
- std::chrono::seconds,
724
- std::chrono::milliseconds>(
725
- _duration);
726
- std::ostringstream outputString;
727
- outputString << std::setw (2 ) << std::setfill (' 0' )
728
- << std::get<0 >(cleanDuration).count () << " "
729
- << std::setw (2 ) << std::setfill (' 0' )
730
- << std::get<1 >(cleanDuration).count () << " :"
731
- << std::setw (2 ) << std::setfill (' 0' )
732
- << std::get<2 >(cleanDuration).count () << " :"
733
- << std::setfill (' 0' ) << std::setw (6 )
734
- << std::fixed << std::setprecision (3 )
735
- << std::get<3 >(cleanDuration).count () +
736
- std::get<4 >(cleanDuration).count ()/1000.0 ;
737
- return outputString.str ();
738
- }
739
-
740
- // The following regex takes a time string in the general format of
741
- // "dd hh:mm:ss.nnn" where n is milliseconds, if just one number is
742
- // provided, it is assumed to be seconds
743
- static const std::regex time_regex (
744
- " ^([0-9]+ ){0,1}" // day:
745
- // Any positive integer
746
-
747
- " (?:([1-9]:|[0-1][0-9]:|2[0-3]:){0,1}" // hour:
748
- // 1 - 9:
749
- // 01 - 19:
750
- // 20 - 23:
751
-
752
- " ([0-9]:|[0-5][0-9]:)){0,1}" // minute:
753
- // 0 - 9:
754
- // 00 - 59:
755
-
756
- " (?:([0-9]|[0-5][0-9]){0,1}" // second:
757
- // 0 - 9
758
- // 00 - 59
759
-
760
- " (\\ .[0-9]{1,3}){0,1})$" ); // millisecond:
761
- // .0 - .9
762
- // .00 - .99
763
- // .000 - 0.999
764
-
693
+ std::string durationToString (
694
+ const std::chrono::steady_clock::duration &_duration);
765
695
766
696
// / \brief Split a std::chrono::steady_clock::duration to a string
767
697
// / \param[in] _timeString The string to convert in general format
@@ -771,165 +701,29 @@ namespace ignition
771
701
// / \param[out] numberSeconds number of seconds in the string
772
702
// / \param[out] numberMilliseconds number of milliseconds in the string
773
703
// / \return True if the regex was able to split the string otherwise False
774
- inline bool splitTimeBasedOnTimeRegex (
704
+ bool splitTimeBasedOnTimeRegex (
775
705
const std::string &_timeString,
776
706
uint64_t & numberDays, uint64_t & numberHours,
777
707
uint64_t & numberMinutes, uint64_t & numberSeconds,
778
- uint64_t & numberMilliseconds)
779
- {
780
- std::smatch matches;
781
-
782
- // `matches` should always be a size of 6 as there are 6 matching
783
- // groups in the regex.
784
- // 1. The whole regex
785
- // 2. The days
786
- // 3. The hours
787
- // 4. The minutes
788
- // 5. The seconds
789
- // 6. The milliseconds
790
- // We can also index them as such below.
791
- // Note that the space will remain in the day match, the colon
792
- // will remain in the hour and minute matches, and the period will
793
- // remain in the millisecond match
794
- if (!std::regex_search (_timeString, matches, time_regex) ||
795
- matches.size () != 6 )
796
- return false ;
797
-
798
- std::string dayString = matches[1 ];
799
- std::string hourString = matches[2 ];
800
- std::string minuteString = matches[3 ];
801
- std::string secondString = matches[4 ];
802
- std::string millisecondString = matches[5 ];
803
-
804
- // Days are the only unbounded number, so check first to see if stoi
805
- // runs successfully
806
- if (!dayString.empty ())
807
- {
808
- // Erase the space
809
- dayString.erase (dayString.length () - 1 );
810
- try
811
- {
812
- numberDays = std::stoi (dayString);
813
- }
814
- catch (const std::out_of_range &)
815
- {
816
- return false ;
817
- }
818
- }
819
-
820
- if (!hourString.empty ())
821
- {
822
- // Erase the colon
823
- hourString.erase (hourString.length () - 1 );
824
- numberHours = std::stoi (hourString);
825
- }
826
-
827
- if (!minuteString.empty ())
828
- {
829
- // Erase the colon
830
- minuteString.erase (minuteString.length () - 1 );
831
- numberMinutes = std::stoi (minuteString);
832
- }
833
-
834
- if (!secondString.empty ())
835
- {
836
- numberSeconds = std::stoi (secondString);
837
- }
838
-
839
- if (!millisecondString.empty ())
840
- {
841
- // Erase the period
842
- millisecondString.erase (0 , 1 );
843
-
844
- // Multiplier because "4" = 400 ms, "04" = 40 ms, and "004" = 4 ms
845
- numberMilliseconds = std::stoi (millisecondString) *
846
- static_cast <uint64_t >(1000 / pow (10 , millisecondString.length ()));
847
- }
848
- return true ;
849
- }
708
+ uint64_t & numberMilliseconds);
850
709
851
710
// / \brief Convert a string to a std::chrono::steady_clock::duration
852
711
// / \param[in] _timeString The string to convert in general format
853
712
// / "dd hh:mm:ss.nnn" where n is millisecond value
854
713
// / \return A std::chrono::steady_clock::duration containing the
855
714
// / string's time value. If it isn't possible to convert, the duration will
856
715
// / be zero.
857
- inline std::chrono::steady_clock::duration stringToDuration (
858
- const std::string &_timeString)
859
- {
860
- using namespace std ::chrono_literals;
861
- std::chrono::steady_clock::duration duration{
862
- std::chrono::steady_clock::duration::zero ()};
863
-
864
- if (_timeString.empty ())
865
- return duration;
866
-
867
- uint64_t numberDays = 0 ;
868
- uint64_t numberHours = 0 ;
869
- uint64_t numberMinutes = 0 ;
870
- uint64_t numberSeconds = 0 ;
871
- uint64_t numberMilliseconds = 0 ;
872
-
873
- if (!splitTimeBasedOnTimeRegex (_timeString, numberDays, numberHours,
874
- numberMinutes, numberSeconds,
875
- numberMilliseconds))
876
- {
877
- return duration;
878
- }
879
-
880
- // TODO(anyone): Replace below day conversion with std::chrono::days.
881
- // / This will exist in C++-20
882
- duration = std::chrono::steady_clock::duration::zero ();
883
- auto delta = std::chrono::milliseconds (numberMilliseconds) +
884
- std::chrono::seconds (numberSeconds) +
885
- std::chrono::minutes (numberMinutes) +
886
- std::chrono::hours (numberHours) +
887
- std::chrono::hours (24 * numberDays);
888
- duration += delta;
889
-
890
- return duration;
891
- }
716
+ std::chrono::steady_clock::duration stringToDuration (
717
+ const std::string &_timeString);
892
718
893
719
// / \brief Convert a string to a std::chrono::steady_clock::time_point
894
720
// / \param[in] _timeString The string to convert in general format
895
721
// / "dd hh:mm:ss.nnn" where n is millisecond value
896
722
// / \return A std::chrono::steady_clock::time_point containing the
897
723
// / string's time value. If it isn't possible to convert, the time will
898
724
// / be negative 1 second.
899
- inline std::chrono::steady_clock::time_point stringToTimePoint (
900
- const std::string &_timeString)
901
- {
902
- using namespace std ::chrono_literals;
903
- std::chrono::steady_clock::time_point timePoint{-1s};
904
-
905
- if (_timeString.empty ())
906
- return timePoint;
907
-
908
- uint64_t numberDays = 0 ;
909
- uint64_t numberHours = 0 ;
910
- uint64_t numberMinutes = 0 ;
911
- uint64_t numberSeconds = 0 ;
912
- uint64_t numberMilliseconds = 0 ;
913
-
914
- if (!splitTimeBasedOnTimeRegex (_timeString, numberDays, numberHours,
915
- numberMinutes, numberSeconds,
916
- numberMilliseconds))
917
- {
918
- return timePoint;
919
- }
920
-
921
- // TODO(anyone): Replace below day conversion with std::chrono::days.
922
- // / This will exist in C++-20
923
- timePoint = math::secNsecToTimePoint (0 , 0 );
924
- auto duration = std::chrono::milliseconds (numberMilliseconds) +
925
- std::chrono::seconds (numberSeconds) +
926
- std::chrono::minutes (numberMinutes) +
927
- std::chrono::hours (numberHours) +
928
- std::chrono::hours (24 * numberDays);
929
- timePoint += duration;
930
-
931
- return timePoint;
932
- }
725
+ std::chrono::steady_clock::time_point stringToTimePoint (
726
+ const std::string &_timeString);
933
727
934
728
// Degrade precision on Windows, which cannot handle 'long double'
935
729
// values properly. See the implementation of Unpair.
0 commit comments