Skip to content

Commit 69f5f6e

Browse files
committed
Add initial time module with Date facade and chrono primitives
1 parent c7db2f6 commit 69f5f6e

12 files changed

Lines changed: 1539 additions & 6 deletions

File tree

CMakeLists.txt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,13 @@ endif()
153153
option(VIX_TIME_BUILD_TESTS "Build time module tests" OFF)
154154

155155
if (VIX_TIME_BUILD_TESTS)
156-
include(CTest)
157-
enable_testing()
158-
add_subdirectory(tests)
156+
if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tests/CMakeLists.txt")
157+
include(CTest)
158+
enable_testing()
159+
add_subdirectory(tests)
160+
else()
161+
message(WARNING "[time/tests] VIX_TIME_BUILD_TESTS=ON but tests/CMakeLists.txt not found, skipping.")
162+
endif()
159163
endif()
160164

161165
# ======================================================
@@ -165,9 +169,14 @@ endif()
165169
option(VIX_TIME_BUILD_EXAMPLES "Build time module examples" OFF)
166170

167171
if (VIX_TIME_BUILD_EXAMPLES)
168-
add_subdirectory(examples)
172+
if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/examples/CMakeLists.txt")
173+
add_subdirectory(examples)
174+
else()
175+
message(WARNING "[time/examples] VIX_TIME_BUILD_EXAMPLES=ON but examples/CMakeLists.txt not found, skipping.")
176+
endif()
169177
endif()
170178

179+
171180
# ======================================================
172181
# Summary
173182
# ======================================================

README.md

Lines changed: 146 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,146 @@
1-
# time
2-
Provides a simple, explicit API for working with dates, times, timestamps, and durations in C++. Designed for ergonomics similar to Node.js and Python, while remaining predictable, type-safe, and aligned with modern C++ and std::chrono.
1+
# vix::time
2+
3+
**Time and date utilities for Vix.cpp.**
4+
5+
`vix::time` provides a simple, explicit, and predictable API for working with
6+
dates, times, timestamps, and durations in modern C++.
7+
8+
The design is inspired by the ergonomics of Node.js and Python, while remaining
9+
fully type-safe, chrono-based, and explicit.
10+
11+
---
12+
13+
## Features
14+
15+
- 📅 **Date facade** (`Date::now()`, `Date::parse()`)
16+
- 🕒 **DateTime** with UTC support and ISO-8601 parsing
17+
-**Timestamp** (epoch-based, nanosecond precision)
18+
-**Duration** (strongly typed, chrono-backed)
19+
- 🧭 **System & Steady clocks**
20+
- 📦 Header-only (v1)
21+
- 🚫 No hidden allocations, no magic, no dependencies
22+
23+
---
24+
25+
## Installation
26+
27+
`vix::time` is designed to be used as part of the Vix umbrella project.
28+
29+
```cmake
30+
add_subdirectory(modules/time)
31+
```
32+
33+
Or link against the exported target:
34+
35+
```cmake
36+
target_link_libraries(your_target PRIVATE vix::time)
37+
```
38+
39+
---
40+
41+
## Quick Example
42+
43+
```cpp
44+
#include <vix/time/time.hpp>
45+
#include <iostream>
46+
47+
using namespace vix::time;
48+
49+
int main()
50+
{
51+
Date today = Date::now();
52+
std::cout << today.to_string() << "\n";
53+
54+
DateTime now = DateTime::now_utc();
55+
std::cout << now.to_string_utc() << "\n";
56+
57+
Timestamp t0 = Timestamp::now();
58+
Timestamp t1 = t0 + Duration::seconds(1);
59+
60+
Duration delta = t1 - t0;
61+
std::cout << delta.count_seconds() << "s\n";
62+
}
63+
```
64+
65+
---
66+
67+
## API Overview
68+
69+
### Date
70+
71+
```cpp
72+
Date::now();
73+
Date::today();
74+
Date::parse("YYYY-MM-DD");
75+
```
76+
77+
Represents a calendar date without time.
78+
79+
### DateTime
80+
81+
```cpp
82+
DateTime::now_utc();
83+
DateTime::parse("YYYY-MM-DDTHH:MM:SSZ");
84+
```
85+
86+
Represents a full date + time in UTC.
87+
88+
### Timestamp
89+
90+
```cpp
91+
Timestamp::now();
92+
Timestamp::from_seconds(1700000000);
93+
```
94+
95+
Absolute point in time (nanoseconds since Unix epoch).
96+
97+
### Duration
98+
99+
```cpp
100+
Duration::seconds(5);
101+
Duration::milliseconds(250);
102+
```
103+
104+
Strongly typed time spans.
105+
106+
### Clocks
107+
108+
```cpp
109+
SystemClock::now(); // wall time (Timestamp)
110+
SteadyClock::now_chrono(); // monotonic clock
111+
SteadyClock::since(start); // elapsed Duration
112+
```
113+
114+
---
115+
116+
## Design Principles
117+
118+
- Explicit over implicit
119+
- Strong types instead of raw integers
120+
- Predictable behavior
121+
- Minimal surface API
122+
- No hidden global state
123+
124+
`vix::time` is intended to be a foundational module used by:
125+
126+
- sync
127+
- cache
128+
- WAL
129+
- retry / timeout logic
130+
- scheduling
131+
132+
---
133+
134+
## Status
135+
136+
- **Version**: v0.1.0
137+
- **Mode**: Header-only
138+
- **C++ Standard**: C++20
139+
- **Stability**: Experimental (API subject to refinement)
140+
141+
---
142+
143+
## License
144+
145+
MIT License
146+
Copyright © 2026 Gaspard Kirira

