sway/examples
Cameron Carstens d6804729c9
Some checks failed
CI / build-sway-examples (push) Has been cancelled
github pages / deploy (push) Has been cancelled
Codspeed Benchmarks / benchmarks (push) Has been cancelled
CI / forc-unit-tests (push) Has been cancelled
CI / check-dependency-version-formats (push) Has been cancelled
CI / check-forc-manifest-version (push) Has been cancelled
CI / get-fuel-core-version (push) Has been cancelled
CI / build-sway-lib-std (push) Has been cancelled
CI / build-reference-examples (push) Has been cancelled
CI / forc-fmt-check-sway-lib-std (push) Has been cancelled
CI / forc-fmt-check-sway-examples (push) Has been cancelled
CI / forc-fmt-check-panic (push) Has been cancelled
CI / check-sdk-harness-test-suite-compatibility (push) Has been cancelled
CI / build-mdbook (push) Has been cancelled
CI / cargo-test-sway-lsp (push) Has been cancelled
CI / build-forc-doc-sway-lib-std (push) Has been cancelled
CI / build-forc-test-project (push) Has been cancelled
CI / cargo-build-workspace (push) Has been cancelled
CI / cargo-clippy (push) Has been cancelled
CI / cargo-toml-fmt-check (push) Has been cancelled
CI / cargo-fmt-check (push) Has been cancelled
CI / cargo-test-forc (push) Has been cancelled
CI / cargo-run-e2e-test-evm (push) Has been cancelled
CI / cargo-test-lib-std (push) Has been cancelled
CI / forc-run-benchmarks (push) Has been cancelled
CI / forc-pkg-fuels-deps-check (push) Has been cancelled
CI / cargo-test-workspace (push) Has been cancelled
CI / cargo-unused-deps-check (push) Has been cancelled
CI / pre-publish-check (push) Has been cancelled
CI / verifications-complete (push) Has been cancelled
CI / cargo-run-e2e-test (push) Has been cancelled
CI / cargo-run-e2e-test-release (push) Has been cancelled
CI / cargo-test-forc-debug (push) Has been cancelled
CI / cargo-test-forc-client (push) Has been cancelled
CI / cargo-test-forc-node (push) Has been cancelled
CI / notify-slack-on-failure (push) Has been cancelled
CI / publish (push) Has been cancelled
CI / build-publish-master-image (push) Has been cancelled
CI / publish-sway-lib-std (push) Has been cancelled
CI / build-publish-release-image (push) Has been cancelled
CI / Build and upload forc binaries to release (push) Has been cancelled
Introduce the Time Library (#7206)
## 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

```sway
// 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

```sway
// 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.

```sway
// 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:

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

let three_weeks = Duration::WEEK * 3;
```

Closes https://github.com/FuelLabs/sway/issues/3945

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
2025-05-30 19:03:23 +10:00
..
abi_superabis SuperABIs for ABIs (#4272) 2023-07-27 08:45:50 +04:00
abi_supertraits set new encoding as true by default and allow it to be disabled (#5915) 2024-04-25 20:37:50 +04:00
advanced_storage_variables Merge std and core libraries (#6729) 2025-03-12 23:52:38 +01:00
arrays Merge std and core libraries (#6729) 2025-03-12 23:52:38 +01:00
asm_return_tuple_pointer Merge std and core libraries (#6729) 2025-03-12 23:52:38 +01:00
basic_storage_variables Merge std and core libraries (#6729) 2025-03-12 23:52:38 +01:00
break_and_continue Merge std and core libraries (#6729) 2025-03-12 23:52:38 +01:00
cei_analysis Merge std and core libraries (#6729) 2025-03-12 23:52:38 +01:00
configurable_constants Merge std and core libraries (#6729) 2025-03-12 23:52:38 +01:00
converting_types Merge std and core libraries (#6729) 2025-03-12 23:52:38 +01:00
counter Merge std and core libraries (#6729) 2025-03-12 23:52:38 +01:00
enums Fix module visibility check (#6685) 2024-11-15 05:59:45 +01:00
fizzbuzz Merge std and core libraries (#6729) 2025-03-12 23:52:38 +01:00
hashing Merge std and core libraries (#6729) 2025-03-12 23:52:38 +01:00
identity Merge std and core libraries (#6729) 2025-03-12 23:52:38 +01:00
liquidity_pool Merge std and core libraries (#6729) 2025-03-12 23:52:38 +01:00
match_expressions Merge std and core libraries (#6729) 2025-03-12 23:52:38 +01:00
methods_and_associated_functions Merge std and core libraries (#6729) 2025-03-12 23:52:38 +01:00
msg_sender Merge std and core libraries (#6729) 2025-03-12 23:52:38 +01:00
multi_contract_calls Merge std and core libraries (#6729) 2025-03-12 23:52:38 +01:00
native_asset Merge std and core libraries (#6729) 2025-03-12 23:52:38 +01:00
nested_storage_variables Merge std and core libraries (#6729) 2025-03-12 23:52:38 +01:00
option Merge std and core libraries (#6729) 2025-03-12 23:52:38 +01:00
ownership Merge std and core libraries (#6729) 2025-03-12 23:52:38 +01:00
ref_mut_params Merge std and core libraries (#6729) 2025-03-12 23:52:38 +01:00
result Merge std and core libraries (#6729) 2025-03-12 23:52:38 +01:00
signatures Merge std and core libraries (#6729) 2025-03-12 23:52:38 +01:00
storage_example Merge std and core libraries (#6729) 2025-03-12 23:52:38 +01:00
storage_map Merge std and core libraries (#6729) 2025-03-12 23:52:38 +01:00
storage_namespace Merge std and core libraries (#6729) 2025-03-12 23:52:38 +01:00
storage_vec Merge std and core libraries (#6729) 2025-03-12 23:52:38 +01:00
struct_storage_variables Merge std and core libraries (#6729) 2025-03-12 23:52:38 +01:00
structs Struct field privacy (#5508) 2024-01-30 17:15:24 +04:00
time Introduce the Time Library (#7206) 2025-05-30 19:03:23 +10:00
traits bug: Update swayfmt::Config & remove partial implementations (#5193) 2023-10-31 20:39:19 -07:00
tuples ci: fixing typos programmatically (#5975) 2024-05-09 10:21:32 +10:00
type_aliases Merge std and core libraries (#6729) 2025-03-12 23:52:38 +01:00
upgradeable_proxy Merge std and core libraries (#6729) 2025-03-12 23:52:38 +01:00
vec Merge std and core libraries (#6729) 2025-03-12 23:52:38 +01:00
wallet_abi Merge std and core libraries (#6729) 2025-03-12 23:52:38 +01:00
wallet_contract_caller_script Merge std and core libraries (#6729) 2025-03-12 23:52:38 +01:00
wallet_smart_contract Merge std and core libraries (#6729) 2025-03-12 23:52:38 +01:00
Forc.lock Introduce the Time Library (#7206) 2025-05-30 19:03:23 +10:00
Forc.toml Introduce the Time Library (#7206) 2025-05-30 19:03:23 +10:00