Skip to content

Add time namespace with clock functions and module tests#387

Open
zendrx wants to merge 12 commits into
buzz-language:mainfrom
zendrx:time-feature
Open

Add time namespace with clock functions and module tests#387
zendrx wants to merge 12 commits into
buzz-language:mainfrom
zendrx:time-feature

Conversation

@zendrx

@zendrx zendrx commented Jul 20, 2026

Copy link
Copy Markdown

This PR adds a new time module to the Buzz standard library, providing essential time-related functions for scripting and application development.

Changes

  • src/lib/time.buzz: New file with public API declarations.
  • src/lib/buzz_time.zig: New file with Zig implementation.
  • src/lib/lib.zig: Updated to include the new time module.
  • tests/behavior/time.buzz: New file with comprehensive tests.

API Overview

import "buzz:time";

// Get current wall-clock time (ms since Unix epoch)
let now = time\now();

// Get monotonic clock for intervals (never goes backward)
let start = time\monotonic();
// ... do work ...
let elapsed = time\monotonic() - start;

// Get CPU time used by the process
let cpu = time\cpu() ! {
    // CPU clock may not be available on all platforms
};

// Format a timestamp as ISO 8601 string
let iso = time\format(now) ! { ... };

// Parse an ISO 8601 string back to a timestamp
let ts = time\parse("2026-07-20T14:30:00Z") ! { ... };

Implementation Details

Uses Zig's std.Io.Clock for cross-platform time handling.

  • now() uses .real clock (wall-clock time, Unix epoch).
  • monotonic() uses .awake clock (never goes backward, for intervals).
  • cpu() uses .cpu_process clock (CPU time used by the process).
  • format() and parse() support ISO 8601 strings.

Testing

  • Added tests/behavior/time.buzz with tests for all functions.
  • Tests cover basic functionality, roundtrip formatting/parsing, and error cases.
  • All tests pass on supported platforms.

Why This Is Useful

  • now(): Essential for timestamps, logging, and time-based logic.
  • monotonic(): Critical for performance measurement and benchmarking.
  • cpu(): Useful for profiling and resource monitoring.
  • format()/parse(): Standardized time representation for serialization and APIs.

Comment thread src/lib/time.buzz Outdated
Comment thread src/lib/buzz_time.zig Outdated
Comment thread src/lib/buzz_time.zig Outdated
Comment thread src/lib/buzz_time.zig Outdated
Comment thread tests/behavior/time.buzz
@@ -0,0 +1,55 @@
import "buzz:std";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I doubt you ran those tests on your machine? This is not valid buzz code.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do that soon got a lil bit stretched up

@zendrx
zendrx requested a review from giann July 21, 2026 12:12

@giann giann left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think you ran your code before requesting the review

Comment thread src/lib/buzz_time.zig Outdated
Comment thread tests/behavior/time.buzz Outdated
Comment thread tests/behavior/time.buzz Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants