Files
marqdo/src/host/time.rs
T
cflmy 9331af78e2 release(2.8.0): 合入 CHANGELOG/README/发版说明/skills prove 任务;CI 链接指向 doc/releases
- CHANGELOG.md:v2.8.0 条目(中英双语);
- README.md:版本行 2.7.1→2.8.0 + 愿景第 6 条(知识可证明);
- doc/releases/v2.8.0.md:双语发版说明(亮点/用法/已知限制);
- skills/marqdo-dev/index.mq.md:新增 prove 任务(路径/目录模式/
  require_lean/json;参数用 + 行声明升参,check 与 narrative-ratio 通过);
- 设计文档 §12.5 标记已合入;发版链接统一指向 doc/releases/v2.8.0.md。
2026-11-18 10:29:26 +08:00

91 lines
2.6 KiB
Rust
Executable File

//! Time host primitives.
use crate::host::as_text;
use std::time::Duration;
#[cfg(not(target_arch = "wasm32"))]
use std::time::{SystemTime, UNIX_EPOCH};
use chrono::{DateTime, NaiveDateTime, Utc};
use crate::host::HostContext;
use crate::value::Value;
pub fn now_unix() -> Result<Value, String> {
#[cfg(target_arch = "wasm32")]
{
// No wall clock on wasm32-unknown-unknown without JS bridge (C4).
return Err("now_unix unavailable in browser wasm (no clock bridge yet)".into());
}
#[cfg(not(target_arch = "wasm32"))]
{
let secs = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_err(|e| format!("now_unix: {e}"))?
.as_secs();
Ok(Value::Int(secs as i64))
}
}
pub fn now_ms() -> Result<Value, String> {
#[cfg(target_arch = "wasm32")]
{
return Err("now_ms unavailable in browser wasm (no clock bridge yet)".into());
}
#[cfg(not(target_arch = "wasm32"))]
{
let ms = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_err(|e| format!("now_ms: {e}"))?
.as_millis();
Ok(Value::Int(ms as i64))
}
}
fn as_i64(v: &Value, label: &str) -> Result<i64, String> {
match v {
Value::Int(n) => Ok(*n),
_ => Err(format!("{label} must be int")),
}
}
pub fn format_time(unix: &Value, pattern: &Value) -> Result<Value, String> {
let secs = as_i64(unix, "unix")?;
let pat = as_text(pattern, "pattern")?;
let dt = DateTime::<Utc>::from_timestamp(secs, 0)
.ok_or_else(|| format!("invalid unix timestamp: {secs}"))?;
Ok(Value::Text(dt.format(pat).to_string()))
}
pub fn parse_time(text: &Value, pattern: &Value) -> Result<Value, String> {
let t = as_text(text, "text")?;
let pat = as_text(pattern, "pattern")?;
let dt = NaiveDateTime::parse_from_str(t, pat).map_err(|e| format!("parse_time: {e}"))?;
Ok(Value::Int(dt.and_utc().timestamp()))
}
pub fn sleep_ms(ctx: &HostContext, ms: &Value) -> Result<Value, String> {
let n = as_i64(ms, "ms")?;
if n < 0 {
return Err("sleep_ms must be >= 0".into());
}
let n = n as u64;
if let Some(lim) = ctx.sleep_limit_ms {
if n > lim {
return Err(format!("sleep_ms {n} exceeds limit {lim}"));
}
}
if n > 0 {
#[cfg(not(target_arch = "wasm32"))]
{
std::thread::sleep(Duration::from_millis(n));
}
#[cfg(target_arch = "wasm32")]
{
let _ = Duration::from_millis(n);
// Browser wasm has no blocking sleep; ignore (C0/C1).
}
}
Ok(Value::None)
}