Switchy Time
A simple time abstraction library providing unified time access with support for both standard system time and simulated time for testing.
Features
- Time Abstraction: Unified
now()andinstant_now()functions that work with different time backends - Standard Time: Use system time for production scenarios
- Simulated Time: Controllable time simulation for testing and development
- Step Control: Manually advance simulated time in discrete steps
- Epoch Offset: Configurable time offset for simulation scenarios
- Thread Local State: Per-thread time simulation state management
- Chrono Integration: Optional
chronosupport withdatetime_local_now()anddatetime_utc_now()functions
Installation
Add this to your Cargo.toml:
[]
= "0.1.4"
Choose your backend/features:
# standard backend only
= { = "0.1.4", = false, = ["std"] }
# simulator backend only
= { = "0.1.4", = false, = ["simulator"] }
# simulator backend with chrono support
= { = "0.1.4", = false, = ["simulator", "chrono"] }
Usage
Basic Time Access
use ;
use ;
Simulated Time (Testing)
use ;
Time Simulation Configuration
use ;
Real Time in Simulation Mode
use ;
Chrono Integration
use ;
Testing Time-Dependent Code
use ;
use Duration;
Environment Configuration
The simulator can be configured via environment variables:
# 1) Fixed epoch offset override (highest precedence)
# 2) Or bounded random epoch offset (inclusive)
# 3) Or preset epoch range profile
# low = practical range
# wide = broader range
# full = broad/default range
# Set step multiplier (milliseconds per step)
# Run your application
Epoch offset precedence order:
SIMULATOR_EPOCH_OFFSETSIMULATOR_EPOCH_MIN+SIMULATOR_EPOCH_MAXSIMULATOR_EPOCH_RANGE_PROFILE- Default profile (
full)
Notes:
SIMULATOR_EPOCH_MINandSIMULATOR_EPOCH_MAXmust both be set together.- Bounds are inclusive and must satisfy
MIN <= MAX.
API Reference
Universal Functions
now()- ReturnsSystemTimefrom appropriate backendinstant_now()- ReturnsInstantfrom appropriate backend- When
simulatoris enabled, these top-level functions use the simulator backend; they use the standard backend only whensimulatoris disabled.
Chrono Functions (with chrono feature)
datetime_local_now()- Returnschrono::DateTime<chrono::Local>from appropriate backenddatetime_utc_now()- Returnschrono::DateTime<chrono::Utc>from appropriate backend
Standard Backend (std feature)
- Uses
std::time::SystemTime::now()andstd::time::Instant::now()directly
Simulator Backend (simulator feature)
now()- Returns simulated time based on current stepinstant_now()- Returns simulated instant based on current stepreset_step()- Reset step counter to 0next_step()- Advance to next step and return new step numberset_step(step)- Set specific step numbercurrent_step()- Get current step numberreset_epoch_offset()- Generate new random epoch offsetepoch_offset()- Get current epoch offsetreset_step_multiplier()- Generate new random step multiplierstep_multiplier()- Get current step multiplierwith_real_time(f)- Execute function with real system timedatetime_local_now()- Returns simulatedchrono::DateTime<chrono::Local>(requireschronofeature)datetime_utc_now()- Returns simulatedchrono::DateTime<chrono::Utc>(requireschronofeature)
Time Calculation
In simulator mode, time is calculated as:
time = UNIX_EPOCH + Duration::from_millis(epoch_offset + (step * step_multiplier))
- epoch_offset: Base time offset (fixed override, bounded random, profile random, or default profile random)
- step: Current step counter (controlled by your code)
- step_multiplier: Milliseconds per step (randomized or from environment)
Cargo Features
std- Enable standard system time backend (default)simulator- Enable time simulation backend (default)chrono- Enable chrono DateTime support
Use Cases
- Production: Use
stdfeature for normal time operations - Testing: Use
simulatorfeature for deterministic time testing - Development: Use
simulatorfeature to test time-dependent logic - Benchmarking: Control time advancement for consistent measurements
Thread Safety
Each thread maintains its own simulation state (step, epoch offset, step multiplier) using thread-local storage.