-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathduration.dart
81 lines (65 loc) · 1.9 KB
/
duration.dart
1
2
3
4
5
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
77
78
79
80
81
import "../document.dart";
import "./duration/iso8601_parser.dart";
/// Represents a ISO8601 duration
class Duration {
/// Number of years
num years;
/// Number of months
num months;
/// Number of weeks
num weeks;
/// Number of days
num days;
/// Nubmer of hours
num hours;
/// Number of minutes
num minutes;
/// Number of seconds
num seconds;
/// Construct a new duration, defaulting to 0 seconds
Duration({
this.years = 0,
this.months = 0,
this.weeks = 0,
this.days = 0,
this.hours = 0,
this.minutes = 0,
this.seconds = 0,
});
/// Construct a new duration from a map of parts
Duration.fromParts(Map<String, num> parts)
: years = parts['years'] ?? 0,
months = parts['months'] ?? 0,
weeks = parts['weeks'] ?? 0,
days = parts['days'] ?? 0,
hours = parts['hours'] ?? 0,
minutes = parts['minutes'] ?? 0,
seconds = parts['seconds'] ?? 0;
@override
bool operator ==(other) =>
other is Duration &&
other.years == years &&
other.months == months &&
other.weeks == weeks &&
other.days == days &&
other.hours == hours &&
other.minutes == minutes &&
other.seconds == seconds;
@override
int get hashCode =>
[years, months, weeks, days, hours, minutes, seconds].hashCode;
@override
String toString() =>
"years:$years months:$months weeks:$weeks days:$days hours:$hours minutes:$minutes seconds:$seconds";
}
/// ISO8601 duration format.
class KdlDuration extends KdlValue<Duration> {
/// Construct a new `KdlDuration`
KdlDuration(super.value, [super.type]);
/// Convert a `KdlString` into a `KdlDuration`
static KdlDuration? convert(KdlValue value, [String type = 'duration']) {
if (value is! KdlString) return null;
var parts = Iso8601DurationParser(value.value).parse();
return KdlDuration(Duration.fromParts(parts), type);
}
}