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
impl Duration
Sourcepub fn from_seconds(seconds: u64) -> Self
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}Sourcepub fn from_minutes(minutes: u64) -> Self
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}Sourcepub fn from_hours(hours: u64) -> Self
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}Sourcepub fn from_hms(hours: u64, minutes: u64, seconds: u64) -> Self
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}Sourcepub fn parse(s: &str) -> Result<Self, DurationError>
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}Sourcepub fn as_seconds(&self) -> u64
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}Sourcepub fn as_minutes(&self) -> u64
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}Sourcepub fn as_hours(&self) -> u64
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}Sourcepub fn seconds_part(&self) -> u64
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}Sourcepub fn minutes_part(&self) -> u64
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}Sourcepub fn hours_part(&self) -> u64
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}Sourcepub fn format(&self) -> String
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}Sourcepub fn saturating_add(self, other: Self) -> Self
pub fn saturating_add(self, other: Self) -> Self
Saturating addition (prevents overflow)
Sourcepub fn saturating_sub(self, other: Self) -> Self
pub fn saturating_sub(self, other: Self) -> Self
Saturating subtraction (prevents underflow)
Source§impl Duration
SystemTime conversion (only when std feature is enabled)
impl Duration
SystemTime conversion (only when std feature is enabled)
Sourcepub fn from_system_time_diff(start: SystemTime, end: SystemTime) -> Option<Self>
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 Ord for Duration
impl Ord for Duration
Source§impl PartialOrd for Duration
impl PartialOrd for Duration
impl Copy for Duration
impl Eq for Duration
impl StructuralPartialEq for Duration
Auto Trait Implementations§
impl Freeze for Duration
impl RefUnwindSafe for Duration
impl Send for Duration
impl Sync for Duration
impl Unpin for Duration
impl UnwindSafe for Duration
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more