#[cfg(test)]
mod tests {
use bt_time_utils::{get_current_time_and_date, get_formatted_date, get_formatted_time_and_date};
use chrono::NaiveDateTime;
#[test]
fn test_formatted(){
assert_eq!(get_formatted_time_and_date("%H:%M","%B %d, %Y"),get_current_time_and_date());
}
#[test]
fn test_get_formatted_date_format() {
let format = "%Y-%m-%d %H:%M:%S";
let result = get_formatted_date(format);
let parsed = NaiveDateTime::parse_from_str(&result, format);
assert!(parsed.is_ok(), "Resulting string did not match expected format");
}
#[test]
fn test_get_formatted_date_not_empty() {
let result = get_formatted_date("%Y");
assert!(!result.is_empty(), "Resulting date string should not be empty");
}
}
#[cfg(test)]
mod local_to_utc_tests {
use bt_time_utils::parse_local_to_utc;
#[test]
fn converts_new_york_to_utc() {
let input = "2026-04-18T13:41";
let tz = "America/New_York";
let result = parse_local_to_utc(input, tz).unwrap();
assert_eq!(result.to_string(), "2026-04-18 17:41:00.0");
}
#[test]
fn converts_los_angeles_to_utc() {
let input = "2026-04-18T13:41";
let tz = "America/Los_Angeles";
let result = parse_local_to_utc(input, tz).unwrap();
assert_eq!(result.to_string(), "2026-04-18 20:41:00.0");
}
#[test]
fn converts_utc_identity() {
let input = "2026-04-18T13:41";
let tz = "UTC";
let result = parse_local_to_utc(input, tz).unwrap();
assert_eq!(result.to_string(), "2026-04-18 13:41:00.0");
}
#[test]
fn handles_winter_standard_time() {
let input = "2026-01-15T12:00";
let tz = "America/New_York";
let result = parse_local_to_utc(input, tz).unwrap();
assert_eq!(result.to_string(), "2026-01-15 17:00:00.0");
}
#[test]
fn invalid_datetime_fails() {
let input = "not-a-date";
let tz = "America/New_York";
let result = parse_local_to_utc(input, tz);
assert!(result.is_err());
}
#[test]
fn invalid_timezone_fails() {
let input = "2026-04-18T13:41";
let tz = "Mars/Phobos";
let result = parse_local_to_utc(input, tz);
assert!(result.is_err());
}
#[test]
fn dst_spring_forward_invalid_time() {
let input = "2026-03-08T02:30";
let tz = "America/New_York";
let result = parse_local_to_utc(input, tz);
assert!(result.is_err());
}
#[test]
fn dst_fall_back_ambiguous_time() {
let input = "2026-11-01T01:30";
let tz = "America/New_York";
let result = parse_local_to_utc(input, tz);
assert!(result.is_err());
}
}
#[cfg(test)]
mod timezone_convert_tests {
use bt_time_utils::{format_in_iana_timezone_or_utc, format_in_utcoffset_timezone};
use time::{macros::format_description, PrimitiveDateTime, UtcOffset};
#[test]
fn test_format_est() {
let dt = PrimitiveDateTime::parse(
"2026-02-23 18:30",
&format_description!("[year]-[month]-[day] [hour]:[minute]")
).unwrap();
let est = UtcOffset::from_hms(-5, 0, 0).unwrap();
let formatted = format_in_utcoffset_timezone(dt, est);
assert_eq!(formatted, "2026-Feb-23 01:30 PM");
}
#[test]
fn test_format_pst() {
let dt = PrimitiveDateTime::parse(
"2026-02-23 08:15",
&format_description!("[year]-[month]-[day] [hour]:[minute]")
).unwrap();
let pst = UtcOffset::from_hms(-8, 0, 0).unwrap();
let formatted = format_in_utcoffset_timezone(dt, pst);
assert_eq!(formatted, "2026-Feb-23 12:15 AM");
}
#[test]
fn test_midnight_rollover() {
let dt = PrimitiveDateTime::parse(
"2026-02-23 01:30",
&format_description!("[year]-[month]-[day] [hour]:[minute]")
).unwrap();
let pst = UtcOffset::from_hms(-8, 0, 0).unwrap();
let formatted = format_in_utcoffset_timezone(dt, pst);
assert_eq!(formatted, "2026-Feb-22 05:30 PM");
}
#[test]
fn test_new_york_conversion() {
let dt = PrimitiveDateTime::parse(
"2026-02-23 18:30",
&format_description!("[year]-[month]-[day] [hour]:[minute]")
).unwrap();
let formatted = format_in_iana_timezone_or_utc(dt, "America/New_York");
assert_eq!(formatted, "2026-Feb-23 01:30 PM");
}
#[test]
fn test_los_angeles_conversion() {
let dt = PrimitiveDateTime::parse(
"2026-02-23 18:30",
&format_description!("[year]-[month]-[day] [hour]:[minute]")
).unwrap();
let formatted = format_in_iana_timezone_or_utc(dt, "America/Los_Angeles");
assert_eq!(formatted, "2026-Feb-23 10:30 AM");
}
#[test]
fn test_midnight_rollover_iana() {
let dt = PrimitiveDateTime::parse(
"2026-02-23 01:30",
&format_description!("[year]-[month]-[day] [hour]:[minute]")
).unwrap();
let formatted = format_in_iana_timezone_or_utc(dt, "America/Los_Angeles");
assert_eq!(formatted, "2026-Feb-22 05:30 PM");
}
#[test]
fn test_invalid_timezone_falls_back_to_utc() {
let dt = PrimitiveDateTime::parse(
"2026-02-23 18:30",
&format_description!("[year]-[month]-[day] [hour]:[minute]")
).unwrap();
let formatted = format_in_iana_timezone_or_utc(dt, "INVALID/TZ");
assert_eq!(formatted, "2026-Feb-23 06:30 PM"); }
#[test]
fn test_dst_transition() {
let dt = PrimitiveDateTime::parse(
"2026-03-08 07:30",
&format_description!("[year]-[month]-[day] [hour]:[minute]")
).unwrap();
let formatted = format_in_iana_timezone_or_utc(dt, "America/New_York");
assert_eq!(formatted, "2026-Mar-08 03:30 AM");
}
}