use chrono::{DateTime, Datelike, NaiveDate, NaiveDateTime, Utc};
pub fn now() -> DateTime<Utc> {
Utc::now()
}
pub fn strtotime(s: &str) -> DateTime<Utc> {
if let Ok(dt) = DateTime::parse_from_rfc3339(s) {
return dt.with_timezone(&Utc);
}
if let Ok(naive) = NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M:%S") {
return DateTime::from_naive_utc_and_offset(naive, Utc);
}
if let Ok(date) = NaiveDate::parse_from_str(s, "%Y-%m-%d") {
if let Some(naive) = date.and_hms_opt(0, 0, 0) {
return DateTime::from_naive_utc_and_offset(naive, Utc);
}
}
now()
}
pub fn format(when: DateTime<Utc>, php_format: &str) -> String {
let mut out = String::with_capacity(php_format.len());
for ch in php_format.chars() {
match ch {
'Y' => out.push_str(&when.format("%Y").to_string()),
'y' => out.push_str(&when.format("%y").to_string()),
'm' => out.push_str(&when.format("%m").to_string()),
'n' => out.push_str(&when.format("%-m").to_string()),
'd' => out.push_str(&when.format("%d").to_string()),
'j' => out.push_str(&when.format("%-d").to_string()),
'F' => out.push_str(&when.format("%B").to_string()),
'M' => out.push_str(&when.format("%b").to_string()),
'l' => out.push_str(&when.format("%A").to_string()),
'D' => out.push_str(&when.format("%a").to_string()),
'H' => out.push_str(&when.format("%H").to_string()),
'G' => out.push_str(&when.format("%-H").to_string()),
'h' => out.push_str(&when.format("%I").to_string()),
'g' => out.push_str(&when.format("%-I").to_string()),
'i' => out.push_str(&when.format("%M").to_string()),
's' => out.push_str(&when.format("%S").to_string()),
'A' => out.push_str(&when.format("%p").to_string()),
'a' => out.push_str(&when.format("%P").to_string()),
'N' => out.push_str(&when.format("%u").to_string()),
'w' => out.push_str(&when.format("%w").to_string()),
'S' => out.push_str(ordinal_suffix(when.day())),
other => out.push(other),
}
}
out
}
fn ordinal_suffix(day: u32) -> &'static str {
match day % 100 {
11..=13 => "th",
_ => match day % 10 {
1 => "st",
2 => "nd",
3 => "rd",
_ => "th",
},
}
}
#[cfg(test)]
mod tests {
use super::*;
use chrono::TimeZone;
fn sample(year: i32, month: u32, day: u32) -> DateTime<Utc> {
Utc.with_ymd_and_hms(year, month, day, 14, 5, 9).unwrap()
}
#[test]
fn formats_year_month_day_with_a_literal_separator() {
assert_eq!(format(sample(2026, 8, 17), "Y-m-d"), "2026-08-17");
}
#[test]
fn formats_full_month_name_and_ordinal_day() {
assert_eq!(format(sample(2026, 8, 1), "F jS, Y"), "August 1st, 2026");
assert_eq!(format(sample(2026, 8, 2), "F jS, Y"), "August 2nd, 2026");
assert_eq!(format(sample(2026, 8, 3), "F jS, Y"), "August 3rd, 2026");
assert_eq!(format(sample(2026, 8, 4), "F jS, Y"), "August 4th, 2026");
assert_eq!(format(sample(2026, 8, 11), "F jS, Y"), "August 11th, 2026");
assert_eq!(format(sample(2026, 8, 21), "F jS, Y"), "August 21st, 2026");
}
#[test]
fn formats_time_components() {
assert_eq!(format(sample(2026, 8, 17), "H:i:s"), "14:05:09");
}
#[test]
fn passes_through_characters_outside_the_recognized_set_literally() {
assert_eq!(format(sample(2026, 8, 17), "Q"), "Q");
}
#[test]
fn strtotime_parses_rfc3339() {
let parsed = strtotime("2026-08-17T14:05:09Z");
assert_eq!(format(parsed, "Y-m-d H:i:s"), "2026-08-17 14:05:09");
}
#[test]
fn strtotime_parses_the_eloquent_timestamp_format() {
let parsed = strtotime("2026-08-17 14:05:09");
assert_eq!(format(parsed, "Y-m-d H:i:s"), "2026-08-17 14:05:09");
}
#[test]
fn strtotime_parses_a_bare_date() {
let parsed = strtotime("2026-08-17");
assert_eq!(format(parsed, "Y-m-d H:i:s"), "2026-08-17 00:00:00");
}
#[test]
fn strtotime_falls_back_to_now_for_an_unrecognized_string() {
let before = Utc::now();
let parsed = strtotime("next monday");
assert!(parsed >= before);
}
}