asAbbreviatedDuration

Constructs a Duration from a string, assumed to be in a *d*h*m*s pattern.

asAbbreviatedDuration
(
const string line
)

Parameters

line string

Abbreviated string line.

Return Value

Type: auto

A Duration as described in the input string.

Throws

DurationStringException if individually negative values were passed.

Examples

1 import std.conv : to;
2 import std.exception : assertThrown;
3 import core.time : days, hours, minutes, seconds;
4 
5 {
6     enum line = "30";
7     immutable actual = line.asAbbreviatedDuration;
8     immutable expected = 30.seconds;
9     assert((actual == expected), actual.to!string);
10 }
11 {
12     enum line = "30s";
13     immutable actual = line.asAbbreviatedDuration;
14     immutable expected = 30.seconds;
15     assert((actual == expected), actual.to!string);
16 }
17 {
18     enum line = "1h30s";
19     immutable actual = line.asAbbreviatedDuration;
20     immutable expected = 1.hours + 30.seconds;
21     assert((actual == expected), actual.to!string);
22 }
23 {
24     enum line = "5h";
25     immutable actual = line.asAbbreviatedDuration;
26     immutable expected = 5.hours;
27     assert((actual == expected), actual.to!string);
28 }
29 {
30     enum line = "1d12h39m40s";
31     immutable actual = line.asAbbreviatedDuration;
32     immutable expected = 1.days + 12.hours + 39.minutes + 40.seconds;
33     assert((actual == expected), actual.to!string);
34 }
35 {
36     enum line = "1d4s";
37     immutable actual = line.asAbbreviatedDuration;
38     immutable expected = 1.days + 4.seconds;
39     assert((actual == expected), actual.to!string);
40 }
41 {
42     enum line = "30s";
43     immutable actual = line.asAbbreviatedDuration;
44     immutable expected = 30.seconds;
45     assert((actual == expected), actual.to!string);
46 }
47 {
48     enum line = "-30s";
49     immutable actual = line.asAbbreviatedDuration;
50     immutable expected = (-30).seconds;
51     assert((actual == expected), actual.to!string);
52 }
53 {
54     import core.time : Duration;
55     enum line = string.init;
56     immutable actual = line.asAbbreviatedDuration;
57     immutable expected = Duration.zero;
58     assert((actual == expected), actual.to!string);
59 }
60 {
61     enum line = "s";
62     assertThrown(line.asAbbreviatedDuration);
63 }
64 {
65     enum line = "1d1h1m1z";
66     assertThrown(line.asAbbreviatedDuration);
67 }
68 {
69     enum line = "2h-30m";
70     assertThrown(line.asAbbreviatedDuration);
71 }