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

First and Last days extensions added #51

Merged
merged 5 commits into from
May 28, 2022
Merged
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 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);
});
});
}
}