php-dates 0.2.0

PHP date() patterns for Rust: strict createFromFormat-style parsing, a pattern resolution contract and a Day.js export, with no framework dependency
Documentation
//! Every expectation here is what PHP itself answers (`tests/fixtures/generate.php`).

use jiff::civil::DateTime;
use php_dates::{PhpDatePattern, PhpDateTime, Zone};
use serde::Deserialize;

#[derive(Deserialize)]
struct Fixture {
    format: Vec<FormatCase>,
    parse: Vec<ParseCase>,
}

#[derive(Deserialize)]
struct FormatCase {
    moment: String,
    zone: String,
    pattern: String,
    expected: String,
}

#[derive(Deserialize)]
struct ParseCase {
    pattern: String,
    value: String,
    now: String,
    zone: String,
    expected: Option<String>,
    micro: bool,
}

fn fixture() -> Fixture {
    serde_json::from_str(include_str!("fixtures/php_oracle.json")).unwrap()
}

fn naive(text: &str) -> DateTime {
    DateTime::strptime("%Y-%m-%d %H:%M:%S%.f", text)
        .or_else(|_| DateTime::strptime("%Y-%m-%d %H:%M:%S", text))
        .unwrap_or_else(|error| panic!("{text}: {error}"))
}

fn report(kind: &str, total: usize, failures: &[String]) {
    assert!(
        failures.is_empty(),
        "{} of {total} {kind} cases differ from PHP:\n{}",
        failures.len(),
        failures.iter().take(60).cloned().collect::<Vec<_>>().join("\n")
    );
}

#[test]
fn format_matches_php_for_every_token() {
    let fixture = fixture();
    let mut failures = vec![];
    for case in &fixture.format {
        let zone = Zone::parse(&case.zone).unwrap_or_else(|| panic!("zone {}", case.zone));
        let date = PhpDateTime::new(naive(&case.moment), zone).unwrap();
        let actual = PhpDatePattern::compile(&case.pattern).unwrap().format(date);
        if actual != case.expected {
            failures.push(format!(
                "format {:?} of {} in {}: PHP {:?}, here {actual:?}",
                case.pattern, case.moment, case.zone, case.expected
            ));
        }
    }
    report("format", fixture.format.len(), &failures);
}

#[test]
fn parse_matches_php_create_from_format() {
    let fixture = fixture();
    let mut failures = vec![];
    for case in &fixture.parse {
        let now = PhpDateTime::new(naive(&case.now), Zone::parse(&case.zone).unwrap()).unwrap();
        let pattern = PhpDatePattern::compile(&case.pattern).unwrap();
        let shown = PhpDatePattern::compile(if case.micro { "Y-m-d H:i:s.u|e|P" } else { "Y-m-d H:i:s|e|P" }).unwrap();
        let actual = pattern.parse_at(&case.value, &now).ok().map(|date| shown.format(date));
        if actual != case.expected {
            failures.push(format!(
                "parse {:?} with {:?} (now {} in {}): PHP {:?}, here {actual:?}",
                case.pattern, case.value, case.now, case.zone, case.expected
            ));
        }
    }
    report("parse", fixture.parse.len(), &failures);
}