Struct Duration

Source
pub struct Duration { /* private fields */ }
Expand description

Simple Duration type with second precision

This struct provides time representation in seconds, optimized for hours/minutes/seconds handling.

Implementations§

Source§

impl Duration

Source

pub fn from_seconds(seconds: u64) -> Self

Create a new Duration from seconds

§Examples
use simple_duration::Duration;

let duration = Duration::from_seconds(3661);
assert_eq!(duration.hours_part(), 1);
assert_eq!(duration.minutes_part(), 1);
assert_eq!(duration.seconds_part(), 1);
Examples found in repository?
examples/basic_usage.rs (line 8)
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}
Source

pub fn from_minutes(minutes: u64) -> Self

Create Duration from minutes

§Examples
use simple_duration::Duration;

let duration = Duration::from_minutes(90);
assert_eq!(duration.as_seconds(), 5400);
assert_eq!(duration.hours_part(), 1);
assert_eq!(duration.minutes_part(), 30);
assert_eq!(duration.seconds_part(), 0);
Examples found in repository?
examples/basic_usage.rs (line 9)
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}
Source

pub fn from_hours(hours: u64) -> Self

Create Duration from hours

§Examples
use simple_duration::Duration;

let duration = Duration::from_hours(2);
assert_eq!(duration.as_seconds(), 7200);
assert_eq!(duration.hours_part(), 2);
assert_eq!(duration.minutes_part(), 0);
assert_eq!(duration.seconds_part(), 0);
Examples found in repository?
examples/basic_usage.rs (line 10)
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}
Source

pub fn from_hms(hours: u64, minutes: u64, seconds: u64) -> Self

Create Duration from hours, minutes, and seconds

§Examples
use simple_duration::Duration;

let duration = Duration::from_hms(1, 30, 45);
assert_eq!(duration.as_seconds(), 5445);
Examples found in repository?
examples/basic_usage.rs (line 11)
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}
Source

pub fn parse(s: &str) -> Result<Self, DurationError>

Parse Duration from “hh:mm:ss” format string

§Examples
use simple_duration::Duration;

let duration = Duration::parse("01:30:45").unwrap();
assert_eq!(duration.hours_part(), 1);
assert_eq!(duration.minutes_part(), 30);
assert_eq!(duration.seconds_part(), 45);

assert!(Duration::parse("invalid").is_err());
Examples found in repository?
examples/basic_usage.rs (line 34)
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}
Source

pub fn as_seconds(&self) -> u64

Get total seconds

§Examples
use simple_duration::Duration;

let duration = Duration::from_seconds(3661);
assert_eq!(duration.as_seconds(), 3661);
Examples found in repository?
examples/basic_usage.rs (line 22)
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}
Source

pub fn as_minutes(&self) -> u64

Get total minutes (truncated)

§Examples
use simple_duration::Duration;

let duration = Duration::from_seconds(150); // 2 minutes 30 seconds
assert_eq!(duration.as_minutes(), 2);

let duration = Duration::from_seconds(3661); // 1 hour 1 minute 1 second
assert_eq!(duration.as_minutes(), 61);
Examples found in repository?
examples/basic_usage.rs (line 23)
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}
Source

pub fn as_hours(&self) -> u64

Get total hours (truncated)

§Examples
use simple_duration::Duration;

let duration = Duration::from_seconds(3661); // 1 hour 1 minute 1 second
assert_eq!(duration.as_hours(), 1);

let duration = Duration::from_seconds(7200); // 2 hours
assert_eq!(duration.as_hours(), 2);
Examples found in repository?
examples/basic_usage.rs (line 24)
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}
Source

pub fn seconds_part(&self) -> u64

Get seconds component (0-59)

§Examples
use simple_duration::Duration;

let duration = Duration::from_seconds(3661); // 1 hour 1 minute 1 second
assert_eq!(duration.seconds_part(), 1);

let duration = Duration::from_seconds(150); // 2 minutes 30 seconds
assert_eq!(duration.seconds_part(), 30);
Examples found in repository?
examples/basic_usage.rs (line 28)
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}
Source

pub fn minutes_part(&self) -> u64

Get minutes component (0-59)

§Examples
use simple_duration::Duration;

let duration = Duration::from_seconds(3661); // 1 hour 1 minute 1 second
assert_eq!(duration.minutes_part(), 1);

let duration = Duration::from_seconds(150); // 2 minutes 30 seconds
assert_eq!(duration.minutes_part(), 2);
Examples found in repository?
examples/basic_usage.rs (line 29)
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}
Source

pub fn hours_part(&self) -> u64

Get hours component (0-∞)

§Examples
use simple_duration::Duration;

let duration = Duration::from_seconds(3661); // 1 hour 1 minute 1 second
assert_eq!(duration.hours_part(), 1);

let duration = Duration::from_seconds(7200); // 2 hours
assert_eq!(duration.hours_part(), 2);
Examples found in repository?
examples/basic_usage.rs (line 30)
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}
Source

pub fn format(&self) -> String

Format as “hh:mm:ss” string

§Examples
use simple_duration::Duration;

let duration = Duration::from_hms(1, 5, 30);
assert_eq!(duration.format(), "01:05:30");
Examples found in repository?
examples/basic_usage.rs (line 13)
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}
Source

pub fn zero() -> Self

Create a zero Duration

Source

pub fn is_zero(&self) -> bool

Check if this Duration is zero

Source

pub fn saturating_add(self, other: Self) -> Self

Saturating addition (prevents overflow)

Source

pub fn saturating_sub(self, other: Self) -> Self

Saturating subtraction (prevents underflow)

Source§

impl Duration

SystemTime conversion (only when std feature is enabled)

Source

pub fn from_system_time_diff(start: SystemTime, end: SystemTime) -> Option<Self>

Create Duration from the time difference between two SystemTimes

§Examples
use simple_duration::Duration;
use std::time::SystemTime;

let start = SystemTime::now();
// Some processing...
let end = SystemTime::now();

if let Some(duration) = Duration::from_system_time_diff(start, end) {
    println!("Elapsed time: {}", duration.format());
}

Trait Implementations§

Source§

impl Add for Duration

Source§

type Output = Duration

The resulting type after applying the + operator.
Source§

fn add(self, other: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl Clone for Duration

Source§

fn clone(&self) -> Duration

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Duration

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Duration

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Hash for Duration

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Duration

Source§

fn cmp(&self, other: &Duration) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Duration

Source§

fn eq(&self, other: &Duration) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Duration

Source§

fn partial_cmp(&self, other: &Duration) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Sub for Duration

Source§

type Output = Duration

The resulting type after applying the - operator.
Source§

fn sub(self, other: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl Copy for Duration

Source§

impl Eq for Duration

Source§

impl StructuralPartialEq for Duration

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.