Changeset 32:d3a67511549d in rrlib_time


Ignore:
Timestamp:
02.10.2020 17:33:44 (8 weeks ago)
Author:
Max Reichardt <max.reichardt@…>
Branch:
17.03
Phase:
public
Message:

Switches tTimestamp type to steady clock time stamps. This makes it obsolete to handle clock jumps - making code less complex and avoids bugs (such bugs occur rarely - and are difficult to find, reproduce, and debug). The main reason high_resolution_clock was used initially, was missing support of std::steady_clock.

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • tests/unit_test_time.cpp

    r28 r32  
    8383    // ParseIsoTimestamp and ToIsoString tests 
    8484    std::string iso_timestamp = "2014-04-04T14:14:14.141414141+02:00"; 
    85     tTimestamp iso_timestamp_from_string = ParseIsoTimestamp(iso_timestamp); 
     85    tSystemTimestamp iso_timestamp_from_string = ParseIsoTimestamp(iso_timestamp); 
    8686 
    8787//    RRLIB_UNIT_TESTS_EQUALITY_MESSAGE("iso_timestamp should be 2014-04-04T14:14:14.141414141+02:00", iso_timestamp, ToIsoString(iso_timestamp_from_string)); 
    8888 
    8989    std::string iso_timestamp_short = "2014-04-04T14:14:14.141414+02:00"; 
    90     tTimestamp iso_timestamp_short_from_string = ParseIsoTimestamp(iso_timestamp_short); 
     90    tSystemTimestamp iso_timestamp_short_from_string = ParseIsoTimestamp(iso_timestamp_short); 
    9191//    RRLIB_UNIT_TESTS_EQUALITY_MESSAGE("iso_timestamp should be 2014-04-04T14:14:14.141414+02:00", iso_timestamp_short, ToIsoString(iso_timestamp_short_from_string)); 
    9292// 
    9393    std::string iso_timestamp_even_shorter = "2014-04-04T14:14:14+02:00"; 
    94     tTimestamp iso_timestamp_even_shorter_from_string = ParseIsoTimestamp(iso_timestamp_even_shorter); 
     94    tSystemTimestamp iso_timestamp_even_shorter_from_string = ParseIsoTimestamp(iso_timestamp_even_shorter); 
    9595//    RRLIB_UNIT_TESTS_EQUALITY_MESSAGE("iso_timestamp should be 2014-04-04T14:14:14+02:00", iso_timestamp_even_shorter, ToIsoString(iso_timestamp_even_shorter_from_string)); 
    9696// 
     
    105105 
    106106    // now tests 
    107     tTimestamp now = Now(); 
     107    tSystemTimestamp now = std::chrono::system_clock::now(); 
    108108    std::string now_string = ToIsoString(now); 
    109     tTimestamp now_from_string = ParseIsoTimestamp(now_string); 
     109    tSystemTimestamp now_from_string = ParseIsoTimestamp(now_string); 
    110110    RRLIB_UNIT_TESTS_EQUALITY_MESSAGE("timestamp should be equal to ParseIsoTimestamp(ToIsoString(timestamp))", ToIsoString(now), ToIsoString(now_from_string)); 
    111111 
     
    155155 
    156156    // GetLastFullHour Test 
    157     tTimestamp iso_timestamp_last_hour1 = GetLastFullHour(iso_timestamp_from_string); 
     157    tSystemTimestamp iso_timestamp_last_hour1 = GetLastFullHour(iso_timestamp_from_string); 
    158158    dur = iso_timestamp_from_string - iso_timestamp_last_hour1; 
    159159    tDuration expected_duration = std::chrono::duration_cast<tDuration>(std::chrono::minutes(14) + std::chrono::seconds(14) + std::chrono::nanoseconds(141414141)); 
    160160    RRLIB_UNIT_TESTS_EQUALITY_MESSAGE("Duration should be PT14M14.141414141S", ToIsoString(expected_duration), ToIsoString(dur)); 
    161161 
    162     tTimestamp iso_timestamp_last_hour2 = GetLastFullHour(iso_timestamp_even_shorter_from_string); 
     162    tSystemTimestamp iso_timestamp_last_hour2 = GetLastFullHour(iso_timestamp_even_shorter_from_string); 
    163163    dur = iso_timestamp_even_shorter_from_string - iso_timestamp_last_hour2; 
    164164    RRLIB_UNIT_TESTS_EQUALITY_MESSAGE("Duration should be PT14M14S", std::string("PT14M14S"), ToIsoString(dur)); 
     
    166166    // ParseNmeaTimestamp Test 
    167167    { 
    168       tTimestamp nmea_timestamp = ParseNmeaTimestamp("140512", "170414"); 
    169       tTimestamp reference_timestamp = ParseIsoTimestamp("2014-04-17T14:05:12+00:00"); 
     168      tSystemTimestamp nmea_timestamp = ParseNmeaTimestamp("140512", "170414"); 
     169      tSystemTimestamp reference_timestamp = ParseIsoTimestamp("2014-04-17T14:05:12+00:00"); 
    170170      RRLIB_UNIT_TESTS_EQUALITY_MESSAGE("Timestamps should be equal", ToIsoString(reference_timestamp), ToIsoString(nmea_timestamp)); 
    171171    } 
    172172    { 
    173       tTimestamp nmea_timestamp = ParseNmeaTimestamp("140512.123", "170414"); 
    174       tTimestamp reference_timestamp = ParseIsoTimestamp("2014-04-17T14:05:12.123+00:00"); 
     173      tSystemTimestamp nmea_timestamp = ParseNmeaTimestamp("140512.123", "170414"); 
     174      tSystemTimestamp reference_timestamp = ParseIsoTimestamp("2014-04-17T14:05:12.123+00:00"); 
    175175      RRLIB_UNIT_TESTS_EQUALITY_MESSAGE("Timestamps should be equal", ToIsoString(reference_timestamp), ToIsoString(nmea_timestamp)); 
    176176    } 
    177177    { 
    178       tTimestamp nmea_timestamp = ParseNmeaTimestamp("140512.5", "170414"); 
    179       tTimestamp reference_timestamp = ParseIsoTimestamp("2014-04-17T14:05:12.5+00:00"); 
     178      tSystemTimestamp nmea_timestamp = ParseNmeaTimestamp("140512.5", "170414"); 
     179      tSystemTimestamp reference_timestamp = ParseIsoTimestamp("2014-04-17T14:05:12.5+00:00"); 
    180180      RRLIB_UNIT_TESTS_EQUALITY_MESSAGE("Timestamps should be equal", ToIsoString(reference_timestamp), ToIsoString(nmea_timestamp)); 
    181181    } 
  • time.cpp

    r28 r32  
    184184tTimestamp Now(bool precise) 
    185185{ 
    186   // TODO: implement optimized retrieval of low precision time should this become a performance issue 
    187186  return ToApplicationTime(tBaseClock::now()); 
    188187} 
     
    242241    if (new_factor != old_factor) 
    243242    { 
    244       tTimestamp system_time = std::chrono::high_resolution_clock::now(); 
     243      tTimestamp system_time = tBaseClock::now(); 
    245244      tTimestamp app_time = ToApplicationTime(system_time); 
    246245 
     
    284283 
    285284#ifdef RRLIB_TIME_PARSING_AVAILABLE 
    286 tTimestamp ParseIsoTimestamp(const std::string& s) 
     285tSystemTimestamp ParseIsoTimestamp(const std::string& s) 
    287286{ 
    288287  tm t; 
     
    329328 
    330329#ifdef RRLIB_TIME_PARSING_AVAILABLE 
    331 tTimestamp ParseNmeaTimestamp(const std::string& nmea_time, const std::string& nmea_date) 
     330tSystemTimestamp ParseNmeaTimestamp(const std::string& nmea_time, const std::string& nmea_date) 
    332331{ 
    333332  tm t; 
     
    359358#endif 
    360359 
    361 std::string ToIsoString(const tTimestamp& timestamp) 
     360std::string ToIsoString(const tSystemTimestamp& timestamp) 
    362361{ 
    363362  char buf[256], time_zone[12], sub_seconds[20]; 
     
    619618 
    620619#if __linux__ 
    621 tTimestamp GetLastFullHour(const tTimestamp &timestamp) 
     620tSystemTimestamp GetLastFullHour(const tSystemTimestamp &timestamp) 
    622621{ 
    623622  std::time_t input_time = std::chrono::system_clock::to_time_t(timestamp); 
  • time.h

    r26 r32  
    6565 * Clock that "application time" is derived from 
    6666 */ 
    67 typedef std::chrono::high_resolution_clock tBaseClock; 
     67typedef std::chrono::steady_clock tBaseClock; 
    6868 
    6969//! Time stamp type 
    7070/*! 
    7171 * std::chrono::time_point type returned by clocks. 
    72  * It should be used in most parts of applications (e.g. Timestamps, Finroc ports). 
     72 * tTimestamp should be used in most parts of applications (e.g. Timestamps, Finroc ports). 
    7373 */ 
    7474typedef tBaseClock::time_point tTimestamp; 
     75 
     76/*! Typedefs for the timestamps of steady_clock and system_clock */ 
     77typedef std::chrono::steady_clock::time_point tSteadyTimestamp; 
     78typedef std::chrono::system_clock::time_point tSystemTimestamp; 
    7579 
    7680//! Duration type 
     
    105109/*! 
    106110 * Returns "application time". 
    107  * By default this is system time. 
     111 * By default this is steady clock system time. 
    108112 * It can, however, also be simulated time (time stretching when simulating etc.). 
    109113 * In order for time stretching to work in whole applications, libraries and application components 
    110114 * that do not explicitly require system time should obtain time from this function. 
    111115 * 
    112  * Note, that obtaining precise high resolution system time can be a rather expensive system call. 
    113  * (a benchmark that did only this could merely obtain around 150K time stamps per second (a few years ago on a Core Duo Notebook processor)). 
    114  * This can be implemented in a much less expensive way, if less precision is required. 
    115  * Therefore, if high precision is not required, precise should be set to false when frequently calling this method. 
    116  * 
    117  * \param precise If true, the high resolution system clock is used. 
    118  *                If false, the time stamp is less precise (+- 25ms). 
     116 * Note, that obtaining precise high resolution system time used to be a rather expensive system call. 
     117 * On modern linux systems it is typically not. 
     118 * 
     119 * \param precise Whether a precise timestamp is required. Setting precise to false may reduce computational overhead and precision of the returned timestamp (platform-specific) 
    119120 */ 
    120121tTimestamp Now(bool precise = true); 
     
    160161 * \return Timestamp 
    161162 */ 
    162 tTimestamp ParseIsoTimestamp(const std::string& s); 
     163tSystemTimestamp ParseIsoTimestamp(const std::string& s); 
    163164#endif 
    164165 
     
    172173 * \return Timestamp 
    173174 */ 
    174 tTimestamp ParseNmeaTimestamp(const std::string& nmea_time, const std::string& nmea_date); 
     175tSystemTimestamp ParseNmeaTimestamp(const std::string& nmea_time, const std::string& nmea_date); 
    175176#endif 
    176177 
     
    181182 * \return ISO 8601 representation of timestamp 
    182183 */ 
    183 std::string ToIsoString(const tTimestamp& timestamp); 
     184std::string ToIsoString(const tSystemTimestamp& timestamp); 
    184185 
    185186#ifdef RRLIB_TIME_PARSING_AVAILABLE 
     
    203204 
    204205/*! 
     206 * Turns steady clock timestamp (typically time since system boot) into string representation of duration following ISO 8601 
     207 * 
     208 * \param timestamp Timestamp to convert 
     209 * \return ISO 8601 representation of duration since boot 
     210 */ 
     211inline std::string ToIsoString(const tSteadyTimestamp& timestamp) 
     212{ 
     213  return ToIsoString(timestamp.time_since_epoch()); 
     214} 
     215 
     216/*! 
    205217 * Turns duration into a simple string (number + unit) 
    206218 * 
     
    217229 * \return timestamp of the last full hour 
    218230 */ 
    219 tTimestamp GetLastFullHour(const tTimestamp& timestamp); 
     231tSystemTimestamp GetLastFullHour(const tSystemTimestamp& timestamp); 
    220232#endif 
    221233 
Note: See TracChangeset for help on using the changeset viewer.