Skip to content

Introduce the Time Library #7206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
May 30, 2025
Merged

Introduce the Time Library #7206

merged 11 commits into from
May 30, 2025

Conversation

bitzoic
Copy link
Member

@bitzoic bitzoic commented May 29, 2025

Description

This PR introduces a comprehensive UNIX time handling library to the Sway standard library, providing essential time manipulation capabilities for smart contracts.

Time operations are fundamental for many smart contract use cases:

  • Token vesting schedules
  • Auction deadlines
  • Time-locked transactions
  • Subscription renewals
  • Staking periods

Currently, Sway lacks standardized time utilities, forcing developers to:

  • Handle time conversions manually
  • Implement custom time logic in every contract
  • Risk errors in critical time calculations

This library solves these problems with a robust, well-tested time abstraction.

Duration Handling

// Create durations using natural units
let lock_period = Duration::days(90);
let auction_extension = Duration::minutes(15);

// Convert between units
log(lock_period.as_weeks()); // ≈12.857 weeks
log(auction_extension.as_seconds()); // 900 seconds

// Duration arithmetic
let total_lock = lock_period + Duration::weeks(2);

Blockchain Time Operations

// Get current block time
let now = Time::now();

// Create future/past timestamps
let unlock_time = now.add(Duration::days(30));
let vesting_start = now.subtract(Duration::weeks(12));

// Measure time differences
let time_elapsed = now.duration_since(vesting_start).unwrap();

TAI64 ↔ UNIX Conversion

Support for the existing TAI64 timestamp() functionality is maintained.

// Native TAI64 from blockchain
let tai_timestamp = timestamp();

// Convert to UNIX time
let unix_time = Time::from_tai64(tai_timestamp);

// Convert back to TAI64
let converted_tai = unix_time.as_tai64();
assert(tai_timestamp == converted_tai);

Benefits

  • Safety: Prevents common time calculation errors
  • Accuracy: Properly handles TAI64/UNIX conversion
  • Productivity: Reduces boilerplate for time operations
  • Consistency: Standardized approach across contracts
  • Readability: Natural time unit expressions

Future Extensions

Once support between different types for Add, Subtract, Multiply, etc are implemented, we may be able to do the following:

let now = Time::now();
let future = now + Duration::DAY;

let three_weeks = Duration::WEEK * 3;

Closes #3945

Checklist

  • I have linked to any relevant issues.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have updated the documentation where relevant (API docs, the reference, and the Sway book).
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added (or requested a maintainer to add) the necessary Breaking* or New Feature labels where relevant.
  • I have done my best to ensure that my PR adheres to the Fuel Labs Code Review Standards.
  • I have requested a review from the relevant team or maintainers.

@bitzoic bitzoic self-assigned this May 29, 2025
@bitzoic bitzoic requested review from a team as code owners May 29, 2025 03:35
@bitzoic bitzoic added enhancement New feature or request lib: std Standard library labels May 29, 2025
@bitzoic bitzoic temporarily deployed to fuel-sway-bot May 29, 2025 03:46 — with GitHub Actions Inactive
Copy link
Member

@DefiCake DefiCake left a comment

Choose a reason for hiding this comment

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

lgtm

@bitzoic bitzoic temporarily deployed to fuel-sway-bot May 30, 2025 00:11 — with GitHub Actions Inactive
@bitzoic bitzoic enabled auto-merge (squash) May 30, 2025 00:11
@bitzoic bitzoic temporarily deployed to fuel-sway-bot May 30, 2025 06:21 — with GitHub Actions Inactive
@bitzoic bitzoic merged commit d680472 into master May 30, 2025
45 checks passed
@bitzoic bitzoic deleted the bitzoic-unix branch May 30, 2025 09:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request lib: std Standard library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consider generating more readable timestamps in the standard library
3 participants