Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
time_utils.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
11#pragma once
12
13#include <chrono>
14#include <string>
15#include <sstream>
16#include <iomanip>
17#include <ctime>
18
19namespace kcenon::logger::utils {
20
28public:
38 static std::string format_timestamp(
39 const std::chrono::system_clock::time_point& tp
40 ) {
41 auto time_t = std::chrono::system_clock::to_time_t(tp);
42 std::tm tm_buf{};
43
44#ifdef _WIN32
45 localtime_s(&tm_buf, &time_t); // Windows thread-safe version
46#else
47 localtime_r(&time_t, &tm_buf); // POSIX thread-safe version
48#endif
49
50 // Format base timestamp
51 char buffer[32];
52 std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &tm_buf);
53
54 // Add milliseconds
55 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
56 tp.time_since_epoch()
57 ) % 1000;
58
59 std::ostringstream oss;
60 oss << buffer << "."
61 << std::setfill('0') << std::setw(3) << ms.count();
62
63 return oss.str();
64 }
65
76 static std::string format_iso8601(
77 const std::chrono::system_clock::time_point& tp
78 ) {
79 auto time_t = std::chrono::system_clock::to_time_t(tp);
80 std::tm tm_buf{};
81
82#ifdef _WIN32
83 gmtime_s(&tm_buf, &time_t); // Windows thread-safe version (UTC)
84#else
85 gmtime_r(&time_t, &tm_buf); // POSIX thread-safe version (UTC)
86#endif
87
88 // Format base timestamp (ISO 8601)
89 char buffer[32];
90 std::strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%S", &tm_buf);
91
92 // Add milliseconds
93 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
94 tp.time_since_epoch()
95 ) % 1000;
96
97 std::ostringstream oss;
98 oss << buffer << "."
99 << std::setfill('0') << std::setw(3) << ms.count()
100 << "Z"; // UTC timezone indicator
101
102 return oss.str();
103 }
104
115 static std::string format_compact(
116 const std::chrono::system_clock::time_point& tp
117 ) {
118 auto time_t = std::chrono::system_clock::to_time_t(tp);
119 std::tm tm_buf{};
120
121#ifdef _WIN32
122 localtime_s(&tm_buf, &time_t);
123#else
124 localtime_r(&time_t, &tm_buf);
125#endif
126
127 // Format compact timestamp
128 char buffer[32];
129 std::strftime(buffer, sizeof(buffer), "%Y%m%d%H%M%S", &tm_buf);
130
131 // Add milliseconds
132 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
133 tp.time_since_epoch()
134 ) % 1000;
135
136 std::ostringstream oss;
137 oss << buffer
138 << std::setfill('0') << std::setw(3) << ms.count();
139
140 return oss.str();
141 }
142
156 static std::string format_for_rotation(
157 const std::chrono::system_clock::time_point& tp,
158 bool include_hour = false
159 ) {
160 auto time_t = std::chrono::system_clock::to_time_t(tp);
161 std::tm tm_buf{};
162
163#ifdef _WIN32
164 localtime_s(&tm_buf, &time_t);
165#else
166 localtime_r(&time_t, &tm_buf);
167#endif
168
169 char buffer[32];
170 if (include_hour) {
171 std::strftime(buffer, sizeof(buffer), "%Y%m%d_%H", &tm_buf);
172 } else {
173 std::strftime(buffer, sizeof(buffer), "%Y%m%d", &tm_buf);
174 }
175
176 return std::string(buffer);
177 }
178
185 static std::chrono::system_clock::time_point now() {
186 return std::chrono::system_clock::now();
187 }
188};
189
190} // namespace kcenon::logger::utils
Time utility functions for timestamp formatting.
Definition time_utils.h:27
static std::string format_timestamp(const std::chrono::system_clock::time_point &tp)
Format timestamp to human-readable format (YYYY-MM-DD HH:MM:SS.mmm)
Definition time_utils.h:38
static std::string format_iso8601(const std::chrono::system_clock::time_point &tp)
Format timestamp to ISO 8601 / RFC 3339 format with UTC timezone.
Definition time_utils.h:76
static std::string format_for_rotation(const std::chrono::system_clock::time_point &tp, bool include_hour=false)
Format timestamp for file rotation (YYYYMMDD or YYYYMMDD_HH)
Definition time_utils.h:156
static std::chrono::system_clock::time_point now()
Get current system time as time_point.
Definition time_utils.h:185
static std::string format_compact(const std::chrono::system_clock::time_point &tp)
Format timestamp to compact format (YYYYMMDDHHMMSSmmm)
Definition time_utils.h:115