datetime
ModuleDate 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:
| PHP | Rust |
|---|---|
new DateTime($s, $tz) / DateTimeImmutable | DateTime::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 < $b | PartialOrd / 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;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;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.
PartialEq, PartialOrd.use utopia_php_std::datetime::Error;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).
Display.use utopia_php_std::datetime::Interval;pub struct Interval(_);A DateInterval made by DateInterval::createFromDateString().
use utopia_php_std::datetime::Message;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;pub struct Messages {
pub warnings: Vec<Message>,
pub errors: Vec<Message>,
}timelib_error_container.
use utopia_php_std::datetime::Modifier;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.
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;pub struct ParsedDate {
// private fields
}What date_parse() returns.
use utopia_php_std::datetime::Timestamp;pub struct Timestamp {
pub sec: i64,
pub usec: i32,
}A point in time, as gettimeofday reports it.
use utopia_php_std::datetime::TzInfo;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.
Debug, PartialEq.use utopia_php_std::datetime::ErrorKind;pub enum ErrorKind {
MalformedString,
InvalidTimeZone,
MalformedIntervalString,
Value,
}The PHP exception (or error) a date/time operation raises.
use utopia_php_std::datetime::TimeZone;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;pub fn abbreviation_list() -> impl Iterator<Item = Abbreviation>The rows of timezonemap.h (DateTimeZone::listAbbreviations() data).
use utopia_php_std::datetime::date;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;pub fn date_parse(datetime: &[u8]) -> ParsedDatedate_parse($datetime).
use utopia_php_std::datetime::gmdate;pub fn gmdate(format: &[u8], timestamp: i64) -> Vec<u8>gmdate($format, $timestamp).
use utopia_php_std::datetime::identifiers;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;pub fn is_valid_id(name: &str) -> booltimelib_timezone_id_is_valid.
use utopia_php_std::datetime::microtime;pub fn microtime() -> Stringmicrotime(): "0.12345600 1700000000" (%.8F %ld of the fraction and the seconds).
use utopia_php_std::datetime::microtime_float;pub fn microtime_float() -> f64microtime(true): seconds since the epoch as a float.
use utopia_php_std::datetime::strtotime;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;pub fn time() -> i64time().
use utopia_php_std::datetime::version;pub fn version() -> &'static strtimezone_version_get(): the version of the bundled database.