| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2011 Jürgen Riegel <[email protected]> * |
| 5 |
* Copyright (c) 2024 Ladislav Michl <[email protected]> * |
| 6 |
* * |
| 7 |
* This file is part of the FreeCAD CAx development system. * |
| 8 |
* * |
| 9 |
* This library is free software; you can redistribute it and/or * |
| 10 |
* modify it under the terms of the GNU Library General Public * |
| 11 |
* License as published by the Free Software Foundation; either * |
| 12 |
* version 2 of the License, or (at your option) any later version. * |
| 13 |
* * |
| 14 |
* This library is distributed in the hope that it will be useful, * |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 17 |
* GNU Library General Public License for more details. * |
| 18 |
* * |
| 19 |
* You should have received a copy of the GNU Library General Public * |
| 20 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 21 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 22 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 23 |
* * |
| 24 |
***************************************************************************/ |
| 25 |
|
| 26 |
|
| 27 |
#ifndef BASE_TIMEINFO_H |
| 28 |
#define BASE_TIMEINFO_H |
| 29 |
|
| 30 |
#include <chrono> |
| 31 |
#include <sstream> |
| 32 |
#include <string> |
| 33 |
#include <FCGlobal.h> |
| 34 |
|
| 35 |
namespace Base |
| 36 |
{ |
| 37 |
|
| 38 |
using Clock = std::chrono::system_clock; |
| 39 |
|
| 40 |
class TimeInfo: public std::chrono::time_point<Clock> |
| 41 |
{ |
| 42 |
private: |
| 43 |
bool _null; |
| 44 |
|
| 45 |
public: |
| 46 |
TimeInfo() |
| 47 |
{ |
| 48 |
setCurrent(); |
| 49 |
} |
| 50 |
|
| 51 |
TimeInfo(const TimeInfo&) = default; |
| 52 |
TimeInfo(TimeInfo&&) = default; |
| 53 |
~TimeInfo() = default; |
| 54 |
|
| 55 |
void setCurrent() |
| 56 |
{ |
| 57 |
static_cast<std::chrono::time_point<Clock>&>(*this) = Clock::now(); |
| 58 |
_null = false; |
| 59 |
} |
| 60 |
|
| 61 |
void setTime_t(std::time_t time) |
| 62 |
{ |
| 63 |
static_cast<std::chrono::time_point<Clock>&>(*this) = Clock::from_time_t(time); |
| 64 |
_null = false; |
| 65 |
} |
| 66 |
|
| 67 |
std::time_t getTime_t() |
| 68 |
{ |
| 69 |
return Clock::to_time_t(*this); |
| 70 |
} |
| 71 |
|
| 72 |
static float diffTimeF(const TimeInfo& start, const TimeInfo& end = TimeInfo()) |
| 73 |
{ |
| 74 |
const std::chrono::duration<float> duration = end - start; |
| 75 |
return duration.count(); |
| 76 |
} |
| 77 |
|
| 78 |
static std::string diffTime(const TimeInfo& start, const TimeInfo& end = TimeInfo()) |
| 79 |
{ |
| 80 |
std::stringstream ss; |
| 81 |
const std::chrono::duration<float> secs = end - start; |
| 82 |
ss << secs.count(); |
| 83 |
return ss.str(); |
| 84 |
} |
| 85 |
|
| 86 |
bool isNull() const |
| 87 |
{ |
| 88 |
return _null; |
| 89 |
} |
| 90 |
|
| 91 |
static TimeInfo null() |
| 92 |
{ |
| 93 |
TimeInfo ti; |
| 94 |
ti._null = true; |
| 95 |
return ti; |
| 96 |
} |
| 97 |
}; // class TimeInfo |
| 98 |
|
| 99 |
using Ticks = std::chrono::steady_clock; |
| 100 |
|
| 101 |
class TimeElapsed: public std::chrono::time_point<Ticks> |
| 102 |
{ |
| 103 |
public: |
| 104 |
TimeElapsed() |
| 105 |
{ |
| 106 |
setCurrent(); |
| 107 |
} |
| 108 |
|
| 109 |
TimeElapsed(const TimeElapsed&) = default; |
| 110 |
TimeElapsed(TimeElapsed&&) = default; |
| 111 |
~TimeElapsed() = default; |
| 112 |
|
| 113 |
void setCurrent() |
| 114 |
{ |
| 115 |
static_cast<std::chrono::time_point<Ticks>&>(*this) = Ticks::now(); |
| 116 |
} |
| 117 |
|
| 118 |
static float diffTimeF(const TimeElapsed& start, const TimeElapsed& end = TimeElapsed()) |
| 119 |
{ |
| 120 |
const std::chrono::duration<float> duration = end - start; |
| 121 |
return duration.count(); |
| 122 |
} |
| 123 |
|
| 124 |
static std::string diffTime(const TimeElapsed& start, const TimeElapsed& end = TimeElapsed()) |
| 125 |
{ |
| 126 |
std::stringstream ss; |
| 127 |
const std::chrono::duration<float> secs = end - start; |
| 128 |
ss << secs.count(); |
| 129 |
return ss.str(); |
| 130 |
} |
| 131 |
}; // class TimeElapsed |
| 132 |
|
| 133 |
} // namespace Base |
| 134 |
|
| 135 |
#endif // BASE_TIMEINFO_H |