jama5262 / jiffy

Jiffy is a Flutter (Android, IOS and Web) date time package for parsing, manipulating, querying and formatting dates
https://pub.dev/packages/jiffy
MIT License
576 stars 124 forks source link

startOf(Units.WEEK) is incorrect for the week of daylight savings #223

Open claude opened 1 year ago

claude commented 1 year ago

Describe the bug

I noticed that we were getting inaccurate results from weekOf for the week when daylight savings changed just recently. Usually we get the previous Sunday for any date during the week, but for some reason we were getting the previous Saturday instead, only last week though.

How to reproduce the bug

import 'package:flutter_test/flutter_test.dart';
import 'package:jiffy/jiffy.dart';

void main() {
  group('Jiffy - ', () {
    test('startOf 2023-03-07', () {
      final startOf = Jiffy.parse('2023-03-07')
          .startOf(Unit.week)
          .format(pattern: 'MMM dd');
      expect(startOf, 'Mar 05');
    });

    test('startOf 2023-03-12', () {
      final startOf = Jiffy.parse('2023-03-12')
          .startOf(Unit.week)
          .format(pattern: 'MMM dd');
      expect(startOf, 'Mar 12');
    });

    test('startOf 2023-03-13', () {
      final startOf = Jiffy.parse('2023-03-13')
          .startOf(Unit.week)
          .format(pattern: 'MMM dd');
      expect(startOf, 'Mar 12');
    });

    test('startOf 2023-03-14', () {
      final startOf = Jiffy.parse('2023-03-14')
          .startOf(Unit.week)
          .format(pattern: 'MMM dd');
      expect(startOf, 'Mar 12');
    });

    test('startOf 2023-03-18', () {
      final startOf = Jiffy.parse('2023-03-18')
          .startOf(Unit.week)
          .format(pattern: 'MMM dd');
      expect(startOf, 'Mar 12');
    });

    test('startOf 2023-03-20', () {
      final startOf = Jiffy.parse('2023-03-20')
          .startOf(Unit.week)
          .format(pattern: 'MMM dd');
      expect(startOf, 'Mar 19');
    });
  });
}

here's the result:

✓ Jiffy -  startOf 2023-03-07
✓ Jiffy -  startOf 2023-03-12
Expected: 'Mar 12'
  Actual: 'Mar 11'
   Which: is different.
          Expected: Mar 12
            Actual: Mar 11
                         ^
           Differ at offset 5

✖ Jiffy -  startOf 2023-03-13
Expected: 'Mar 12'
  Actual: 'Mar 11'
   Which: is different.
          Expected: Mar 12
            Actual: Mar 11
                         ^
           Differ at offset 5

✖ Jiffy -  startOf 2023-03-14
Expected: 'Mar 12'
  Actual: 'Mar 11'
   Which: is different.
          Expected: Mar 12
            Actual: Mar 11
                         ^
           Differ at offset 5

✖ Jiffy -  startOf 2023-03-18
✓ Jiffy -  startOf 2023-03-20

What is the expected behavior

I'd expect all of those tests to pass. If you notice, the only date that gets the correct start week for the week of the 3/12 is on 3/12 itself, that returns 3/12 as expected, but all other dates that week return 3/11 instead.

Tested using Jiffy version 6.1.0