Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add number of months instead of weeks if they exceed a certain amount #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,23 @@ main() {
);
}
```

## Months instead of weeks

If a duration spans more than two months it will be formatted as 'x months' instead of 'y weeks'.
To override the amount of months use `minNumberMonths`.

```dart
main() {
// => 8 weeks 4 days
printDuration(
Duration(days: 60)
);

// => 1 month 30 days
printDuration(
Duration(days: 60),
minNumberMonths: 1
);
}
```
27 changes: 20 additions & 7 deletions lib/src/duration.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:duration/src/time_const.dart';

import 'locale/locale.dart';
import 'tersity.dart';

Expand Down Expand Up @@ -35,7 +37,8 @@ String prettyDuration(Duration duration,
String delimiter,
String conjunction,
bool abbreviated = false,
bool first = false}) {
bool first = false,
int minNumberMonths = 2}) {
if (abbreviated && delimiter == null) {
delimiter = ', ';
spacer = '';
Expand All @@ -55,14 +58,22 @@ String prettyDuration(Duration duration,
return sb.toString();
};

final int weeks = (duration.inDays / 7).floor();
final int months = (duration.inDays / avgDaysPerMonth).floor();
int daysCounted = 0;

if (weeks > 0) {
out.add(partFmt(weeks, locale.week));
if (months >= minNumberMonths) {
out.add(partFmt(months, locale.month));
daysCounted = (months * avgDaysPerMonth).floor();
} else {
final int weeks = (duration.inDays / 7).floor();
if (weeks > 0) {
out.add(partFmt(weeks, locale.week));
daysCounted = weeks * 7;
}
}

if (tersity >= DurationTersity.day) {
final int days = duration.inDays - (weeks * 7);
final int days = duration.inDays - daysCounted;
if (days > 0) out.add(partFmt(days, locale.day));

if (tersity >= DurationTersity.hour) {
Expand Down Expand Up @@ -126,14 +137,16 @@ String printDuration(Duration duration,
String spacer,
String delimiter,
String conjugation,
bool abbreviated = false}) {
bool abbreviated = false,
int minNumberMonths = 2}) {
final String fmt = prettyDuration(duration,
tersity: tersity,
locale: locale,
spacer: spacer,
delimiter: delimiter,
conjunction: conjugation,
abbreviated: abbreviated);
abbreviated: abbreviated,
minNumberMonths: minNumberMonths);
print(fmt);
return fmt;
}
4 changes: 4 additions & 0 deletions lib/src/time_const.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ Duration hours(int hours) => Duration(hours: hours);

// Creates a [Duration] with given [days]
Duration days(int days) => Duration(days: days);

// Average number of days per month (365 / 12)
// Obviously a bit off on leap years.
const double avgDaysPerMonth = 30.42;
23 changes: 23 additions & 0 deletions test/pretty_duration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,19 @@ void main() {
final dur = Duration(days: 14);
expect(prettyDuration(dur), '2 weeks');
}

{
final dur = Duration(days: 60);
expect(prettyDuration(dur), '8 weeks 4 days');
}
{
final dur = Duration(days: 61);
expect(prettyDuration(dur), '2 months 1 day');
}
{
final dur = Duration(days: 60);
expect(prettyDuration(dur, minNumberMonths: 1), '1 month 30 days');
}
});

test('Abbreviated', () {
Expand Down Expand Up @@ -204,6 +217,16 @@ void main() {
final dur = Duration(days: 14);
expect(prettyDuration(dur, abbreviated: true), '2w');
}

{
final dur = Duration(days: 60);
expect(prettyDuration(dur, abbreviated: true), '8w, 4d');
}

{
final dur = Duration(days: 61);
expect(prettyDuration(dur, abbreviated: true), '2mon, 1d');
}
});

test('Delimiter', () {
Expand Down