Struct yonsei_flexible::time::HMS
source · Fields§
§hour: i8
§minute: i8
§second: i8
Implementations§
source§impl HMS
impl HMS
sourcepub fn new(hour: i8, minute: i8, second: i8) -> Self
pub fn new(hour: i8, minute: i8, second: i8) -> Self
Examples found in repository?
src/time.rs (line 28)
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
pub fn parse(s: &str) -> Option<Self> {
let mut parts = s.split(':');
let hour = parts.next()?.parse().ok()?;
let minute = parts.next()?.parse().ok()?;
let second = match parts.next() {
None => 0,
Some(s) => s.parse().ok()?,
};
Some(HMS::new(hour, minute, second))
}
}
impl PartialOrd for HMS {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(
self.hour
.cmp(&other.hour)
.then(self.minute.cmp(&other.minute))
.then(self.second.cmp(&other.second)),
)
}
}
impl Ord for HMS {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.partial_cmp(other).unwrap()
}
}
impl Display for HMS {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:02}:{:02}:{:02}", self.hour, self.minute, self.second)
}
}
impl Add for HMS {
type Output = HMS;
fn add(self, other: HMS) -> HMS {
let mut hour = self.hour + other.hour;
let mut minute = self.minute + other.minute;
let mut second = self.second + other.second;
if second >= 60 {
second -= 60;
minute += 1;
}
if minute >= 60 {
minute -= 60;
hour += 1;
}
HMS::new(hour, minute, second)
}
}
impl Sub for HMS {
type Output = HMS;
fn sub(self, other: HMS) -> HMS {
let mut hour = self.hour - other.hour;
let mut minute = self.minute - other.minute;
let mut second = self.second - other.second;
if second < 0 {
second += 60;
minute -= 1;
}
if minute < 0 {
minute += 60;
hour -= 1;
}
HMS::new(hour, minute, second)
}
More examples
src/calc.rs (line 7)
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
pub fn calc_work_time(interval: Interval) -> HMS {
if interval.start_time < HMS::new(6, 0, 0) {
panic!("start time must be after 6:00");
}
if interval.end_time >= HMS::new(24, 0, 0) {
panic!("end time must be before 24:00");
}
let mut work_time = interval.end_time - interval.start_time;
let (total_exceed, _, _) = calc_total_exceed(interval);
work_time = work_time - total_exceed;
if work_time > HMS::new(12, 0, 0) {
work_time = HMS::new(12, 0, 0);
}
work_time
}
pub fn calc_minimum_finish_time(start: HMS) -> HMS {
calc_hope_finish_time(start, HMS::new(4,0,0))
}
pub fn calc_hope_finish_time(start: HMS, hope_work_time: HMS) -> HMS {
let mut finish = start + hope_work_time;
let mut work_time = calc_work_time(Interval::new(start, finish));
loop {
if work_time < hope_work_time {
finish = finish + (hope_work_time - work_time);
work_time = calc_work_time(Interval::new(start, finish));
} else if work_time > hope_work_time {
finish = finish - (work_time - hope_work_time);
work_time = calc_work_time(Interval::new(start, finish));
} else {
break;
}
}
finish
}
pub fn calc_total_exceed(interval: Interval) -> (HMS, Meal, Meal) {
let mut total_exceed = HMS::new(0, 0, 0);
let lunch = contains_lunch_break(interval);
let dinner = contains_dinner_break(interval);
let half_hour = HMS::new(0, 30, 0);
match lunch {
(true, exceed) => {
total_exceed = total_exceed + exceed;
}
_ => (),
}
match dinner {
(true, exceed) => {
total_exceed = total_exceed + exceed;
}
_ => (),
}
total_exceed = if total_exceed < half_hour {
half_hour
} else {
total_exceed
};
(total_exceed, lunch, dinner)
}
src/cond.rs (line 27)
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
pub fn contains_lunch_break(interval: Interval) -> (bool, HMS) {
let lunch_break = Interval::new(HMS::new(11, 0, 0), HMS::new(13, 0, 0));
let mut contains = false;
let mut exceed = HMS::new(0, 0, 0);
// 1. Check if the interval contains the lunch break
if interval.contains(lunch_break.start_time) {
if interval.contains(lunch_break.end_time) {
contains = true;
exceed = HMS::new(1,0,0);
} else {
// 2. Check if the difference is more than 1 hour
let diff = interval.end_time - lunch_break.start_time;
if diff >= HMS::new(1, 0, 0) {
contains = true;
exceed = diff - HMS::new(1, 0, 0);
}
}
} else if interval.contains(lunch_break.end_time) {
// 3. Check if the difference is more than 1 hour
let diff = lunch_break.end_time - interval.start_time;
if diff >= HMS::new(1, 0, 0) {
contains = true;
exceed = diff - HMS::new(1, 0, 0);
}
}
(contains, exceed)
}
pub fn contains_dinner_break(interval: Interval) -> (bool, HMS) {
let dinner_break = Interval::new(HMS::new(18, 0, 0), HMS::new(20, 0, 0));
let mut contains = false;
let mut exceed = HMS::new(0, 0, 0);
// 1. Check if the interval contains the dinner break
if interval.contains(dinner_break.start_time) {
if interval.contains(dinner_break.end_time) {
contains = true;
exceed = HMS::new(1,0,0);
} else {
// 2. Check if the difference is more than 1 hour
let diff = interval.end_time - dinner_break.start_time;
if diff >= HMS::new(1, 0, 0) {
contains = true;
exceed = diff - HMS::new(1, 0, 0);
}
}
} else if interval.contains(dinner_break.end_time) {
// 3. Check if the difference is more than 1 hour
let diff = dinner_break.end_time - interval.start_time;
if diff >= HMS::new(1, 0, 0) {
contains = true;
exceed = diff - HMS::new(1, 0, 0);
}
}
(contains, exceed)
}
sourcepub fn parse(s: &str) -> Option<Self>
pub fn parse(s: &str) -> Option<Self>
Examples found in repository?
examples/example.rs (line 40)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
fn main() {
let example_inputs = r#"
10:00:00 - 17:00:00
10:00:00 - 21:00:00
11:15:00 - 17:00:00
11:45:00 - 17:00:00
12:15:00 - 17:00:00
12:45:00 - 17:00:00
11:15:00 - 18:15:00
11:45:00 - 18:15:00
12:15:00 - 18:15:00
12:45:00 - 18:15:00
11:15:00 - 18:45:00
11:45:00 - 18:45:00
12:15:00 - 18:45:00
12:45:00 - 18:45:00
11:15:00 - 19:15:00
11:45:00 - 19:15:00
12:15:00 - 19:15:00
12:45:00 - 19:15:00
11:15:00 - 19:45:00
11:45:00 - 19:45:00
12:15:00 - 19:45:00
12:45:00 - 19:45:00
"#;
let inputs = example_inputs
.lines()
.filter(|line| !line.trim().is_empty())
.map(|line| {
let mut iter = line.split("-");
let start = iter.next().unwrap().trim();
let end = iter.next().unwrap().trim();
Interval::new(HMS::parse(start).unwrap(), HMS::parse(end).unwrap())
})
.collect::<Vec<_>>();
for input in inputs {
println!("==================================================");
println!("Input: {} - {}", input.start_time, input.end_time);
println!("Total time: {}", input.get_duration());
println!("Work time: {}", calc_work_time(input));
println!("==================================================");
println!("");
}
}
Trait Implementations§
source§impl Ord for HMS
impl Ord for HMS
source§impl PartialOrd<HMS> for HMS
impl PartialOrd<HMS> for HMS
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self
and other
) and is used by the <=
operator. Read more