├── Timestamp.h └── Timestamp.cpp /Timestamp.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef TIMESTAMP_H 3 | #define TIMESTAMP_H 4 | 5 | //System includes 6 | #ifdef _WIN32 7 | typedef __int64 int64_t; 8 | #else 9 | #include 10 | #endif 11 | 12 | #include 13 | 14 | //Library includes 15 | 16 | //Local includes 17 | 18 | namespace AVN 19 | { 20 | 21 | //Return the full timestamp as a string. 22 | std::string stringFromTimestamp_full(int64_t i64Timestamp_us); 23 | 24 | //Return the HH:mm:ss timestamp as a string. 25 | std::string stringFromTimestamp_HHmmss(int64_t i64Timestamp_us); 26 | 27 | //Return the HH:mm:ss.uuuuuu timestamp as a string. 28 | std::string stringFromTimestamp_HHmmssuuuuuu(int64_t i64Timestamp_us); 29 | 30 | //This return the time in a similar for except without the date. This is useful for printing the duration 31 | //output when substract 1 timestamp from another 32 | std::string stringFromTimeDuration(int64_t i64Timestamp_us); 33 | 34 | //This function is same as above except it prints the units explicitly in the string 35 | std::string stringFromTimeDurationExplicitUnits(int64_t i64Timestamp_us); 36 | 37 | //Get current time. Uses microsecond clock if boost is available otherwise the API with second accuracy 38 | int64_t getTimeNow_us(); 39 | 40 | } // namespace AVN 41 | 42 | #endif //TIMESTAMP_H 43 | -------------------------------------------------------------------------------- /Timestamp.cpp: -------------------------------------------------------------------------------- 1 | 2 | //System includes 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | //Library includes 10 | #ifdef USE_BOOST_TIME //Define this in calling code to get more accurate time if boost is available 11 | #ifndef Q_MOC_RUN //Qt's MOC and Boost have some issues don't let MOC process boost headers 12 | #include 13 | #endif 14 | #else 15 | #include 16 | #endif 17 | 18 | //Local includes 19 | #include "Timestamp.h" 20 | 21 | using namespace std; 22 | 23 | //Return the full timestamp as a string. 24 | string AVN::stringFromTimestamp_full(int64_t i64Timestamp_us) 25 | { 26 | //make filename from zTime 27 | time_t ztime = i64Timestamp_us / 1000000; 28 | struct tm* timeInfo; 29 | timeInfo = localtime ( &ztime ); 30 | 31 | char caTimeStrA[21]; 32 | 33 | strftime ( caTimeStrA, 21, "%Y-%m-%dT%H.%M.%S.", timeInfo ); 34 | 35 | char caTimeStrB[27]; 36 | sprintf(caTimeStrB, "%s%.6llu", caTimeStrA, (long long unsigned int)i64Timestamp_us % 1000000 ); 37 | 38 | return string(caTimeStrB, 26); 39 | } 40 | 41 | //Return the HH:mm:ss timestamp as a string. 42 | string AVN::stringFromTimestamp_HHmmss(int64_t i64Timestamp_us) 43 | { 44 | //make filename from zTime 45 | time_t ztime = i64Timestamp_us / 1000000; 46 | struct tm* timeInfo; 47 | timeInfo = localtime ( &ztime ); 48 | 49 | char caTimeStr[10]; 50 | 51 | strftime ( caTimeStr, 10, "%H:%M:%S", timeInfo ); 52 | 53 | return string(caTimeStr, 8); 54 | } 55 | 56 | //Return the HH:mm:ss.uuuuuu timestamp as a string. 57 | string AVN::stringFromTimestamp_HHmmssuuuuuu(int64_t i64Timestamp_us) 58 | { 59 | //make filename from zTime 60 | time_t ztime = i64Timestamp_us / 1000000; 61 | struct tm* timeInfo; 62 | timeInfo = localtime ( &ztime ); 63 | 64 | char caTimeStrA[10]; 65 | 66 | strftime ( caTimeStrA, 10, "%H:%M:%S.", timeInfo ); 67 | 68 | char caTimeStrB[16]; 69 | sprintf(caTimeStrB, "%s%.6llu", caTimeStrA, (long long unsigned int)i64Timestamp_us % 1000000 ); 70 | 71 | return string(caTimeStrB, 15); 72 | } 73 | 74 | //This return the time in a similar for except without the date. This is useful for printing the duration 75 | //output when substract 1 timestamp from another 76 | string AVN::stringFromTimeDuration(int64_t i64Timestamp_us) 77 | { 78 | int iNMicroSeconds = i64Timestamp_us % 1000000; 79 | int iNSeconds = (i64Timestamp_us / 1000000) % 60; 80 | int iNMinutes = (i64Timestamp_us / 60000000) % 60; 81 | int iNHours = (i64Timestamp_us / 3600000000LL) % 24; 82 | int iNDays = (i64Timestamp_us / 3600000000LL / 24); 83 | bool bNegative = i64Timestamp_us < 0; 84 | 85 | //For negative time duration value all quatities end up as negative in above calculations 86 | //Only largest none zero quatity should be negative where applicable 87 | 88 | iNHours = abs(iNHours); 89 | iNMinutes = abs(iNMinutes); 90 | iNSeconds = abs(iNSeconds); 91 | iNMicroSeconds = abs(iNMicroSeconds); 92 | 93 | stringstream oSS; 94 | if(bNegative) 95 | oSS << std::string("-"); 96 | oSS << iNDays; 97 | oSS << "T"; 98 | oSS << setfill('0') << setw(2) << iNHours; 99 | oSS << "."; 100 | oSS << setfill('0') << setw(2) << iNMinutes; 101 | oSS << "."; 102 | oSS << setfill('0') << setw(2) << iNSeconds; 103 | oSS << "."; 104 | oSS << setfill('0') << setw(6) << iNMicroSeconds; 105 | 106 | return oSS.str(); 107 | } 108 | 109 | //This function is same as above except it prints the units explicitly in the string 110 | string AVN::stringFromTimeDurationExplicitUnits(int64_t i64Timestamp_us) 111 | { 112 | int iNMicroSeconds = i64Timestamp_us % 1000000; 113 | int iNSeconds = (i64Timestamp_us / 1000000) % 60; 114 | int iNMinutes = (i64Timestamp_us / 60000000) % 60; 115 | int iNHours = (i64Timestamp_us / 3600000000LL) % 24; 116 | int iNDays = (i64Timestamp_us / 3600000000LL / 24); 117 | bool bNegative = i64Timestamp_us < 0; 118 | 119 | //For negative time duration value all quatities end up as negative in above calculations 120 | //Only largest none zero quatity should be negative where applicable 121 | 122 | if(iNDays) 123 | { 124 | iNHours = abs(iNHours); 125 | iNMinutes = abs(iNMinutes); 126 | iNSeconds = abs(iNSeconds); 127 | iNMicroSeconds = abs(iNMicroSeconds); 128 | } 129 | if(iNHours) 130 | { 131 | iNMinutes = abs(iNMinutes); 132 | iNSeconds = abs(iNSeconds); 133 | iNMicroSeconds = abs(iNMicroSeconds); 134 | } 135 | if(iNMinutes) 136 | { 137 | iNSeconds = abs(iNSeconds); 138 | iNMicroSeconds = abs(iNMicroSeconds); 139 | } 140 | if(iNSeconds) 141 | { 142 | iNMicroSeconds = abs(iNMicroSeconds); 143 | } 144 | 145 | 146 | stringstream oSS; 147 | if(bNegative) 148 | oSS << std::string("-"); 149 | 150 | if(iNDays) 151 | { 152 | oSS << iNDays; 153 | oSS << "d "; 154 | } 155 | 156 | if(iNDays || iNHours) 157 | { 158 | oSS << setfill('0') << setw(2) << iNHours; 159 | oSS << "h "; 160 | } 161 | 162 | if(iNDays || iNHours || iNMinutes) 163 | { 164 | oSS << setfill('0') << setw(2) << iNMinutes; 165 | oSS << "min "; 166 | } 167 | 168 | if(iNDays || iNHours || iNMinutes || iNSeconds) 169 | { 170 | oSS << setfill('0') << setw(2) << iNSeconds; 171 | oSS << "s "; 172 | } 173 | 174 | oSS << setfill('0') << setw(6) << iNMicroSeconds; 175 | oSS << "us"; 176 | 177 | return oSS.str(); 178 | } 179 | 180 | int64_t AVN::getTimeNow_us() 181 | { 182 | //If boost is available use its microsecond clock. 183 | //Otherwise use default ctime API (only returns integer seconds) 184 | 185 | #ifdef USE_BOOST_TIME 186 | boost::posix_time::ptime oTimeNow = boost::posix_time::microsec_clock::universal_time(); 187 | boost::posix_time::ptime oEpoch = boost::posix_time::time_from_string(string("1970-01-01 00:00:00.000")); 188 | boost::posix_time::time_duration oDuration = oTimeNow - oEpoch; 189 | 190 | int64_t i64TimeNow_us = oDuration.total_microseconds(); 191 | #else 192 | time_t timeNowLocal; 193 | time ( &timeNowLocal ); 194 | struct tm *timeNowUTC = gmtime( &timeNowLocal ); 195 | 196 | struct tm epoch; 197 | epoch.tm_hour = 0; //These 3 since midnight 198 | epoch.tm_min = 0; 199 | epoch.tm_sec = 0; 200 | epoch.tm_year = 70; //Years since 1900 (we want 1970) 201 | epoch.tm_mon = 0; //Months since January 202 | epoch.tm_mday = 1; //Day of the month 203 | 204 | int64_t i64TimeNow_us = difftime( mktime(timeNowUTC), mktime(&epoch) ) * 1000000LL; 205 | #endif 206 | 207 | return i64TimeNow_us; 208 | } 209 | --------------------------------------------------------------------------------