first commit
This commit is contained in:
177
src/utilities/hospital.rs
Normal file
177
src/utilities/hospital.rs
Normal file
@@ -0,0 +1,177 @@
|
||||
use core::panic;
|
||||
use std::io;
|
||||
use chrono::{offset::LocalResult, DateTime, Datelike, Duration, Local, TimeZone};
|
||||
|
||||
struct Hospital {
|
||||
id: u32,
|
||||
name: String,
|
||||
}
|
||||
|
||||
// struct Schedule {
|
||||
// from: DateTime<chrono_tz::Tz>,
|
||||
// to: DateTime<chrono_tz::Tz>,
|
||||
// }
|
||||
|
||||
pub fn create_hospital_list() {
|
||||
|
||||
// create the hospitals
|
||||
let hospitals: [Hospital; 2] = [
|
||||
Hospital {
|
||||
id: 1,
|
||||
name: "Centre Hospitalier".to_string(),
|
||||
},
|
||||
Hospital {
|
||||
id: 2,
|
||||
name: "Hopital Kirchberg".to_string(),
|
||||
},
|
||||
];
|
||||
|
||||
// let mut start: DateTime<Local> = Local.with_ymd_and_hms(2025, 1, 6, 8, 0, 0).unwrap(); // set date for testing
|
||||
let mut start: DateTime<Local> = Local::now();
|
||||
let mut end: DateTime<Local> = get_end_of_shift(start);
|
||||
|
||||
println!("It is now {} and the current Shift is ending on {}", start.format("%A %d/%m/%Y %H:%M"), end.format("%A %d/%m/%Y %H:%M"));
|
||||
println!("Whitch Hospital is actualy on duty?");
|
||||
|
||||
// print out the hospitals
|
||||
for hospital in &hospitals {
|
||||
println!("{}) {}", hospital.id, hospital.name)
|
||||
}
|
||||
|
||||
let mut hospital_id_input = String::new();
|
||||
io::stdin()
|
||||
.read_line(&mut hospital_id_input)
|
||||
.expect("Failed to read the line");
|
||||
let hospital_id_input = hospital_id_input.trim();
|
||||
let hospital_id: i32 = match hospital_id_input.parse() {
|
||||
Ok(num) => num,
|
||||
Err(_) => {
|
||||
println!("Please enter a valid number!");
|
||||
return;
|
||||
}
|
||||
};
|
||||
let mut hospital_index: usize = (hospital_id - 1) as usize;
|
||||
|
||||
for _ in 1..=100 {
|
||||
|
||||
// Print the current hospital on duty
|
||||
println!("From {} to {} is {} on duty.", start.format("%A %d/%m/%Y %H:%M"), end.format("%A %d/%m/%Y %H:%M"), hospitals[hospital_index].name );
|
||||
|
||||
// new shift
|
||||
start = end;
|
||||
end = get_end_of_shift(start);
|
||||
|
||||
// change hospital
|
||||
hospital_index = if hospital_index == 0 { 1 } else { 0 }
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
fn get_end_of_shift(start: DateTime<Local>) -> DateTime<Local> {
|
||||
|
||||
let end: DateTime<Local>;
|
||||
|
||||
// define weekday & hour
|
||||
let weekday_str: String = start.format("%u").to_string();
|
||||
let weekday: u32 = weekday_str.parse().unwrap();
|
||||
let hour_str: String = start.format("%H").to_string();
|
||||
let hour: u32 = hour_str.parse().unwrap();
|
||||
|
||||
// From Monday to Thursday before 7:00
|
||||
if weekday >= 1 && weekday <= 4 && hour < 7 {
|
||||
|
||||
let next_day: DateTime<Local> = start;
|
||||
let year: i32 = next_day.year();
|
||||
let month: u32 = next_day.month();
|
||||
let day: u32 = next_day.day();
|
||||
|
||||
let local_result: LocalResult<DateTime<Local>> = Local.with_ymd_and_hms(year, month, day, 7, 0, 0);
|
||||
end = match local_result {
|
||||
LocalResult::None => {
|
||||
panic!("The specified date and time do not exist.");
|
||||
},
|
||||
LocalResult::Single(datetime) => datetime,
|
||||
LocalResult::Ambiguous(datetime1, _datetime2, ) => {
|
||||
datetime1
|
||||
}
|
||||
};
|
||||
|
||||
// From Monday to Wednesday after 7:00
|
||||
} else if weekday >= 1 && weekday <= 3 && hour >= 7 {
|
||||
|
||||
let next_day: DateTime<Local> = start + Duration::days(1);
|
||||
let year: i32 = next_day.year();
|
||||
let month: u32 = next_day.month();
|
||||
let day: u32 = next_day.day();
|
||||
|
||||
let local_result: LocalResult<DateTime<Local>> = Local.with_ymd_and_hms(year, month, day, 7, 0, 0);
|
||||
end = match local_result {
|
||||
LocalResult::None => {
|
||||
panic!("The specified date and time do not exist.");
|
||||
},
|
||||
LocalResult::Single(datetime) => datetime,
|
||||
LocalResult::Ambiguous(datetime1, _datetime2, ) => {
|
||||
datetime1
|
||||
}
|
||||
};
|
||||
|
||||
} else if weekday == 4 && hour >= 7 {
|
||||
|
||||
let next_day: DateTime<Local> = start + Duration::days(1);
|
||||
let year: i32 = next_day.year();
|
||||
let month: u32 = next_day.month();
|
||||
let day: u32 = next_day.day();
|
||||
|
||||
let local_result: LocalResult<DateTime<Local>> = Local.with_ymd_and_hms(year, month, day, 17, 0, 0);
|
||||
end = match local_result {
|
||||
LocalResult::None => {
|
||||
panic!("The specified date and time do not exist.");
|
||||
},
|
||||
LocalResult::Single(datetime) => datetime,
|
||||
LocalResult::Ambiguous(datetime1, _datetime2, ) => {
|
||||
datetime1
|
||||
}
|
||||
};
|
||||
|
||||
} else if weekday == 5 && hour < 17 {
|
||||
|
||||
let next_day: DateTime<Local> = start;
|
||||
let year: i32 = next_day.year();
|
||||
let month: u32 = next_day.month();
|
||||
let day: u32 = next_day.day();
|
||||
|
||||
let local_result: LocalResult<DateTime<Local>> = Local.with_ymd_and_hms(year, month, day, 17, 0, 0);
|
||||
end = match local_result {
|
||||
LocalResult::None => {
|
||||
panic!("The specified date and time do not exist.");
|
||||
},
|
||||
LocalResult::Single(datetime) => datetime,
|
||||
LocalResult::Ambiguous(datetime1, _datetime2, ) => {
|
||||
datetime1
|
||||
}
|
||||
};
|
||||
|
||||
// Friday after 17:00
|
||||
} else {
|
||||
|
||||
let count_days: i64 = 8 - weekday as i64;
|
||||
let next_day: DateTime<Local> = start + Duration::days(count_days);
|
||||
let year: i32 = next_day.year();
|
||||
let month: u32 = next_day.month();
|
||||
let day: u32 = next_day.day();
|
||||
|
||||
let local_result: LocalResult<DateTime<Local>> = Local.with_ymd_and_hms(year, month, day, 7, 0, 0);
|
||||
end = match local_result {
|
||||
LocalResult::None => {
|
||||
panic!("The specified date and time do not exist.");
|
||||
},
|
||||
LocalResult::Single(datetime) => datetime,
|
||||
LocalResult::Ambiguous(datetime1, _datetime2, ) => {
|
||||
datetime1
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
end
|
||||
}
|
||||
Reference in New Issue
Block a user