format method
Returns formatted String with a specified format.
Currently allowed format types:
- Y, YY (years)
- M, MM (months)
- W, WW (weeks)
- D, DD (days)
- h, hh (hours)
- m, mm (months)
- s, ss (seconds)
To use it as a format type wrap it with curly braces.
Two character length format type, e.g. 'ss', adds a trailing zero at the beginning if it is lesser than 10.
If the IsoDuration is negative then a minus
is present in the format
as well. To inverse (negative becomes positive and vice versa) the IsoDuration
use inverse.
Example:
final dur = IsoDuration(hours: 2, minutes: 30);
dur.format('See you in {h} hours and {mm} minutes'); // 'See you in 2 hours and 30 minutes'
Implementation
String format(String format) {
final strBuffer = StringBuffer('');
final list = format.split(RegExp('(?={)|(?<=})'));
for (final item in list) {
if (!RegExp('({)[A-Za-z]{1,2}(})').hasMatch(item)) {
strBuffer.write(item);
continue;
}
var tempItem = item.replaceAll(RegExp('[{}]'), '');
if (_TimeFormat.values.any((e) => e.describe() == tempItem)) {
strBuffer.write(_getFormat(tempItem));
} else {
strBuffer.write(item);
}
}
return strBuffer.toString();
}