Rust

datetime

Module

Date and time: date()/DateTime::format, DateTime parsing and timezones, strtotime, microtime.

This is a port of timelib (the library inside ext/date) and of the parts of php_date.c around it, so that dates parse, normalise, convert between zones and format exactly as PHP 8.5 does, quirks included:

PHPRust
new DateTime($s, $tz) / DateTimeImmutableDateTime::parse (explicit "now"), DateTime::new (system clock)
DateTime::createFromFormat($f, $s, $tz)DateTime::from_format
->format($f), date($f, $ts), gmdate($f, $ts)DateTime::format, date, gmdate
->modify($s)DateTime::modify (DateTime), DateTime::modified (DateTimeImmutable)
->setDate($y, $m, $d)DateTime::set_date (DateTime), DateTime::with_date (DateTimeImmutable)
->setTime($h, $i, $s, $us)DateTime::set_time (DateTime), DateTime::with_time (DateTimeImmutable)
->setTimezone($tz), ->getTimezone()DateTime::set_timezone, DateTime::timezone
->getTimestamp(), ->getOffset(), ->getMicrosecond()DateTime::timestamp, DateTime::offset, DateTime::microsecond
->add(DateInterval::createFromDateString($s))DateTime::add, Interval::from_date_string
$a < $bPartialOrd / Ord on DateTime
new DateTimeZone($name), ->getName(), ->getOffset($dt)TimeZone::new, TimeZone::name, TimeZone::offset_at
strtotime($s, $base)strtotime
date_parse($s)date_parse (ParsedDate::to_php)
time(), microtime(true), microtime()time, microtime_float, microtime
timezone_version_get(), timezone_identifiers_list()version, identifiers

PHP keeps a default timezone in a global (date.timezone, UTC in Appwrite). Here it is an argument: functions that use it take a default TzInfo (TzInfo::utc for Appwrite).

Timezone data is PHP's own bundled database (version version), not the system's, so results do not depend on the host.

Not modelled: DatePeriod, DateTime::diff(), DateTime::sub(), intervals created from ISO 8601 durations, setISODate, idate, mktime, strftime, getdate, sunrise/sunset functions; date_default_timezone_set (pass the zone instead).

Contents

use utopia_php_std::datetime::Abbreviation;
Rust
pub struct Abbreviation {
    pub name: &'static str,
    pub dst: bool,
    pub gmtoffset: i32,
    pub full_tz_name: Option<&'static str>,
}

A row of timelib's abbreviation tables.

use utopia_php_std::datetime::DateTime;
Rust
pub struct DateTime {
    // private fields
}

A DateTime (or DateTimeImmutable): a timelib time with its zone.

== and < compare instants, as PHP's do (timelib_time_compare): the same moment in two zones is equal.

Also implements PartialEq, PartialOrd.
use utopia_php_std::datetime::Error;
Rust
pub struct Error {
    // private fields
}

Why a date/time operation failed: Error::php_class names the PHP exception, Error::message_bytes is its message byte for byte (PHP quotes the input verbatim, which need not be UTF-8).

Also implements Display.
use utopia_php_std::datetime::Interval;
Rust
pub struct Interval(_);

A DateInterval made by DateInterval::createFromDateString().

use utopia_php_std::datetime::Message;
Rust
pub struct Message {
    pub position: i64,
    pub character: u8,
    pub message: &'static str,
}

One error or warning: where (byte offset in the trimmed input), the byte there, and the message.

use utopia_php_std::datetime::Messages;
Rust
pub struct Messages {
    pub warnings: Vec<Message>,
    pub errors: Vec<Message>,
}

timelib_error_container.

use utopia_php_std::datetime::Modifier;
Rust
pub struct Modifier(_);

A modifier string parsed once, to apply to many dates: what DateTimeImmutable::modify($modifier) does, without parsing the string again each time.

Rust
use php_std::datetime::{DateTime, Modifier, Timestamp, TzInfo};

let next_day = Modifier::parse(b"+1 day")?;
let at = DateTime::parse(b"2026-03-28 12:00", Some(&php_std::datetime::TimeZone::new("Europe/Paris")?), &TzInfo::utc(), Timestamp { sec: 0, usec: 0 })?;
assert_eq!(at.modified_by(&next_day).format(b"Y-m-d H:i T"), b"2026-03-29 12:00 CEST");
assert_eq!(at.modified_by(&next_day), at.modified(b"+1 day")?);
use utopia_php_std::datetime::ParsedDate;
Rust
pub struct ParsedDate {
    // private fields
}

What date_parse() returns.

use utopia_php_std::datetime::Timestamp;
Rust
pub struct Timestamp {
    pub sec: i64,
    pub usec: i32,
}

A point in time, as gettimeofday reports it.

use utopia_php_std::datetime::TzInfo;
Rust
pub struct TzInfo {
    // private fields
}

A timezone identifier resolved against PHP's database.

The name is kept as it was requested (europe/paris stays lowercase), as timelib does.

Also implements Debug, PartialEq.
use utopia_php_std::datetime::ErrorKind;
Rust
pub enum ErrorKind {
    MalformedString,
    InvalidTimeZone,
    MalformedIntervalString,
    Value,
}

The PHP exception (or error) a date/time operation raises.

use utopia_php_std::datetime::TimeZone;
Rust
pub enum TimeZone {
    Offset(i64),
    Abbr {
        abbr: String,
        utc_offset: i64,
        dst: bool,
    },
    Id(TzInfo),
}

A timezone, as DateTimeZone holds it.

use utopia_php_std::datetime::abbreviation_list;
Rust
pub fn abbreviation_list() -> impl Iterator<Item = Abbreviation>

The rows of timezonemap.h (DateTimeZone::listAbbreviations() data).

use utopia_php_std::datetime::date;
Rust
pub fn date(format: &[u8], timestamp: i64, default: &TzInfo) -> Vec<u8>

date($format, $timestamp) in the default zone.

use utopia_php_std::datetime::date_parse;
Rust
pub fn date_parse(datetime: &[u8]) -> ParsedDate

date_parse($datetime).

use utopia_php_std::datetime::gmdate;
Rust
pub fn gmdate(format: &[u8], timestamp: i64) -> Vec<u8>

gmdate($format, $timestamp).

use utopia_php_std::datetime::identifiers;
Rust
pub fn identifiers() -> impl Iterator<Item = &'static str>

timezone_identifiers_list() order: every identifier PHP knows, aliases included.

use utopia_php_std::datetime::is_valid_id;
Rust
pub fn is_valid_id(name: &str) -> bool

timelib_timezone_id_is_valid.

use utopia_php_std::datetime::microtime;
Rust
pub fn microtime() -> String

microtime(): "0.12345600 1700000000" (%.8F %ld of the fraction and the seconds).

use utopia_php_std::datetime::microtime_float;
Rust
pub fn microtime_float() -> f64

microtime(true): seconds since the epoch as a float.

use utopia_php_std::datetime::strtotime;
Rust
pub fn strtotime(datetime: &[u8], base: i64, default: &TzInfo) -> Option<i64>

strtotime($datetime, $baseTimestamp) in the default zone: None where PHP returns false.

use utopia_php_std::datetime::time;
Rust
pub fn time() -> i64

time().

use utopia_php_std::datetime::version;
Rust
pub fn version() -> &'static str

timezone_version_get(): the version of the bundled database.