Skip to content
This repository was archived by the owner on Oct 13, 2025. It is now read-only.
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
2 changes: 1 addition & 1 deletion .dart_tool/pub/bin/sdk-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.0-dev.32.0
2.1.0-dev.1.0.flutter-ccb16f7282
Binary file removed .dart_tool/pub/bin/test/test.dart.snapshot
Binary file not shown.
Binary file added .dart_tool/pub/bin/test/test.dart.snapshot.dart2
Binary file not shown.
14 changes: 14 additions & 0 deletions lib/date_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class Utils {
static String fullDayFormat(DateTime d) => _fullDayFormat.format(d);
static String apiDayFormat(DateTime d) => _apiDayFormat.format(d);

static bool _isMondayFirstDayOfWeek = false;

static const List<String> weekdays = const [
"Sun",
"Mon",
Expand Down Expand Up @@ -49,11 +51,19 @@ class Utils {
return new DateTime(month.year, month.month);
}

static void setIsMondayFirstDayOfWeek(bool isMonday) {
_isMondayFirstDayOfWeek = isMonday;
}

static DateTime firstDayOfWeek(DateTime day) {
/// Handle Daylight Savings by setting hour to 12:00 Noon
/// rather than the default of Midnight
day = new DateTime.utc(day.year, day.month, day.day, 12);

if (_isMondayFirstDayOfWeek) {
return day.subtract(new Duration(days: day.weekday - 1));
}

/// Weekday is on a 1-7 scale Monday - Sunday,
/// This Calendar works from Sunday - Monday
var decreaseNum = day.weekday % 7;
Expand All @@ -65,6 +75,10 @@ class Utils {
/// rather than the default of Midnight
day = new DateTime.utc(day.year, day.month, day.day, 12);

if (_isMondayFirstDayOfWeek) {
return day.add(new Duration(days: day.weekday - 1));
}

/// Weekday is on a 1-7 scale Monday - Sunday,
/// This Calendar's Week starts on Sunday
var increaseNum = day.weekday % 7;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ dependencies:
intl: ^0.15.0

dev_dependencies:
test: ^0.12.0
test: ^1.3.0
13 changes: 11 additions & 2 deletions test/date_utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,21 @@ void main() {
var lastDayOfCurrentWeek = Utils.lastDayOfWeek(today);

expect(
Utils
.daysInRange(firstDayOfCurrentWeek, lastDayOfCurrentWeek)
Utils.daysInRange(firstDayOfCurrentWeek, lastDayOfCurrentWeek)
.toList()
.length,
7);
});
}
test('setWhichIsFirstDayOfWeek', () {
var date = new DateTime(2018, 9, 6);
Utils.setIsMondayFirstDayOfWeek(true);

var firstDayOfWeek = new DateTime(2018, 9, 3);
var lastDayOfWeek = new DateTime(2018, 9, 9);

expect(Utils.firstDayOfWeek(date).day, firstDayOfWeek.day);
expect(Utils.lastDayOfWeek(date).day, lastDayOfWeek.day);
});
});
}