TEC
A lightweight C++ library enabling safe, efficient execution in multithreaded and concurrent systems.
Loading...
Searching...
No Matches
tec_utils.hpp
Go to the documentation of this file.
1// Time-stamp: <Last changed 2026-02-20 16:07:01 by magnolia>
2/*----------------------------------------------------------------------
3------------------------------------------------------------------------
4Copyright (c) 2020-2026 The Emacs Cat (https://github.com/olddeuteronomy/tec).
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17------------------------------------------------------------------------
18----------------------------------------------------------------------*/
26#pragma once
27
28#include <chrono>
29#include <cstdio>
30#include <string>
31
32#include "tec/tec_def.hpp" // IWYU pragma: keep
33
34
35namespace tec {
36
42
47using Clock = std::chrono::steady_clock;
48
52using Seconds = std::chrono::seconds;
53
57using TimePointSec = std::chrono::time_point<Clock, Seconds>;
58
62using MilliSec = std::chrono::milliseconds;
63
67using TimePointMs = std::chrono::time_point<Clock, MilliSec>;
68
72using MicroSec = std::chrono::microseconds;
73
77using TimePointMu = std::chrono::time_point<Clock, MicroSec>;
78
85template <typename Duration>
86Duration now() {
87 return std::chrono::duration_cast<Duration>(Clock::now() - std::chrono::time_point<Clock, Duration>());
88}
89
97template <typename Duration>
98Duration since(Duration start) {
99 return now<Duration>() - start;
100}
101
108constexpr const char* time_unit(Seconds) { return "s"; }
109constexpr const char* time_unit(MilliSec) { return "ms"; }
110constexpr const char* time_unit(MicroSec) { return "mu"; }
111
117constexpr Seconds one_hour() { return Seconds(60 * 60); }
118
124constexpr Seconds one_day() { return Seconds(24 * 60 * 60); }
125
127
134template <typename Duration = MilliSec>
135class Timer {
136private:
137 Duration src_;
138
139public:
145 start();
146 }
147
152 void start() {
153 src_ = now<Duration>();
154 }
155
161 Duration stop() {
162 return now<Duration>() - src_;
163 }
164};
165
171
172#if !(defined(__TEC_WINDOWS__) || defined(__TEC_MINGW__))
173
174#include <unistd.h>
175#include <pwd.h>
176
177#if defined(__TEC_APPLE__)
178#include <sys/syslimits.h>
179#else
180#include <linux/limits.h>
181#endif
182
188inline std::string getcomputername() {
189 char host[PATH_MAX];
190 if (gethostname(host, sizeof(host)) == 0)
191 return host;
192 else
193 return "";
194}
195
201inline std::string getusername() {
202 uid_t uid = geteuid();
203 struct passwd* pw = getpwuid(uid);
204 if (pw != NULL)
205 return pw->pw_name;
206 else
207 return "";
208}
209
210#endif // !(__TEC_WINDOWS__ || __TEC_MINGW__)
211
213
214} // namespace tec
215
216#if defined(__TEC_WINDOWS__) || defined(__TEC_MINGW__)
217// MS Windows stuff goes here. NOT YET!
218#include "tec/mswin/tec_win_utils.hpp"
219#endif // __TEC_WINDOWS__
A simple timer for measuring elapsed time.
Definition tec_utils.hpp:135
Duration stop()
Stops the timer and returns the elapsed time.
Definition tec_utils.hpp:161
Timer()
Constructs and starts a timer.
Definition tec_utils.hpp:144
void start()
Starts or restarts the timer.
Definition tec_utils.hpp:152
Common definitions and utilities for the tec namespace.
std::string getusername()
Retrieves the logged-in username.
Definition tec_utils.hpp:201
constexpr Seconds one_day()
Returns a constant duration of one day.
Definition tec_utils.hpp:124
constexpr const char * time_unit(Seconds)
Returns the unit string for a duration type.
Definition tec_utils.hpp:108
std::chrono::steady_clock Clock
Type alias for a monotonic clock suitable for measuring intervals.
Definition tec_utils.hpp:47
std::chrono::time_point< Clock, MilliSec > TimePointMs
Type alias for a time point in milliseconds.
Definition tec_utils.hpp:67
std::chrono::seconds Seconds
Type alias for seconds duration.
Definition tec_utils.hpp:52
std::chrono::time_point< Clock, Seconds > TimePointSec
Type alias for a time point in seconds.
Definition tec_utils.hpp:57
Duration now()
Returns the current time as a duration since the epoch.
Definition tec_utils.hpp:86
Duration since(Duration start)
Returns the duration since a specified start time.
Definition tec_utils.hpp:98
std::chrono::milliseconds MilliSec
Type alias for milliseconds duration.
Definition tec_utils.hpp:62
constexpr Seconds one_hour()
Returns a constant duration of one hour.
Definition tec_utils.hpp:117
std::string getcomputername()
Retrieves the computer hostname.
Definition tec_utils.hpp:188
std::chrono::microseconds MicroSec
Type alias for microseconds duration.
Definition tec_utils.hpp:72
std::chrono::time_point< Clock, MicroSec > TimePointMu
Type alias for a time point in microseconds.
Definition tec_utils.hpp:77