Dates: Account for leat year in `getLastPeriod` (https://github.com/woocommerce/woocommerce-admin/pull/6470)

* force end of month

* changelog
This commit is contained in:
Paul Sealock 2021-03-02 09:40:26 +13:00 committed by GitHub
parent 630f31ae9d
commit 077eb4d4f8
3 changed files with 12 additions and 2 deletions

View File

@ -1,3 +1,7 @@
# Unreleased
- Take into account leap year in calculating `getLastPeriod`.
# 2.1.0
- Update to @wordpress/eslint coding standards.

View File

@ -148,6 +148,7 @@ export function getLastPeriod( period, compare ) {
secondaryEnd = secondaryStart.clone().endOf( period );
} else {
// Otherwise, use days in primary period to figure out how far to go back
// This is necessary for calculating weeks instead of using `endOf`.
const daysDiff = primaryEnd.diff( primaryStart, 'days' );
secondaryEnd = primaryStart.clone().subtract( 1, 'days' );
secondaryStart = secondaryEnd.clone().subtract( daysDiff, 'days' );
@ -156,6 +157,12 @@ export function getLastPeriod( period, compare ) {
secondaryStart = primaryStart.clone().subtract( 1, 'years' );
secondaryEnd = primaryEnd.clone().subtract( 1, 'years' );
}
// When the period is month, be sure to force end of month to take into account leap year
if ( period === 'month' ) {
secondaryEnd = secondaryEnd.clone().endOf( 'month' );
}
return {
primaryStart,
primaryEnd,

View File

@ -552,7 +552,7 @@ describe( 'getLastPeriod', () => {
).toBe( true );
} );
it.skip( 'should return correct values for previous_year', () => {
it( 'should return correct values for previous_year', () => {
const dateValue = getLastPeriod( 'month', 'previous_year' );
const lastMonthkLastYearStart = lastMonthStart
@ -561,7 +561,6 @@ describe( 'getLastPeriod', () => {
const lastMonthkLastYearEnd = lastMonthkLastYearStart
.clone()
.endOf( 'month' );
expect(
lastMonthkLastYearStart.isSame(
dateValue.secondaryStart,