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.
// Get current timestamptimestamp = Time.now()println(timestamp) // 1704110400 (example Unix timestamp)// Use timestamp for calculationsstartTime = Time.now()// ... do some work ...endTime = Time.now()duration = endTime - startTimeprintln("Operation took " + duration + " seconds")
Pause execution for a specified number of milliseconds:
Copy
Ask AI
// Sleep for 1 second (1000 milliseconds)Time.sleep(1000)println("This prints after 1 second")// Sleep for half a secondTime.sleep(500)println("This prints after 500ms")// Sleep for 2.5 secondsTime.sleep(2500)println("This prints after 2.5 seconds")
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.
sec = Time.secondprintln(sec) // 1 (1 second)// Use as base unit for calculationsminutes = 5totalSeconds = minutes * 60 * Time.secondprintln(totalSeconds) // 300 (5 minutes in seconds)
day = Time.dayprintln(day) // 86400 (1 day in seconds)// Convert days to secondsdays = 7seconds = days * Time.dayprintln(seconds) // 604800 (1 week in seconds)
Returns the duration of one year in seconds (365 days):
Copy
Ask AI
year = Time.yearprintln(year) // 31536000 (365 days in seconds)// Convert years to secondsyears = 2seconds = years * Time.yearprintln(seconds) // 63072000 (2 years in seconds)
Use appropriate time units: Choose duration constants that match your use case
Handle sleep interruptions: Be aware that sleep operations pause execution
Consider time zones: Time.now() returns UTC timestamp
Use constants for readability: Duration constants make code more readable than magic numbers
Measure performance: Use timers to measure execution time of operations
Copy
Ask AI
// Good - use duration constantstimeout = 30 * Time.secondinterval = 5 * Time.minutedeadline = Time.now() + 7 * Time.day// Good - readable sleep durationsTime.sleep(2 * Time.second) // instead of Time.sleep(2000)Time.sleep(500 * Time.millisecond) // instead of Time.sleep(500)// Good - measure execution timestartTime = Time.now()performOperation()executionTime = Time.now() - startTimeprintln("Operation took " + executionTime + " seconds")// Good - convert to appropriate unitssoul formatDuration(seconds) { if (seconds < Time.minute) { return seconds.toFixed(2) + " seconds" } else if (seconds < Time.hour) { return (seconds / Time.minute).toFixed(2) + " minutes" } else if (seconds < Time.day) { return (seconds / Time.hour).toFixed(2) + " hours" } else { return (seconds / Time.day).toFixed(2) + " days" }}// Good - handle long operations with progresssoul longOperation(items) { total = Array.length(items) startTime = Time.now() for i, item in enumerate(items) { processItem(item) // Show progress every 10 items if (i % 10 == 0) { elapsed = Time.now() - startTime remaining = (elapsed / (i + 1)) * (total - i - 1) println("Progress: " + (i + 1) + "/" + total + " (ETA: " + formatDuration(remaining) + ")") } }}
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.