Add DateTime type and operations

This commit introduces the `DateTime` type to the language, enabling
users to work with dates and times. It includes:

- A new `DateTime` variant in the `Value` and `StaticType` enums.
- A `date` function for parsing date strings into `DateTime` values.
- Overloads for `+` and `-` operators to support `DateTime` arithmetic.
- Comparison operators (`>`, `<`) for `DateTime` values.
- Corresponding unit tests for `DateTime` operations.
- Updates to the `gemini.md` documentation to reflect the new
  functionality.
  Add DateTime type and operations

Introduce a new `DateTime` type to the language, allowing for date and
time manipulation. This includes:

*   Parsing dates and datetimes from strings.
*   Performing arithmetic operations (addition and subtraction) between
    `DateTime` and `Int` (representing milliseconds) and between two
    `DateTime` values (resulting in an `Int` duration).
*   Enabling comparison operations (`>`, `<`) between `DateTime` values.
This commit is contained in:
Michael Schimmel
2026-02-19 13:27:44 +01:00
parent 49db73800a
commit a5957f524b
4 changed files with 54 additions and 2 deletions
+11
View File
@@ -5,6 +5,7 @@ use std::fmt;
use std::sync::OnceLock;
use std::sync::Mutex; // Still needed for global keyword registry
use std::any::Any;
use chrono::{TimeZone, Utc};
/// Simple source location
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -61,6 +62,7 @@ pub enum Value {
Bool(bool),
Int(i64),
Float(f64),
DateTime(i64),
Text(Rc<str>),
Keyword(Keyword),
List(Rc<Vec<Value>>),
@@ -84,6 +86,7 @@ pub enum StaticType {
Bool,
Int,
Float,
DateTime,
Text,
Keyword,
List(Box<StaticType>),
@@ -101,6 +104,7 @@ impl fmt::Display for StaticType {
StaticType::Bool => write!(f, "bool"),
StaticType::Int => write!(f, "int"),
StaticType::Float => write!(f, "float"),
StaticType::DateTime => write!(f, "datetime"),
StaticType::Text => write!(f, "text"),
StaticType::Keyword => write!(f, "keyword"),
StaticType::List(inner) => write!(f, "[{}]", inner),
@@ -182,6 +186,7 @@ impl Value {
Value::Bool(_) => StaticType::Bool,
Value::Int(_) => StaticType::Int,
Value::Float(_) => StaticType::Float,
Value::DateTime(_) => StaticType::DateTime,
Value::Text(_) => StaticType::Text,
Value::Keyword(_) => StaticType::Keyword,
Value::List(_) => StaticType::List(Box::new(StaticType::Any)),
@@ -201,6 +206,12 @@ impl fmt::Display for Value {
Value::Bool(b) => write!(f, "{}", b),
Value::Int(i) => write!(f, "{}", i),
Value::Float(fl) => write!(f, "{}", fl),
Value::DateTime(ts) => {
match Utc.timestamp_millis_opt(*ts) {
chrono::LocalResult::Single(dt) => write!(f, "#{}#", dt.format("%Y-%m-%d %H:%M:%S")),
_ => write!(f, "#timestamp({})#", ts),
}
},
Value::Text(t) => write!(f, "\"{}\"", t),
Value::Keyword(k) => write!(f, ":{}", k.name()),
Value::List(l) => {