use chrono::{DateTime, Datelike, NaiveDate, TimeZone};
#[must_use]
#[inline]
pub fn to_date_string(date: NaiveDate) -> String {
date.format("%Y-%m-%d").to_string()
}
#[must_use]
#[inline]
pub fn to_long_date(date: NaiveDate) -> String {
format!(
"{} {}, {}",
date.format("%B"),
date.day(),
date.format("%Y")
)
}
#[must_use]
#[inline]
pub fn to_short_date(date: NaiveDate) -> String {
format!(
"{} {}, {}",
date.format("%b"),
date.day(),
date.format("%Y")
)
}
#[must_use]
#[inline]
pub fn to_iso8601<Tz: TimeZone>(datetime: &DateTime<Tz>) -> String {
datetime.to_rfc3339()
}
#[must_use]
#[inline]
pub fn to_rfc2822<Tz: TimeZone>(datetime: &DateTime<Tz>) -> String {
datetime.to_rfc2822()
}
#[cfg(test)]
mod tests {
use super::*;
use chrono::{FixedOffset, Local, NaiveDate, TimeZone};
#[test]
fn test_to_date_string() {
let date = NaiveDate::from_ymd_opt(2026, 2, 22).unwrap();
assert_eq!(to_date_string(date), "2026-02-22");
}
#[test]
fn test_to_date_string_single_digit_month_and_day() {
let date = NaiveDate::from_ymd_opt(2026, 1, 5).unwrap();
assert_eq!(to_date_string(date), "2026-01-05");
}
#[test]
fn test_to_date_string_year_end() {
let date = NaiveDate::from_ymd_opt(2026, 12, 31).unwrap();
assert_eq!(to_date_string(date), "2026-12-31");
}
#[test]
fn test_to_date_string_leap_day() {
let date = NaiveDate::from_ymd_opt(2028, 2, 29).unwrap();
assert_eq!(to_date_string(date), "2028-02-29");
}
#[test]
fn test_to_long_date() {
let date = NaiveDate::from_ymd_opt(2026, 2, 22).unwrap();
assert_eq!(to_long_date(date), "February 22, 2026");
}
#[test]
fn test_to_long_date_single_digit_day() {
let date = NaiveDate::from_ymd_opt(2026, 2, 5).unwrap();
assert_eq!(to_long_date(date), "February 5, 2026");
}
#[test]
fn test_to_long_date_january() {
let date = NaiveDate::from_ymd_opt(2026, 1, 1).unwrap();
assert_eq!(to_long_date(date), "January 1, 2026");
}
#[test]
fn test_to_long_date_december() {
let date = NaiveDate::from_ymd_opt(2026, 12, 31).unwrap();
assert_eq!(to_long_date(date), "December 31, 2026");
}
#[test]
fn test_to_long_date_leap_day() {
let date = NaiveDate::from_ymd_opt(2028, 2, 29).unwrap();
assert_eq!(to_long_date(date), "February 29, 2028");
}
#[test]
fn test_to_short_date() {
let date = NaiveDate::from_ymd_opt(2026, 2, 22).unwrap();
assert_eq!(to_short_date(date), "Feb 22, 2026");
}
#[test]
fn test_to_short_date_single_digit_day() {
let date = NaiveDate::from_ymd_opt(2026, 2, 5).unwrap();
assert_eq!(to_short_date(date), "Feb 5, 2026");
}
#[test]
fn test_to_short_date_january() {
let date = NaiveDate::from_ymd_opt(2026, 1, 1).unwrap();
assert_eq!(to_short_date(date), "Jan 1, 2026");
}
#[test]
fn test_to_short_date_year_end() {
let date = NaiveDate::from_ymd_opt(2026, 12, 31).unwrap();
assert_eq!(to_short_date(date), "Dec 31, 2026");
}
#[test]
fn test_to_short_date_leap_day() {
let date = NaiveDate::from_ymd_opt(2028, 2, 29).unwrap();
assert_eq!(to_short_date(date), "Feb 29, 2028");
}
#[test]
fn test_to_iso8601_local() {
let datetime = Local
.with_ymd_and_hms(2026, 2, 22, 14, 30, 0)
.single()
.unwrap();
let result = to_iso8601(&datetime);
assert!(
result.starts_with("2026-02-22T14:30:00"),
"unexpected iso8601: {result}"
);
}
#[test]
fn test_to_iso8601_utc() {
let tz = FixedOffset::east_opt(0).unwrap();
let datetime = tz
.with_ymd_and_hms(2026, 2, 22, 14, 30, 0)
.single()
.unwrap();
assert_eq!(to_iso8601(&datetime), "2026-02-22T14:30:00+00:00");
}
#[test]
fn test_to_iso8601_positive_offset() {
let tz = FixedOffset::east_opt(5 * 3600 + 30 * 60).unwrap(); let datetime = tz
.with_ymd_and_hms(2026, 2, 22, 14, 30, 0)
.single()
.unwrap();
assert_eq!(to_iso8601(&datetime), "2026-02-22T14:30:00+05:30");
}
#[test]
fn test_to_iso8601_negative_offset() {
let tz = FixedOffset::west_opt(6 * 3600).unwrap(); let datetime = tz
.with_ymd_and_hms(2026, 2, 22, 14, 30, 0)
.single()
.unwrap();
assert_eq!(to_iso8601(&datetime), "2026-02-22T14:30:00-06:00");
}
#[test]
fn test_to_iso8601_midnight() {
let tz = FixedOffset::east_opt(0).unwrap();
let datetime = tz.with_ymd_and_hms(2026, 1, 1, 0, 0, 0).single().unwrap();
assert_eq!(to_iso8601(&datetime), "2026-01-01T00:00:00+00:00");
}
#[test]
fn test_to_iso8601_leap_day() {
let tz = FixedOffset::east_opt(0).unwrap();
let datetime = tz.with_ymd_and_hms(2028, 2, 29, 12, 0, 0).single().unwrap();
assert_eq!(to_iso8601(&datetime), "2028-02-29T12:00:00+00:00");
}
#[test]
fn test_to_iso8601_same_instant_different_offset() {
let utc = FixedOffset::east_opt(0).unwrap();
let ist = FixedOffset::east_opt(5 * 3600 + 30 * 60).unwrap();
let dt_utc = utc.with_ymd_and_hms(2026, 1, 1, 0, 0, 0).single().unwrap();
let dt_ist = ist.with_ymd_and_hms(2026, 1, 1, 5, 30, 0).single().unwrap();
assert_eq!(to_iso8601(&dt_utc), "2026-01-01T00:00:00+00:00");
assert_eq!(to_iso8601(&dt_ist), "2026-01-01T05:30:00+05:30");
assert_eq!(dt_utc.to_utc(), dt_ist.to_utc());
}
#[test]
fn test_to_rfc2822_local() {
let datetime = Local
.with_ymd_and_hms(2026, 2, 22, 14, 30, 0)
.single()
.unwrap();
let result = to_rfc2822(&datetime);
assert!(
result.starts_with("Sun, 22 Feb 2026 14:30:00"),
"unexpected rfc2822: {result}"
);
}
#[test]
fn test_to_rfc2822_utc() {
let tz = FixedOffset::east_opt(0).unwrap();
let datetime = tz
.with_ymd_and_hms(2026, 2, 22, 14, 30, 0)
.single()
.unwrap();
assert_eq!(to_rfc2822(&datetime), "Sun, 22 Feb 2026 14:30:00 +0000");
}
#[test]
fn test_to_rfc2822_positive_offset() {
let tz = FixedOffset::east_opt(5 * 3600 + 30 * 60).unwrap(); let datetime = tz
.with_ymd_and_hms(2026, 2, 22, 14, 30, 0)
.single()
.unwrap();
assert_eq!(to_rfc2822(&datetime), "Sun, 22 Feb 2026 14:30:00 +0530");
}
#[test]
fn test_to_rfc2822_negative_offset() {
let tz = FixedOffset::west_opt(6 * 3600).unwrap(); let datetime = tz
.with_ymd_and_hms(2026, 2, 22, 14, 30, 0)
.single()
.unwrap();
assert_eq!(to_rfc2822(&datetime), "Sun, 22 Feb 2026 14:30:00 -0600");
}
#[test]
fn test_to_rfc2822_weekday_monday() {
let tz = FixedOffset::east_opt(0).unwrap();
let datetime = tz.with_ymd_and_hms(2026, 2, 23, 9, 0, 0).single().unwrap(); assert_eq!(to_rfc2822(&datetime), "Mon, 23 Feb 2026 09:00:00 +0000");
}
#[test]
fn test_to_rfc2822_weekday_friday() {
let tz = FixedOffset::east_opt(0).unwrap();
let datetime = tz.with_ymd_and_hms(2026, 2, 27, 9, 0, 0).single().unwrap(); assert_eq!(to_rfc2822(&datetime), "Fri, 27 Feb 2026 09:00:00 +0000");
}
#[test]
fn test_to_rfc2822_leap_day() {
let tz = FixedOffset::east_opt(0).unwrap();
let datetime = tz.with_ymd_and_hms(2028, 2, 29, 0, 0, 0).single().unwrap(); assert_eq!(to_rfc2822(&datetime), "Tue, 29 Feb 2028 00:00:00 +0000");
}
}