diff --git a/.dart_tool/pub/bin/sdk-version b/.dart_tool/pub/bin/sdk-version index c380242..07203f2 100644 --- a/.dart_tool/pub/bin/sdk-version +++ b/.dart_tool/pub/bin/sdk-version @@ -1 +1 @@ -2.0.0-dev.32.0 +2.1.0-dev.1.0.flutter-ccb16f7282 diff --git a/.dart_tool/pub/bin/test/test.dart.snapshot b/.dart_tool/pub/bin/test/test.dart.snapshot deleted file mode 100644 index 6b07ff3..0000000 Binary files a/.dart_tool/pub/bin/test/test.dart.snapshot and /dev/null differ diff --git a/.dart_tool/pub/bin/test/test.dart.snapshot.dart2 b/.dart_tool/pub/bin/test/test.dart.snapshot.dart2 new file mode 100644 index 0000000..5d6784b Binary files /dev/null and b/.dart_tool/pub/bin/test/test.dart.snapshot.dart2 differ diff --git a/lib/date_utils.dart b/lib/date_utils.dart index bd4ab35..dd067aa 100644 --- a/lib/date_utils.dart +++ b/lib/date_utils.dart @@ -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 weekdays = const [ "Sun", "Mon", @@ -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; @@ -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; diff --git a/pubspec.yaml b/pubspec.yaml index e0b99fe..67bfbad 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -13,4 +13,4 @@ dependencies: intl: ^0.15.0 dev_dependencies: - test: ^0.12.0 + test: ^1.3.0 diff --git a/test/date_utils_test.dart b/test/date_utils_test.dart index 375329c..8a85e08 100644 --- a/test/date_utils_test.dart +++ b/test/date_utils_test.dart @@ -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); + }); }); }