Date & Time
The time module provides utilities for working with timestamps and date formatting.
Required Imports
Section titled “Required Imports”#[allow(warnings)]mod bindings;
use bindings::exports::mik::core::handler::{self, Guest, Response};use mik_sdk::prelude::*;The time module is included in mik_sdk::prelude::*.
Platform Support
Section titled “Platform Support”The time module automatically uses the appropriate implementation:
| Platform | Implementation |
|---|---|
| WASM (wasm32-wasip2) | Native wasi:clocks/wall-clock |
| Native (testing) | std::time::SystemTime |
No configuration needed - just import and use.
Getting Current Time
Section titled “Getting Current Time”Unix Timestamp (Seconds)
Section titled “Unix Timestamp (Seconds)”use mik_sdk::time;
let timestamp = time::now(); // u64// Example: 1737024600Unix Timestamp (Milliseconds)
Section titled “Unix Timestamp (Milliseconds)”Useful for JavaScript interop where Date.now() uses milliseconds:
let timestamp_ms = time::now_millis(); // u64// Example: 1737024600500ISO 8601 String
Section titled “ISO 8601 String”let iso = time::now_iso(); // String// Example: "2025-01-16T10:50:00Z"
// With milliseconds (if non-zero)// Example: "2025-01-16T10:50:00.500Z"Converting Raw Values
Section titled “Converting Raw Values”If you have raw seconds and nanoseconds:
// To millisecondslet ms = time::to_millis(seconds, nanoseconds);
// To ISO 8601 stringlet iso = time::to_iso(seconds, nanoseconds);
// Examplestime::to_millis(1737024600, 500_000_000); // 1737024600500time::to_iso(1737024600, 0); // "2025-01-16T10:50:00Z"time::to_iso(1737024600, 500_000_000); // "2025-01-16T10:50:00.500Z"Common Patterns
Section titled “Common Patterns”Timestamp in JSON Response
Section titled “Timestamp in JSON Response”fn get_status(_req: &Request) -> Response { ok!({ "status": "ok", "timestamp": time::now(), "timestamp_iso": time::now_iso() })}Record Creation Time
Section titled “Record Creation Time”fn create_item(body: CreateInput, _req: &Request) -> Response { let id = random::uuid(); let created_at = time::now_iso();
let (sql, params) = sql_create!(items { id: &id, name: &body.name, created_at: &created_at, });
created!(format!("/items/{}", id), { "id": id, "created_at": created_at })}Update Timestamps
Section titled “Update Timestamps”fn update_item(path: Id, body: UpdateInput, _req: &Request) -> Response { let (sql, params) = sql_update!(items { set: { name: &body.name, updated_at: time::now_iso(), }, filter: { id: path.as_str() }, });
ok!({ "updated": true })}Cursor Pagination with Timestamps
Section titled “Cursor Pagination with Timestamps”use mik_sdk::query::Cursor;
fn list_items(query: ListQuery, _req: &Request) -> Response { let (sql, params) = sql_read!(items { select: [id, name, created_at], order: [-created_at, -id], after: query.cursor.as_deref(), limit: 20, });
// After fetching results, build next cursor // let last = items.last(); // let next_cursor = Cursor::new() // .string("created_at", &last.created_at) // .int("id", last.id) // .encode();
ok!({ "sql": sql })}Logging with Timestamps
Section titled “Logging with Timestamps”The logging module automatically includes ISO 8601 timestamps:
log!(info, "operation completed", duration_ms: elapsed);// Output: {"level":"info","msg":"operation completed","duration_ms":"123","ts":"2025-01-16T10:50:00.500Z"}ISO 8601 Format
Section titled “ISO 8601 Format”The now_iso() and to_iso() functions return UTC timestamps in ISO 8601 format:
YYYY-MM-DDTHH:MM:SSZ // Without millisecondsYYYY-MM-DDTHH:MM:SS.sssZ // With millisecondsExamples:
2025-01-16T10:50:00Z2025-01-16T10:50:00.500Z2024-02-29T12:00:00Z(leap year)
Algorithm
Section titled “Algorithm”ISO 8601 formatting uses Howard Hinnant’s date algorithm, which is:
- Well-tested and widely used
- Handles leap years correctly
- Efficient for embedded use
API Summary
Section titled “API Summary”| Function | Returns | Description |
|---|---|---|
time::now() | u64 | Unix timestamp in seconds |
time::now_millis() | u64 | Unix timestamp in milliseconds |
time::now_iso() | String | Current time as ISO 8601 |
time::to_millis(secs, nanos) | u64 | Convert to milliseconds |
time::to_iso(secs, nanos) | String | Convert to ISO 8601 |
Examples
Section titled “Examples”use mik_sdk::time;
// Get current time in various formatslet secs = time::now(); // 1737024600let ms = time::now_millis(); // 1737024600500let iso = time::now_iso(); // "2025-01-16T10:50:00.500Z"
// Convert known valueslet epoch_iso = time::to_iso(0, 0); // "1970-01-01T00:00:00Z"let y2k = time::to_iso(946684800, 0); // "2000-01-01T00:00:00Z"
// Use in responsesok!({ "created_at": time::now_iso(), "expires_at": time::to_iso(time::now() + 3600, 0) // +1 hour})