examples/CMakeLists.txt

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# @file CMakeLists.txt
2+
# @author Gaspard Kirira
3+
#
4+
# Copyright 2026, Gaspard Kirira.
5+
# All rights reserved.
6+
# https://github.com/vixcpp/time
7+
#
8+
# Use of this source code is governed by a MIT license
9+
# that can be found in the LICENSE file.
10+
#
11+
# ====================================================================
12+
# Vix.cpp - vix::time examples
13+
# ====================================================================
14+
15+
cmake_minimum_required(VERSION 3.20)
16+
17+
# Collect example sources (each .cpp becomes one executable)
18+
file(GLOB EXAMPLE_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
19+
20+
if (NOT EXAMPLE_SOURCES)
21+
message(STATUS "[time/examples] No .cpp examples found.")
22+
return()
23+
endif()
24+
25+
message(STATUS "[time/examples] Found ${EXAMPLE_SOURCES}")
26+
27+
foreach (src ${EXAMPLE_SOURCES})
28+
get_filename_component(name ${src} NAME_WE)
29+
30+
# Prefix to avoid collisions in umbrella builds
31+
set(target "vix_time_example_${name}")
32+
33+
add_executable(${target} ${src})
34+
35+
target_compile_features(${target} PRIVATE cxx_std_20)
36+
37+
# Link the module
38+
target_link_libraries(${target} PRIVATE vix::time)
39+
40+
# Optional umbrella helpers (if present)
41+
if (TARGET vix_warnings)
42+
target_link_libraries(${target} PRIVATE vix_warnings)
43+
endif()
44+
45+
if (VIX_ENABLE_SANITIZERS AND TARGET vix_sanitizers)
46+
target_link_libraries(${target} PRIVATE vix_sanitizers)
47+
endif()
48+
49+
# Nice output folder in IDEs
50+
set_target_properties(${target} PROPERTIES
51+
FOLDER "examples/time"
52+
OUTPUT_NAME ${name}
53+
)
54+
55+
message(STATUS "[time/examples] Added ${target} -> ${name}")
56+
endforeach()

examples/basic_date.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* @file basic_date.cpp
3+
*
4+
* Minimal example demonstrating the Vix time module.
5+
*/
6+
7+
#include <iostream>
8+
9+
#include <vix/time/time.hpp>
10+
11+
using namespace vix::time;
12+
13+
int main()
14+
{
15+
// --------------------------------------------
16+
// Date facade (Node.js / Python-like)
17+
// --------------------------------------------
18+
19+
Date today = Date::now();
20+
std::cout << "Today (UTC): " << today.to_string() << "\n";
21+
22+
Date parsed = Date::parse("2026-02-07");
23+
std::cout << "Parsed date: " << parsed.to_string() << "\n";
24+
25+
// Convert date to timestamp (00:00:00 UTC)
26+
Timestamp day_start = parsed.to_timestamp_utc();
27+
std::cout << "Day start (epoch seconds): "
28+
<< day_start.seconds_since_epoch() << "\n";
29+
30+
// --------------------------------------------
31+
// DateTime
32+
// --------------------------------------------
33+
34+
DateTime now = DateTime::now_utc();
35+
std::cout << "Now (UTC): " << now.to_string_utc() << "\n";
36+
37+
DateTime parsed_dt = DateTime::parse("2026-02-07T10:30:15Z");
38+
std::cout << "Parsed datetime: "
39+
<< parsed_dt.to_string_utc() << "\n";
40+
41+
// --------------------------------------------
42+
// Timestamp + Duration arithmetic
43+
// --------------------------------------------
44+
45+
Timestamp t0 = Timestamp::now();
46+
Duration wait = Duration::seconds(2);
47+
48+
Timestamp t1 = t0 + wait;
49+
Duration delta = t1 - t0;
50+
51+
std::cout << "Delta (seconds): "
52+
<< delta.count_seconds() << "\n";
53+
54+
// --------------------------------------------
55+
// Steady clock (elapsed time)
56+
// --------------------------------------------
57+
58+
auto start = SteadyClock::now_chrono();
59+
// simulate work
60+
int sink = 0;
61+
for (int i = 0; i < 1'000'000; ++i)
62+
sink += i;
63+
Duration elapsed = SteadyClock::since(start);
64+
65+
std::cout << "Elapsed (ms): "
66+
<< elapsed.count_ms() << "\n";
67+
68+
return 0;
69+
}

include/vix/time/Clock.hpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* @file Clock.hpp
3+
* @author Gaspard Kirira
4+
*
5+
* Copyright 2026, Gaspard Kirira.
6+
* All rights reserved.
7+
* https://github.com/vixcpp/time
8+
*
9+
* Use of this source code is governed by a MIT license
10+
* that can be found in the LICENSE file.
11+
*
12+
* =====================================================
13+
* Vix.cpp — Time Module / Clock
14+
* =====================================================
15+
*
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.
22+
*/
23+
24+
#ifndef VIX_TIME_CLOCK_HPP
25+
#define VIX_TIME_CLOCK_HPP
26+
27+
#include <chrono>
28+
29+
#include <vix/time/Duration.hpp>
30+
#include <vix/time/Timestamp.hpp>
31+
32+
namespace vix::time
33+
{
34+
/**
35+
* @brief System clock helpers (wall time).
36+
*
37+
* Returns epoch-based timestamps (nanoseconds since epoch).
38+
*/
39+
struct SystemClock
40+
{
41+
static Timestamp now() noexcept
42+
{
43+
return Timestamp::now();
44+
}
45+
};
46+
47+
/**
48+
* @brief Steady/monotonic clock helpers.
49+
*
50+
* Used for measuring elapsed time (timeouts, benchmarks).
51+
* Not related to wall time and not convertible to Timestamp.
52+
*/
53+
class SteadyClock
54+
{
55+
public:
56+
using chrono_tp = std::chrono::time_point<std::chrono::steady_clock, std::chrono::nanoseconds>;
57+
58+
static chrono_tp now_chrono() noexcept
59+
{
60+
return std::chrono::time_point_cast<std::chrono::nanoseconds>(std::chrono::steady_clock::now());
61+
}
62+
63+
static Duration since(const chrono_tp &start) noexcept
64+
{
65+
const auto delta = now_chrono() - start;
66+
return Duration(std::chrono::duration_cast<std::chrono::nanoseconds>(delta));
67+
}
68+
};
69+
70+
} // namespace vix::time
71+
72+
#endif // VIX_TIME_CLOCK_HPP

0 commit comments

Comments
 (0)