1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- #pragma once
- #include <chrono>
- #include <string>
- #include <iostream>
- #include <iomanip>
- class TimeTool {
- public:
- enum TemporalPrecision {
- TemporalPrecisionYear = 0,
- TemporalPrecisionMount = 4,
- TemporalPrecisionDay = 6,
- TemporalPrecisionHour = 8,
- TemporalPrecisionMin = 10,
- TemporalPrecisionSec = 12,
- TemporalPrecisionMs = 15,
- };
- public:
- static std::string timeDropoutStr(TemporalPrecision begain = TemporalPrecisionMs, TemporalPrecision end = TemporalPrecisionYear) {
- std::stringstream time;
- std::string t = getTime();
- int max = begain;
- int min = end;
- if (begain == TemporalPrecisionYear) {
- max += 3;
- } else if (begain == TemporalPrecisionMs) {
- max += 2;
- } else {
- max +=1;
- }
- for (int i = min; i < max; i++) {
- time << t[i];
- }
- t.clear();
- time >> t;
- return t;
- }
- static unsigned long long timeDropoutLL(TemporalPrecision begain = TemporalPrecisionMs, TemporalPrecision end = TemporalPrecisionYear) {
- std::string str = timeDropoutStr(begain, end);
- if (str.empty()) {
- return 0;
- }
- unsigned long long time_dropout = std::stoull(str);
- return time_dropout;
- }
- private:
- static std::string getTime() {
- auto now = std::chrono::system_clock::now();
- auto timet = std::chrono::system_clock::to_time_t(now);
- auto localTime = *std::gmtime(&timet);
- auto ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now).time_since_epoch().count() % 1000;
- std::stringstream ss;
- std::string str;
- ss << std::put_time(&localTime, "%Y%m%d%H%M%S") << std::setfill('0') << std::setw(3) << ms;
- ss >> str;
- return str;
- }
- };
|