update method
The update function handles not only the updates to an existing Timespan but also acts as a quasi constructor. Since all parameters are optional the default behaviour is explained in the update-function itself.
Implementation
void update({DateTime? begin, DateTime? end, Duration? duration}) {
if (duration == null) {
/// use the supplied begin or unix time = 0
this.begin = begin ?? DateTime(1970);
/// use the supplied end or now
this.end = end ?? DateTime.now();
/// calc resulting duration
this.duration = this.end.difference(this.begin);
} else if (begin != null && end != null) {
/// If this condition is reached, all arguments are given.
/// Hence the duration needs to match the begin and end times.
if (begin.difference(end).compareTo(duration) == 0) {
this.begin = begin;
this.end = end;
this.duration = duration;
} else {
throw ArgumentError("Starttime endtime and duration do not match");
}
/// if only begin and duration are given,
/// calc the end time from begin + duration
} else if (begin != null) {
this.begin = begin;
this.end = begin.add(duration);
this.duration = duration;
/// if only end and duration are given,
/// calc the begin time from end - duration
} else {
this.end = end ?? DateTime.now();
this.begin = this.end.subtract(duration);
this.duration = duration;
}
}