basic_usage/
basic_usage.rs

1use simple_duration::Duration;
2
3fn main() {
4    println!("=== simple_duration Usage Example ===\n");
5
6    // Examples of various constructors
7    println!("1. Constructors:");
8    let d1 = Duration::from_seconds(3661);
9    let d2 = Duration::from_minutes(90);
10    let d3 = Duration::from_hours(2);
11    let d4 = Duration::from_hms(1, 30, 45);
12
13    println!("  from_seconds(3661): {}", d1.format());
14    println!("  from_minutes(90):   {}", d2.format());
15    println!("  from_hours(2):      {}", d3.format());
16    println!("  from_hms(1,30,45):  {}", d4.format());
17
18    // Example: get total amounts in each unit
19    println!("\n2. Get total amounts (as_* methods):");
20    let duration = Duration::from_hms(2, 15, 30); // 2 hours 15 minutes 30 seconds
21    println!("  Duration: {}", duration.format());
22    println!("  as_seconds(): {} seconds", duration.as_seconds());
23    println!("  as_minutes(): {} minutes", duration.as_minutes());
24    println!("  as_hours():   {} hours", duration.as_hours());
25
26    // Example: get each component
27    println!("\n3. Get each component (h:m:s format):");
28    println!("  seconds_part(): {} (seconds part)", duration.seconds_part());
29    println!("  minutes_part():      {} (minutes part)", duration.minutes_part());
30    println!("  hours_part():        {} (hours part)", duration.hours_part());
31
32    // Example: parse from string
33    println!("\n4. Parse from string:");
34    match Duration::parse("12:34:56") {
35        Ok(d) => println!("  \"12:34:56\" -> {} ({} seconds)", d.format(), d.as_seconds()),
36        Err(e) => println!("  Error: {:?}", e),
37    }
38
39    // Example: arithmetic
40    println!("\n5. Arithmetic with Duration:");
41    let d_a = Duration::from_minutes(45);
42    let d_b = Duration::from_minutes(30);
43    println!("  {} + {} = {}", d_a.format(), d_b.format(), (d_a + d_b).format());
44    println!("  {} - {} = {}", d_a.format(), d_b.format(), (d_a - d_b).format());
45
46    // Practical example
47    println!("\n6. Practical example:");
48    
49    // Calculate total work time
50    let morning_work = Duration::from_hms(4, 0, 0);   // 4 hours
51    let afternoon_work = Duration::from_hms(3, 30, 0); // 3 hours 30 minutes
52    let total_work = morning_work + afternoon_work;
53    
54    println!("  Morning work: {}", morning_work.format());
55    println!("  Afternoon work: {}", afternoon_work.format());
56    println!("  Total work time: {} ({} minutes)", total_work.format(), total_work.as_minutes());
57    
58    // Calculate remaining time to target
59    let target_hours = Duration::from_hours(8); // 8 hour target
60    let remaining = target_hours - total_work;
61    println!("  Remaining to target: {}", remaining.format());
62
63    println!("\n=== End ===");
64}