system.time
Time and date operations, timing, and delays.
import system.time as time
Current Time
now()
Get current Unix timestamp in seconds.
let timestamp = time.now()
io.println("Current timestamp: " + timestamp)
nowMs()
Get current timestamp in milliseconds.
let ms = time.nowMs()
io.println("Milliseconds: " + ms)
nowNs()
Get current timestamp in nanoseconds (high precision).
let ns = time.nowNs()
io.println("Nanoseconds: " + ns)
Formatting
format(timestamp, pattern)
Format timestamp as string.
let now = time.now()
io.println(time.format(now, "%Y-%m-%d")) // "2024-01-15"
io.println(time.format(now, "%H:%M:%S")) // "14:30:45"
io.println(time.format(now, "%Y-%m-%d %H:%M")) // "2024-01-15 14:30"
io.println(time.format(now, "%A, %B %d")) // "Monday, January 15"
Format Specifiers
| Specifier | Description | Example |
|---|---|---|
%Y | Year (4 digits) | 2024 |
%y | Year (2 digits) | 24 |
%m | Month (01-12) | 01 |
%d | Day (01-31) | 15 |
%H | Hour 24h (00-23) | 14 |
%I | Hour 12h (01-12) | 02 |
%M | Minute (00-59) | 30 |
%S | Second (00-59) | 45 |
%p | AM/PM | PM |
%A | Weekday name | Monday |
%a | Weekday short | Mon |
%B | Month name | January |
%b | Month short | Jan |
%j | Day of year | 015 |
%W | Week number | 03 |
%Z | Timezone | UTC |
%% | Literal % | % |
Parsing
parse(string, pattern)
Parse string to timestamp.
let ts = time.parse("2024-01-15", "%Y-%m-%d")
io.println("Timestamp: " + ts)
let ts2 = time.parse("2024-01-15 14:30:00", "%Y-%m-%d %H:%M:%S")
Time Components
components(timestamp)
Get time components as dictionary.
let now = time.now()
let parts = time.components(now)
io.println("Year: " + parts["year"])
io.println("Month: " + parts["month"])
io.println("Day: " + parts["day"])
io.println("Hour: " + parts["hour"])
io.println("Minute: " + parts["minute"])
io.println("Second: " + parts["second"])
io.println("Weekday: " + parts["weekday"]) // 0=Sunday, 6=Saturday
io.println("Day of Year: " + parts["dayOfYear"])
Individual Component Functions
let now = time.now()
io.println(time.year(now)) // 2024
io.println(time.month(now)) // 1-12
io.println(time.day(now)) // 1-31
io.println(time.hour(now)) // 0-23
io.println(time.minute(now)) // 0-59
io.println(time.second(now)) // 0-59
io.println(time.weekday(now)) // 0-6 (Sun-Sat)
Delays
sleep(milliseconds)
Pause execution for specified milliseconds.
io.println("Starting...")
time.sleep(1000) // Wait 1 second
io.println("1 second later...")
time.sleep(500) // Wait 500ms
io.println("500ms later...")
sleepSec(seconds)
Pause execution for specified seconds.
time.sleepSec(2) // Wait 2 seconds
Performance Timing
Measure Execution Time
let start = time.nowMs()
// Code to measure
for (let i = 0; i < 1000000; i = i + 1) {
let x = i * 2
}
let elapsed = time.nowMs() - start
io.println("Elapsed: " + elapsed + " ms")
High-Precision Timing
let start = time.nowNs()
// Fast operation
let sum = 0
for (let i = 0; i < 1000; i = i + 1) {
sum = sum + i
}
let elapsed = time.nowNs() - start
io.println("Elapsed: " + elapsed + " ns")
io.println("Elapsed: " + (elapsed / 1000) + " µs")
Time Arithmetic
Adding/Subtracting Time
let now = time.now()
// Add time (in seconds)
let tomorrow = now + (24 * 60 * 60) // +1 day
let nextWeek = now + (7 * 24 * 60 * 60) // +7 days
let oneHourAgo = now - (60 * 60) // -1 hour
io.println("Now: " + time.format(now, "%Y-%m-%d %H:%M"))
io.println("Tomorrow: " + time.format(tomorrow, "%Y-%m-%d %H:%M"))
io.println("Next week: " + time.format(nextWeek, "%Y-%m-%d %H:%M"))
Time Constants
let SECOND = 1
let MINUTE = 60
let HOUR = 60 * 60
let DAY = 24 * 60 * 60
let WEEK = 7 * 24 * 60 * 60
let now = time.now()
let inTwoHours = now + (2 * HOUR)
let inThreeDays = now + (3 * DAY)
Time Comparison
let start = time.parse("2024-01-01", "%Y-%m-%d")
let end = time.parse("2024-12-31", "%Y-%m-%d")
let now = time.now()
if (now >= start && now <= end) {
io.println("We're in 2024!")
}
// Days between dates
let diff = end - start
let days = diff / (24 * 60 * 60)
io.println("Days in 2024: " + days)
Examples
Stopwatch Class
import system.time as time
import system.io as io
class Stopwatch {
constructor() {
self.startTime = 0
self.running = false
}
fn start() {
self.startTime = time.nowMs()
self.running = true
}
fn stop() {
self.running = false
return self.elapsed()
}
fn elapsed() {
if (self.running) {
return time.nowMs() - self.startTime
}
return 0
}
fn elapsedFormatted() {
let ms = self.elapsed()
let secs = ms / 1000
let mins = secs / 60
return mins + "m " + (secs % 60) + "s"
}
}
let sw = Stopwatch()
sw.start()
time.sleep(2500)
io.println("Elapsed: " + sw.elapsedFormatted())
Countdown Timer
import system.time as time
import system.io as io
fn countdown(seconds) {
for (let i = seconds; i > 0; i = i - 1) {
io.print("\rTime remaining: " + i + " seconds ")
io.flush()
time.sleepSec(1)
}
io.println("\rTime's up! ")
}
countdown(10)
Log with Timestamp
import system.time as time
import system.io as io
fn log(level, message) {
let timestamp = time.format(time.now(), "%Y-%m-%d %H:%M:%S")
io.println("[" + timestamp + "] [" + level + "] " + message)
}
log("INFO", "Application started")
log("DEBUG", "Loading configuration")
log("ERROR", "Connection failed")
Rate Limiter
import system.time as time
class RateLimiter {
constructor(maxRequests, windowMs) {
self.maxRequests = maxRequests
self.windowMs = windowMs
self.requests = []
}
fn allow() {
let now = time.nowMs()
let windowStart = now - self.windowMs
// Remove old requests
let newRequests = []
for (let i = 0; i < len(self.requests); i = i + 1) {
if (self.requests[i] > windowStart) {
newRequests[len(newRequests)] = self.requests[i]
}
}
self.requests = newRequests
if (len(self.requests) < self.maxRequests) {
self.requests[len(self.requests)] = now
return true
}
return false
}
}
// Allow 5 requests per second
let limiter = RateLimiter(5, 1000)
for (let i = 0; i < 10; i = i + 1) {
if (limiter.allow()) {
io.println("Request " + i + " allowed")
} else {
io.println("Request " + i + " rate limited")
time.sleep(200)
}
}
💡 Tip
For benchmarking, use nowNs() for nanosecond precision. Run benchmarks multiple times and average the results for accuracy.
See Also: Standard Library Overview →