use chrono::{DateTime, Datelike, NaiveDate, Utc};
pub(crate) fn format_time(time: &DateTime<Utc>) -> String {
time.format("%H:%M").to_string()
}
pub(crate) fn format_date(date: Option<&NaiveDate>) -> String {
match date {
Some(date) => date.format("%d/%m/%Y").to_string(),
None => "".to_string(),
}
}
pub(crate) fn format_datetime(datetime: Option<&DateTime<Utc>>) -> String {
match datetime {
Some(datetime) => datetime.format("%Y-%m-%dT%H:%M:%S").to_string(),
None => "".to_string(),
}
}
pub(crate) fn get_day_of_week(date: Option<NaiveDate>) -> String {
match date {
Some(date) => {
let number_from_sunday = date.weekday().number_from_sunday() - 1;
number_from_sunday.to_string()
}
None => "".to_string(),
}
}
pub(crate) fn get_ergani_overtime_cancellation(cancellation: bool) -> String {
if cancellation {
"1".to_string()
} else {
"0".to_string()
}
}
#[cfg(test)]
mod tests {
use super::*;
use chrono::{NaiveDate, Utc};
#[test]
fn test_format_time() {
let dt = Utc::now();
let formatted_time = format_time(&dt);
assert_eq!(formatted_time, dt.format("%H:%M").to_string());
}
#[test]
fn test_format_date() {
let date = NaiveDate::from_ymd_opt(2021, 1, 1).unwrap();
let formatted_date = format_date(Some(&date));
assert_eq!(formatted_date, date.format("%d/%m/%Y").to_string());
}
#[test]
fn test_format_datetime() {
let dt = Utc::now();
let formatted_datetime = format_datetime(Some(&dt));
assert_eq!(
formatted_datetime,
dt.format("%Y-%m-%dT%H:%M:%S").to_string()
);
}
#[test]
fn test_get_day_of_week() {
let date = NaiveDate::from_ymd_opt(2024, 3, 17).unwrap();
let day_of_week = get_day_of_week(Some(date));
let expected_day_of_week = (date.weekday().number_from_sunday() - 1).to_string();
assert_eq!(day_of_week, expected_day_of_week);
}
#[test]
fn test_get_ergani_overtime_cancellation() {
let cancellation = true;
let ergani_cancellation = get_ergani_overtime_cancellation(cancellation);
assert_eq!(ergani_cancellation, "1");
}
}