TimeUtil

1. Overview

The TimeUtil module provides lightweight, reusable utilities for retrieving the current system time in commonly used formats. It is designed to avoid external dependencies and to be safe for use across core components, background services, and persistence layers.

This module currently exposes two standalone utility functions:

  • Retrieval of the current UNIX timestamp (seconds)
  • Retrieval of the current date in ISO-8601 calendar format

2. Dependencies

  • <chrono> – high-resolution system clock
  • <ctime> – calendar time conversion
  • C++ standard library – basic type support

3. unixNow()

3.1 Function Signature

uint64_t unixNow()

Returns the current UNIX timestamp expressed as the number of seconds elapsed since the Unix Epoch (1970-01-01T00:00:00Z).


3.2 Implementation Details

The function uses the system clock provided by std::chrono::system_clock and converts the duration since epoch to seconds:

std::chrono::duration_cast<std::chrono::seconds>(
  std::chrono::system_clock::now().time_since_epoch()
)

The result is cast to an unsigned 64-bit integer.


3.3 Return Semantics

  • Unit: seconds
  • Type: uint64_t
  • Resolution: 1 second

The function never fails and does not throw exceptions.


4. isoDateNow()

4.1 Function Signature

std::string isoDateNow()

Returns the current local date formatted as an ISO-8601 calendar date.


4.2 Date Format

The returned string follows the format:

YYYY-MM-DD

Example:

2026-01-21

4.3 Implementation Details

The function:

  • Retrieves the current time using std::time()
  • Converts it to local time via std::localtime()
  • Formats the date using std::strftime()

A fixed-size buffer is used to avoid dynamic allocation.


5. Timezone Considerations

  • unixNow() is timezone-independent (UTC-based)
  • isoDateNow() uses the system’s local timezone

This distinction is intentional:

  • UNIX timestamps are used for storage, comparison, and scheduling
  • ISO dates are intended for user-facing display and reporting

6. Thread Safety

  • unixNow() is fully thread-safe
  • isoDateNow() relies on std::localtime(), which may use internal static storage

In practice, isoDateNow() is safe for typical application usage, but it should not be called concurrently in high-contention, real-time paths.

If strict thread safety is required, consider using localtime_r() or a C++20 time API.


7. Usage Context

The TimeUtil module is typically used for:

  • Timestamping database records
  • Logging and audit trails
  • Identity leak detection and first-seen tracking
  • User-visible dates in UI summaries

8. Error Handling

This module does not expose any error states:

  • No exceptions are thrown
  • No error codes are returned
  • All functions return deterministic values

This design makes TimeUtil safe to call from low-level and early-startup code paths.


9. Performance Characteristics

  • Constant time complexity
  • No heap allocations
  • Negligible overhead

The functions are suitable for frequent invocation without measurable performance impact.