Skip to content

Current Time with ard/chrono

The ard/chrono module provides access to the current Unix timestamp (seconds since 1970-01-01 UTC).

The chrono module provides:

  • Current time as integer timestamp for database operations and calculations
use ard/chrono
fn main() {
let now = chrono::now()
io::print("Current Unix timestamp: {now}")
}

Get the current Unix timestamp in seconds.

Returns an integer representing seconds since 1970-01-01 UTC.

use ard/chrono
let now = chrono::now()
io::print("Current timestamp: {now}")
use ard/chrono
use ard/sql
fn cache_set(db: sql::Database, key: Str, value: Dynamic, ttl_seconds: Int) {
let now = chrono::now()
let expires_at = now + ttl_seconds
db.query("INSERT INTO cache (key, value, expires_at) VALUES (?, ?, ?)")
.run([key, json::encode(value), expires_at])
}
use ard/chrono
fn is_expired(expires_at: Int) Bool {
let now = chrono::now()
expires_at <= now
}
use ard/chrono
let now = chrono::now()
let one_hour_ago = now - 3600
let one_hour_later = now + 3600
io::print("Past: {one_hour_ago}")
io::print("Now: {now}")
io::print("Future: {one_hour_later}")
  • Cache TTL Management - Calculate expiration times and check if cached data is stale
  • Database Timestamps - Store current time in INTEGER columns
  • Rate Limiting - Track request windows by timestamp
  • Event Logging - Record when events occurred
  • Time Comparisons - Check if deadlines have passed

chrono::now() returns an Int - the Unix timestamp in seconds.

use ard/chrono
let now = chrono::now()
// now is an integer, use it directly
let future = now + 3600
  • Returns seconds (integer) since Unix epoch (1970-01-01 UTC)
  • No fractional seconds or nanoseconds
  • Sufficient for cache TTLs, rate limiting, and most timestamp needs
  • Always returns UTC (Unix timestamps are timezone-agnostic)
  • For human-readable local time with timezone, use ard/dates
use ard/chrono
use ard/duration
let now = chrono::now()
// Add 1 hour (duration returns nanoseconds, divide to get seconds)
let in_one_hour = now + duration::from_hours(1) / 1000000000
use ard/chrono
use ard/dates
let human_date = dates::get_today() // "2025-01-17"
let now = chrono::now()
io::print("{human_date} [{now}]")

chrono::now() returns an Int directly—no error handling needed. System clock failures are extremely rare and not worth defensive programming.