Skip to content

Commit 2b9d945

Browse files
committed
Add doxygen and benchmarck
1 parent 69f5f6e commit 2b9d945

17 files changed

Lines changed: 680 additions & 281 deletions

CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,19 @@ if (VIX_TIME_BUILD_EXAMPLES)
176176
endif()
177177
endif()
178178

179+
# ======================================================
180+
# Benchmarks
181+
# ======================================================
182+
183+
option(VIX_TIME_BUILD_BENCH "Build time module benchmarks" OFF)
184+
185+
if (VIX_TIME_BUILD_BENCH)
186+
if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/src/bench/CMakeLists.txt")
187+
add_subdirectory(src/bench)
188+
else()
189+
message(WARNING "[time/bench] VIX_TIME_BUILD_BENCH=ON but src/bench/CMakeLists.txt not found, skipping.")
190+
endif()
191+
endif()
179192

180193
# ======================================================
181194
# Summary

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,37 @@ fully type-safe, chrono-based, and explicit.
2020
- 📦 Header-only (v1)
2121
- 🚫 No hidden allocations, no magic, no dependencies
2222

23+
## Benchmarks
24+
25+
Benchmarks are built using an internal chrono-based harness
26+
(no external dependencies).
27+
28+
Build:
29+
```bash
30+
cmake -S . -B build -DVIX_TIME_BUILD_BENCH=ON
31+
cmake --build build
32+
```
33+
Run:
34+
35+
```bash
36+
./build/src/bench/vix_time_bench
37+
```
38+
39+
#### Example results (Linux, x86_64):
40+
41+
- Date.parse("YYYY-MM-DD"): ~170 ns (p50)
42+
- DateTime.parse ISO-8601: ~1.1 µs (p50)
43+
- Timestamp.now(): ~65 ns (p50)
44+
- Date.to_timestamp_utc(): ~240 ns (p50)
45+
46+
### These numbers make vix::time suitable for:
47+
48+
- logging
49+
- tracing
50+
- WAL
51+
- retry / timeout logic
52+
- scheduling
53+
2354
---
2455

2556
## Installation

examples/basic_date.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ using namespace vix::time;
1212

