TEC
A lightweight C++ library enabling safe, efficient execution in multithreaded and concurrent systems.
Loading...
Searching...
No Matches
tec_timestamp.hpp
Go to the documentation of this file.
1// Time-stamp: <Last changed 2026-02-20 16:05:28 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----------------------------------------------------------------------*/
25#pragma once
26
27#include <cstdlib>
28#include <string>
29#include <ctime>
30#include <chrono>
31#include <mutex>
32
33#include "tec/tec_def.hpp" // IWYU pragma: keep
34
35
36namespace tec {
37
53struct Timestamp {
56
58 using count_t = int64_t;
59
61 using duration_t = std::chrono::nanoseconds;
62
64 using system_clock_t = std::chrono::system_clock;
65
67 using time_point_t = std::chrono::time_point<system_clock_t, duration_t>;
68
70
81
82 // ──────────────────────────────────────────────────────────────────────────
83 // Constructors
84 // ──────────────────────────────────────────────────────────────────────────
85
88 : count{0}
89 {}
90
93 : count{_count}
94 {}
95
98 : count{d.count()}
99 {}
100
101 Timestamp(const Timestamp&) = default;
102 Timestamp(Timestamp&&) = default;
103
104 // ──────────────────────────────────────────────────────────────────────────
105 // Accessors / Conversions
106 // ──────────────────────────────────────────────────────────────────────────
107
112 duration_t dur() const {
113 duration_t d{count};
114 return d;
115 }
116
122 std::tm utc_time() const {
123 // To prevent possible problems with std::gmtime that may not be thread-safe.
124 std::lock_guard<std::mutex> lk(time_mutex::get());
125 auto tp = time_point_t{dur()};
126 auto tt = system_clock_t::to_time_t(tp);
127 auto tm_utc = *std::gmtime(&tt);
128 return tm_utc;
129 }
130
136 std::tm local_time() const {
137 // To prevent possible problems with std::localtime that may not be thread-safe.
138 std::lock_guard<std::mutex> lk(time_mutex::get());
139 auto tp = time_point_t{dur()};
140 auto tt = system_clock_t::to_time_t(tp);
141 auto tm_local = *std::localtime(&tt);
142 return tm_local;
143 }
144
149 std::string utc_time_str() const {
150 static constexpr char fmt[] = "%FT%TZ";
151 auto tm = utc_time();
152 return iso_8601(fmt, &tm);
153 }
154
159 std::string local_time_str() const {
160 static constexpr char fmt[] = "%FT%T%z";
161 auto tm = local_time();
162 return iso_8601(fmt, &tm);
163 }
164
172 static Timestamp now() {
173 auto now = system_clock_t::now();
174 auto d = now.time_since_epoch();
175 return {std::chrono::duration_cast<duration_t>(d).count()};
176 }
177
178private:
185 static std::string iso_8601(const char* fmt, std::tm* tm) {
186 char buf[80];
187 std::strftime(buf, sizeof(buf)-1, fmt, tm);
188 return buf;
189 }
190
196 struct time_mutex {
201 static std::mutex& get() {
202 static std::mutex mtx_time__;
203 return mtx_time__;
204 }
205 };
206
207}; // struct Timestamp
208
209} // namespace tec
A point in time.
Definition tec_timestamp.hpp:53
std::chrono::nanoseconds duration_t
Duration unit used by this timestamp (nanoseconds)
Definition tec_timestamp.hpp:61
std::string utc_time_str() const
Returns ISO 8601 string in UTC (with Z suffix)
Definition tec_timestamp.hpp:149
duration_t dur() const
Returns the duration since epoch as std::chrono::nanoseconds.
Definition tec_timestamp.hpp:112
int64_t count_t
Underlying integer type used to store nanosecond count.
Definition tec_timestamp.hpp:58
static Timestamp now()
Returns current time as Timestamp (UTC-based, nanosecond precision)
Definition tec_timestamp.hpp:172
Timestamp()
Default constructor — creates timestamp at epoch (count = 0)
Definition tec_timestamp.hpp:87
std::tm local_time() const
Returns broken-down local time as std::tm.
Definition tec_timestamp.hpp:136
Timestamp(count_t _count)
Construct from raw nanosecond count since epoch.
Definition tec_timestamp.hpp:92
std::string local_time_str() const
Returns ISO 8601 string in local time (with timezone offset)
Definition tec_timestamp.hpp:159
Timestamp(duration_t d)
Construct from std::chrono::nanoseconds duration.
Definition tec_timestamp.hpp:97
std::chrono::time_point< system_clock_t, duration_t > time_point_t
std::chrono time_point type with nanosecond precision
Definition tec_timestamp.hpp:67
count_t count
Nanoseconds since Unix epoch (1970-01-01T00:00:00Z).
Definition tec_timestamp.hpp:80
std::tm utc_time() const
Returns broken-down UTC time as std::tm.
Definition tec_timestamp.hpp:122
std::chrono::system_clock system_clock_t
Clock type used as the basis for time_point conversion.
Definition tec_timestamp.hpp:64
Common definitions and utilities for the tec namespace.