toDateTime method
Format:
Implementation
DateTime? toDateTime() {
if (this == null) {
return null;
}
var parts = this!.trim().split(' ');
if (parts.length == 4) {
// Streamed x y ago
parts = parts.skip(1).toList();
}
if (parts.length != 3) {
return null;
}
final qty = int.parse(parts.first);
// Try to get the unit
final unit = parts[1];
final time = switch (unit) {
_ when unit.startsWith('second') => Duration(seconds: qty),
_ when unit.startsWith('minute') => Duration(minutes: qty),
_ when unit.startsWith('hour') => Duration(hours: qty),
_ when unit.startsWith('day') => Duration(days: qty),
_ when unit.startsWith('week') => Duration(days: qty * 7),
_ when unit.startsWith('month') => Duration(days: qty * 30),
_ when unit.startsWith('year') => Duration(days: qty * 365),
_ => throw StateError("Couldn't parse $unit unit of time. "
'Please report this to the project page!')
};
return DateTime.now().subtract(time);
}