use async_trait::async_trait;
use chrono::{Local, Utc};
use crate::ast::Value;
use crate::interpreter::{ExecResult, OutputData};
use crate::tools::{ExecContext, ParamSchema, Tool, ToolArgs, ToolSchema};
pub struct Date;
#[async_trait]
impl Tool for Date {
fn name(&self) -> &str {
"date"
}
fn schema(&self) -> ToolSchema {
ToolSchema::new("date", "Display current date and time")
.param(ParamSchema::optional(
"format",
"string",
Value::String("%Y-%m-%d %H:%M:%S".into()),
"strftime format string",
))
.param(ParamSchema::optional(
"utc",
"bool",
Value::Bool(false),
"Use UTC instead of local time (-u)",
))
.param(ParamSchema::optional(
"iso",
"bool",
Value::Bool(false),
"Output in ISO 8601 format",
))
.param(ParamSchema::optional(
"unix",
"bool",
Value::Bool(false),
"Output Unix timestamp (seconds since epoch)",
))
.example("Current date and time", "date")
.example("ISO 8601 format", "date --iso")
.example("Unix timestamp", "date --unix")
}
async fn execute(&self, args: ToolArgs, _ctx: &mut ExecContext) -> ExecResult {
let use_utc = args.has_flag("utc") || args.has_flag("u");
let use_iso = args.has_flag("iso");
let use_unix = args.has_flag("unix");
let output = if use_unix {
if use_utc {
Utc::now().timestamp().to_string()
} else {
Local::now().timestamp().to_string()
}
} else if use_iso {
if use_utc {
Utc::now().to_rfc3339()
} else {
Local::now().to_rfc3339()
}
} else {
let format = args
.get_string("format", 0)
.map(|s| s.strip_prefix('+').unwrap_or(&s).to_string())
.unwrap_or_else(|| "%Y-%m-%d %H:%M:%S".to_string());
if use_utc {
Utc::now().format(&format).to_string()
} else {
Local::now().format(&format).to_string()
}
};
ExecResult::with_output(OutputData::text(format!("{}\n", output)))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::vfs::{MemoryFs, VfsRouter};
use std::sync::Arc;
fn make_ctx() -> ExecContext {
let mut vfs = VfsRouter::new();
vfs.mount("/", MemoryFs::new());
ExecContext::new(Arc::new(vfs))
}
#[tokio::test]
async fn test_date_default() {
let mut ctx = make_ctx();
let result = Date.execute(ToolArgs::new(), &mut ctx).await;
assert!(result.ok());
assert!(result.text_out().contains('-')); assert!(result.text_out().contains(':')); }
#[tokio::test]
async fn test_date_custom_format() {
let mut ctx = make_ctx();
let mut args = ToolArgs::new();
args.positional.push(Value::String("%Y".into()));
let result = Date.execute(args, &mut ctx).await;
assert!(result.ok());
let out = result.text_out();
let year = out.trim();
assert_eq!(year.len(), 4);
assert!(year.chars().all(|c| c.is_ascii_digit()));
}
#[tokio::test]
async fn test_date_iso() {
let mut ctx = make_ctx();
let mut args = ToolArgs::new();
args.flags.insert("iso".to_string());
let result = Date.execute(args, &mut ctx).await;
assert!(result.ok());
assert!(result.text_out().contains('T'));
}
#[tokio::test]
async fn test_date_unix() {
let mut ctx = make_ctx();
let mut args = ToolArgs::new();
args.flags.insert("unix".to_string());
let result = Date.execute(args, &mut ctx).await;
assert!(result.ok());
let out = result.text_out();
let timestamp = out.trim();
assert!(timestamp.chars().all(|c| c.is_ascii_digit()));
let ts: i64 = timestamp.parse().unwrap_or(0);
assert!(ts > 946684800); }
#[tokio::test]
async fn test_date_utc_flag() {
let mut ctx = make_ctx();
let mut args = ToolArgs::new();
args.flags.insert("u".to_string());
args.flags.insert("iso".to_string());
let result = Date.execute(args, &mut ctx).await;
assert!(result.ok());
assert!(result.text_out().contains("+00:00") || result.text_out().contains('Z'));
}
#[tokio::test]
async fn test_date_utc_long_flag() {
let mut ctx = make_ctx();
let mut args = ToolArgs::new();
args.flags.insert("utc".to_string());
args.flags.insert("iso".to_string());
let result = Date.execute(args, &mut ctx).await;
assert!(result.ok());
assert!(result.text_out().contains("+00:00") || result.text_out().contains('Z'));
}
}