use simple_datetime_rs::{Date, DateTime, Format, Time};
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("=== simple-datetime-rs Formatting Demo ===\n");
let date = Date::new(2023, 6, 15);
let time = Time::new(14, 30, 45);
let datetime = DateTime::new(date, time, 0);
println!("1. Basic Date Formatting:");
println!(" %Y-%m-%d: {}", date.format("%Y-%m-%d")?);
println!(" %B %d, %Y: {}", date.format("%B %d, %Y")?);
println!(" %A, %B %d, %Y: {}", date.format("%A, %B %d, %Y")?);
println!(" %D (MM/DD/YY): {}", date.format("%D")?);
println!(" %F (YYYY-MM-DD): {}", date.format("%F")?);
println!(" Day of year (%j): {}", date.format("%j")?);
println!();
println!("2. Basic Time Formatting:");
println!(" %H:%M:%S: {}", time.format("%H:%M:%S")?);
println!(" %I:%M %p: {}", time.format("%I:%M %p")?);
println!(" %T (HH:MM:SS): {}", time.format("%T")?);
println!();
println!("3. DateTime Formatting:");
println!(
" %Y-%m-%d %H:%M:%S: {}",
datetime.format("%Y-%m-%d %H:%M:%S")?
);
println!(
" %A, %B %d, %Y at %I:%M %p: {}",
datetime.format("%A, %B %d, %Y at %I:%M %p")?
);
println!(" %+ (ISO 8601): {}", datetime.format("%+")?);
println!();
println!("4. Timezone Formatting:");
let dt_utc = DateTime::new(date, time, 0);
let dt_est = DateTime::new(date, time, -300);
let dt_cet = DateTime::new(date, time, 120);
println!(" UTC (%z): {}", dt_utc.format("%z")?);
println!(" EST (%:z): {}", dt_est.format("%:z")?);
println!(" CET (%:z): {}", dt_cet.format("%:z")?);
println!();
println!("5. Microsecond Formatting:");
let time_with_micros = Time::new_with_microseconds(14, 30, 45, 123456);
println!(
" %H:%M:%S.%f: {}",
time_with_micros.format("%H:%M:%S.%f")?
);
println!(" %T.%f: {}", time_with_micros.format("%T.%f")?);
println!();
println!("6. Custom Format Strings:");
println!(" Log format: {}", datetime.format("[%Y-%m-%d %H:%M:%S]")?);
println!(" File name: {}", datetime.format("backup_%Y%m%d_%H%M%S")?);
println!(
" Human readable: {}",
datetime.format("%A, %B %d, %Y at %I:%M %p")?
);
println!(" Progress: {}% complete", datetime.format("100%%")?);
println!();
println!("7. DateTime::now() - Current Time:");
let now = DateTime::now();
println!(" Current time: {}", now.format("%Y-%m-%d %H:%M:%S")?);
println!(" Current time (12-hour): {}", now.format("%I:%M:%S %p")?);
println!(
" Current time (full): {}",
now.format("%A, %B %d, %Y at %I:%M:%S %p")?
);
println!();
println!("8. Weekday Information:");
let dates = vec![
Date::new(2023, 6, 12),
Date::new(2023, 6, 13),
Date::new(2023, 6, 14),
Date::new(2023, 6, 15),
Date::new(2023, 6, 16),
Date::new(2023, 6, 17),
Date::new(2023, 6, 18),
];
for date in dates {
println!(
" {}: {} (%w)",
date.format("%Y-%m-%d")?,
date.format("%A")?
);
}
println!();
println!("9. Month Names:");
for month in 1..=12 {
let test_date = Date::new(2023, month, 1);
println!(
" Month {}: {} ({})",
month,
test_date.format("%B")?,
test_date.format("%b")?
);
}
println!();
println!("10. Error Handling:");
match "invalid format %X".parse::<Date>() {
Ok(_) => println!(" Unexpected success"),
Err(e) => println!(" Expected error: {}", e),
}
match datetime.format("%X") {
Ok(_) => println!(" Unexpected success"),
Err(e) => println!(" Expected error: {}", e),
}
println!("\n=== Formatting Demo Complete ===");
Ok(())
}