1313
int main()
1414
{
15-
// --------------------------------------------
16-
// Date facade (Node.js / Python-like)
17-
// --------------------------------------------
18-
15+
// Date facade
1916
Date today = Date::now();
2017
std::cout << "Today (UTC): " << today.to_string() << "\n";
2118

@@ -27,21 +24,15 @@ int main()
2724
std::cout << "Day start (epoch seconds): "
2825
<< day_start.seconds_since_epoch() << "\n";
2926

30-
// --------------------------------------------
3127
// DateTime
32-
// --------------------------------------------
33-
3428
DateTime now = DateTime::now_utc();
3529
std::cout << "Now (UTC): " << now.to_string_utc() << "\n";
3630

3731
DateTime parsed_dt = DateTime::parse("2026-02-07T10:30:15Z");
3832
std::cout << "Parsed datetime: "
3933
<< parsed_dt.to_string_utc() << "\n";
4034

41-
// --------------------------------------------
4235
// Timestamp + Duration arithmetic
43-
// --------------------------------------------
44-
4536
Timestamp t0 = Timestamp::now();
4637
Duration wait = Duration::seconds(2);
4738

@@ -51,10 +42,7 @@ int main()
5142
std::cout << "Delta (seconds): "
5243
<< delta.count_seconds() << "\n";
5344

54-
// --------------------------------------------
5545
// Steady clock (elapsed time)
56-
// --------------------------------------------
57-
5846
auto start = SteadyClock::now_chrono();
5947
// simulate work
6048
int sink = 0;

include/vix/time/Clock.hpp

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,8 @@
99
* Use of this source code is governed by a MIT license
1010
* that can be found in the LICENSE file.
1111
*
12-
* =====================================================
13-
* Vix.cpp — Time Module / Clock
14-
* =====================================================
12+
* Vix.cpp
1513
*
16-
* Clock utilities for retrieving current time.
17-
*
18-
* - System clock: wall time, epoch-based (Timestamp)
19-
* - Steady clock: monotonic time for measuring durations
20-
*
21-
* This module keeps APIs explicit and predictable.
2214
*/
2315

2416
#ifndef VIX_TIME_CLOCK_HPP
@@ -34,10 +26,25 @@ namespace vix::time
3426
/**
3527
* @brief System clock helpers (wall time).
3628
*
37-
* Returns epoch-based timestamps (nanoseconds since epoch).
29+
* @details
30+
* Use this when you need the current real-world time, such as:
31+
* - timestamps in logs
32+
* - persisted records (files, databases, events)
33+
* - user-facing "created_at" / "updated_at"
34+
*
35+
* The returned value is an epoch-based @ref Timestamp (nanoseconds since epoch).
36+
*
37+
* @note
38+
* Wall time can jump (NTP sync, manual clock change, VM suspend/resume).
39+
* For measuring elapsed time, prefer @ref SteadyClock.
3840
*/
3941
struct SystemClock
4042
{
43+
/**
44+
* @brief Get the current wall-clock time as a @ref Timestamp.
45+
*
46+
* @return Current time (nanoseconds since epoch).
47+
*/
4148
static Timestamp now() noexcept
4249
{
4350
return Timestamp::now();
@@ -47,19 +54,49 @@ namespace vix::time
4754
/**
4855
* @brief Steady/monotonic clock helpers.
4956
*
50-
* Used for measuring elapsed time (timeouts, benchmarks).
51-
* Not related to wall time and not convertible to Timestamp.
57+
* @details
58+
* Use this for measuring elapsed time reliably:
59+
* - timeouts
60+
* - benchmarks
61+
* - profiling durations
62+
*
63+
* This clock is monotonic and does not track wall time.
64+
* It is not convertible to @ref Timestamp.
65+
*
66+
* @par Beginner example
67+
* @code
68+
* using vix::time::SteadyClock;
69+
*
70+
* const auto start = SteadyClock::now_chrono();
71+
* // ... work ...
72+
* const auto elapsed = SteadyClock::since(start);
73+
* @endcode
5274
*/
5375
class SteadyClock
5476
{
5577
public:
78+
/// Internal chrono time_point type used by @ref SteadyClock.
5679
using chrono_tp = std::chrono::time_point<std::chrono::steady_clock, std::chrono::nanoseconds>;
5780

81+
/**
82+
* @brief Get a monotonic time point with nanosecond precision.
83+
*
84+
* @return Current steady_clock time_point cast to nanoseconds.
85+
*/
5886
static chrono_tp now_chrono() noexcept
5987
{
6088
return std::chrono::time_point_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now());
6189
}
6290

91+
/**
92+
* @brief Compute the elapsed duration since @p start.
93+
*
94+
* @param start A time point returned by @ref now_chrono.
95+
* @return Elapsed time as a @ref Duration.
96+
*
97+
* @note
98+
* Passing a time point from a different clock type is undefined behavior.
99+
*/
63100
static Duration since(const chrono_tp &start) noexcept
64101
{
65102
const auto delta = now_chrono() - start;

include/vix/time/Date.hpp

Lines changed: 66 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,7 @@
99
* Use of this source code is governed by a MIT license
1010
* that can be found in the LICENSE file.
1111
*
12-
* =====================================================
13-
* Vix.cpp - Time Module / Date
14-
* =====================================================
15-
*
16-
* Ergonomic date facade inspired by Node.js and Python.
17-
*
18-
* This type represents a calendar date (year-month-day) without time.
19-
* It is designed to be:
20-
* - simple to construct and parse
21-
* - explicit and predictable
22-
* - chrono-based
12+
* Vix.cpp
2313
*/
2414

2515
#ifndef VIX_TIME_DATE_HPP
@@ -37,16 +27,33 @@
3727
namespace vix::time
3828
{
3929
/**
40-
* @brief Calendar date (year-month-day).
30+
* @brief Calendar date (year-month-day), without time and without timezone.
31+
*
32+
* @details
33+
* Date is a small value type representing a calendar day.
34+
* It does not store timezone data.
35+
*
36+
* Converting a Date to a @ref Timestamp is done in UTC at 00:00:00.
37+
*
38+
* This type is designed to stay friendly for beginners (simple fields, simple parsing)
39+
* while remaining useful for advanced users (chrono-based validation and conversion).
4140
*
42-
* This is a lightweight value type. It does not store timezone data.
43-
* All conversions to Timestamp are performed in UTC at 00:00:00.
41+
* @par Beginner example
42+
* @code
43+
* using vix::time::Date;
44+
*
45+
* Date d = Date::parse("2026-02-07");
46+
* if (!d.is_valid())
47+
* return;
48+
*
49+
* std::cout << d.to_string() << "\n";
50+
* @endcode
4451
*/
4552
class Date
4653
{
4754
public:
4855
/**
49-
* @brief Construct a default date (1970-01-01).
56+
* @brief Construct the default date: 1970-01-01.
5057
*/
5158
constexpr Date() noexcept
5259
: year_(1970), month_(1), day_(1)
@@ -56,27 +63,36 @@ namespace vix::time
5663
/**
5764
* @brief Construct a date from explicit fields.
5865
*
59-
* @param year Full year (e.g. 2026)
60-
* @param month Month in [1..12]
61-
* @param day Day in [1..31] (calendar validation is done by is_valid()).
66+
* @param year Full year (e.g. 2026).
67+
* @param month Month in [1..12] (range is checked by @ref is_valid).
68+
* @param day Day in [1..31] (calendar validation is checked by @ref is_valid).
69+
*
70+
* @note
71+
* This constructor does not validate the calendar day. Use @ref is_valid
72+
* to check validity.
6273
*/
6374
constexpr Date(int year, int month, int day) noexcept
6475
: year_(year), month_(month), day_(day)
6576
{
6677
}
6778

6879
/**
69-
* @brief Current date (UTC).
80+
* @brief Current date in UTC.
81+
*
82+
* @return Today's date in UTC.
7083
*
71-
* Alias of today().
84+
* @note
85+
* This is an alias of @ref today.
7286
*/
7387
static Date now() noexcept
7488
{
7589
return today();
7690
}
7791

7892
/**
79-
* @brief Current date (UTC).
93+
* @brief Current date in UTC.
94+
*
95+
* @return Today's date in UTC.
8096
*/
8197
static Date today() noexcept
8298
{
@@ -92,11 +108,17 @@ namespace vix::time
92108
}
93109

94110
/**
95-
* @brief Parse date from "YYYY-MM-DD".
111+
* @brief Parse a date from "YYYY-MM-DD".
96112
*
97-
* On parse failure, returns the default date (1970-01-01).
113+
* @details
114+
* On parse failure, this returns the default date (1970-01-01).
98115
*
99116
* @param s Input string view.
117+
* @return Parsed date, or default date on failure.
118+
*
119+
* @note
120+
* Parsing succeeds even if the date is not a valid calendar day.
121+
* Use @ref is_valid to validate.
100122
*/
101123
static Date parse(std::string_view s) noexcept
102124
{
@@ -105,30 +127,34 @@ namespace vix::time
105127
int d = 0;
106128

107129
if (!vix::time::parse::parse_ymd(s, y, m, d))
108-
{
109130
return Date();
110-
}
111131

112132
return Date(y, m, d);
113133
}
114134

115135
/**
116-
* @brief Return the year.
136+
* @brief Get the year component.
117137
*/
118138
constexpr int year() const noexcept { return year_; }
119139

120140
/**
121-
* @brief Return the month in [1..12] (as stored).
141+
* @brief Get the month component (as stored).
142+
*
143+
* @return Month in [1..12] if valid.
122144
*/
123145
constexpr int month() const noexcept { return month_; }
124146

125147
/**
126-
* @brief Return the day in [1..31] (as stored).
148+
* @brief Get the day component (as stored).
149+
*
150+
* @return Day in [1..31] if valid.
127151
*/
128152
constexpr int day() const noexcept { return day_; }
129153

130154
/**
131-
* @brief Check if the date is a valid calendar day.
155+
* @brief Check if this date is a valid calendar day.
156+
*
157+
* @return True if year-month-day forms a valid date, false otherwise.
132158
*/
133159
constexpr bool is_valid() const noexcept
134160
{
@@ -145,7 +171,11 @@ namespace vix::time
145171
/**
146172
* @brief Convert this date to a UTC timestamp at 00:00:00.
147173
*
148-
* If the date is invalid, returns Timestamp().
174+
* @details
175+
* If the date is invalid (@ref is_valid is false), this returns a default
176+
* constructed @ref Timestamp.
177+
*
178+
* @return UTC timestamp at midnight for this date, or default Timestamp on invalid date.
149179
*/
150180
Timestamp to_timestamp_utc() const noexcept
151181
{
@@ -167,6 +197,11 @@ namespace vix::time
167197

168198
/**
169199
* @brief Format as "YYYY-MM-DD".
200+
*
201+
* @return String representation of the date.
202+
*
203+
* @note
204+
* This does not validate the date. It prints stored fields.
170205
*/
171206
std::string to_string() const
172207
{
@@ -195,7 +230,7 @@ namespace vix::time
195230
}
196231

197232
/**
198-
* @brief Strict weak ordering.
233+
* @brief Strict weak ordering (lexicographic by year, month, day).
199234
*/
200235
friend constexpr bool operator<(const Date &a, const Date &b) noexcept
201236
{

0 commit comments

Comments
 (0)