Skip to content

Commit

Permalink
First and Last days extensions added #51
Browse files Browse the repository at this point in the history
  • Loading branch information
jogboms authored May 28, 2022
2 parents d11e087 + 23ec82b commit 6ec373d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
20 changes: 20 additions & 0 deletions lib/src/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,26 @@ extension DateTimeTimeExtension on DateTime {
microsecond ?? this.microsecond,
);
}

/// Returns the Monday of this week
DateTime get firstDayOfWeek =>
isUtc ? DateTime.utc(year, month, day + 1 - weekday) : DateTime(year, month, day + 1 - weekday);

/// Returns the Sunday of this week
DateTime get lastDayOfWeek =>
isUtc ? DateTime.utc(year, month, day + 7 - weekday) : DateTime(year, month, day + 7 - weekday);

/// Returns the first day of this month
DateTime get firstDayOfMonth => isUtc ? DateTime.utc(year, month, 1) : DateTime(year, month, 1);

/// Returns the last day of this month (considers leap years)
DateTime get lastDayOfMonth => isUtc ? DateTime.utc(year, month + 1, 0) : DateTime(year, month + 1, 0);

/// Returns the first day of this year
DateTime get firstDayOfYear => isUtc ? DateTime.utc(year, 1, 1) : DateTime(year, 1, 1);

/// Returns the last day of this year
DateTime get lastDayOfYear => isUtc ? DateTime.utc(year, 12, 31) : DateTime(year, 12, 31);
}

extension DurationTimeExtension on Duration {
Expand Down
54 changes: 53 additions & 1 deletion test/time_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,58 @@ void main() {
expect(later.isUtc, isTrue);
});
});

group('can get first/last days', () {
test('first day of week', () {
final initial = DateTime(2022, 5, 20);
final expected = DateTime(2022, 5, 16);
expect(initial.firstDayOfWeek, expected);
});

test('last day of week', () {
final initial = DateTime(2022, 5, 20);
final expected = DateTime(2022, 5, 22);
expect(initial.lastDayOfWeek, expected);
});

test('first day of month', () {
final initial = DateTime(2022, 5, 20);
final expected = DateTime(2022, 5, 1);
expect(initial.firstDayOfMonth, expected);
});

group('last day of month', (){
test('last day of month', () {
final initial = DateTime(2022, 5, 20);
final expected = DateTime(2022, 5, 31);
expect(initial.lastDayOfMonth, expected);
});
test('february not leap year', () {
final initial = DateTime(2022, 2, 20);
final expected = DateTime(2022, 2, 28);
expect(initial.lastDayOfMonth, expected);
});

test('february leap year', () {
final initial = DateTime(2020, 2, 20);
final expected = DateTime(2020, 2, 29);
expect(initial.lastDayOfMonth, expected);
});

});

test('first day of year', () {
final initial = DateTime(2022, 5, 20);
final expected = DateTime(2022, 1, 1);
expect(initial.firstDayOfYear, expected);
});

test('last day of year', () {
final initial = DateTime(2022, 5, 20);
final expected = DateTime(2022, 12, 31);
expect(initial.lastDayOfYear, expected);
});
});
});
});

Expand Down Expand Up @@ -564,4 +616,4 @@ void main() {
expect(extraTime >= 0, true);
});
});
}
}

0 comments on commit 6ec373d

Please sign in to comment.