Skip to main content

Time

The Time module provides essential time manipulation capabilities for Soul, including sleep functionality, current timestamp retrieval, and comprehensive duration constants for time calculations. It follows a JavaScript-style API for familiar usage patterns.

Time Operations

now - Get current timestamp

Get the current Unix timestamp in seconds:

sleep - Pause execution

Pause execution for a specified number of milliseconds:

Duration Constants

The Time module provides duration constants that return the number of seconds for each time unit. These constants are useful for time calculations and conversions.

nanosecond - Nanosecond duration

Returns the duration of one nanosecond in seconds:

microsecond - Microsecond duration

Returns the duration of one microsecond in seconds:

millisecond - Millisecond duration

Returns the duration of one millisecond in seconds:

second - Second duration

Returns the duration of one second:

minute - Minute duration

Returns the duration of one minute in seconds:

hour - Hour duration

Returns the duration of one hour in seconds:

day - Day duration

Returns the duration of one day in seconds:

week - Week duration

Returns the duration of one week in seconds:

month - Month duration

Returns the duration of one month in seconds (30 days):

year - Year duration

Returns the duration of one year in seconds (365 days):

Time Calculations

Duration Conversions

Convert between different time units:

Time Arithmetic

Perform arithmetic operations with time values:

Practical Examples

Timer Implementation

Retry Mechanism with Exponential Backoff

Rate Limiting

Periodic Task Scheduler

Time-based Cache

Benchmark Utility

Best Practices

  1. Use appropriate time units: Choose duration constants that match your use case
  2. Handle sleep interruptions: Be aware that sleep operations pause execution
  3. Consider time zones: Time.now() returns UTC timestamp
  4. Use constants for readability: Duration constants make code more readable than magic numbers
  5. Measure performance: Use timers to measure execution time of operations
The Time module provides essential timing capabilities for Soul applications, enabling precise time management, performance measurement, and time-based operations with a comprehensive set of duration constants for all common time units.