* remove previous year week alignment, include full day at end of previous period

* update script testing of weeks in last year
This commit is contained in:
Ron Rennick 2019-11-25 09:19:19 -04:00 committed by GitHub
parent 53e3009c4b
commit c5f0fa90b2
2 changed files with 10 additions and 29 deletions

View File

@ -152,15 +152,8 @@ export function getLastPeriod( period, compare ) {
secondaryStart = secondaryEnd.clone().subtract( daysDiff, 'days' );
}
} else {
secondaryStart =
'week' === period
? primaryStart
.clone()
.subtract( 1, 'years' )
.week( primaryStart.week() )
.startOf( 'week' )
: primaryStart.clone().subtract( 1, 'years' );
secondaryEnd = secondaryStart.clone().endOf( period );
secondaryStart = primaryStart.clone().subtract( 1, 'years' );
secondaryEnd = primaryEnd.clone().subtract( 1, 'years' );
}
return {
primaryStart,
@ -189,15 +182,9 @@ export function getCurrentPeriod( period, compare ) {
secondaryStart = primaryStart.clone().subtract( 1, period );
secondaryEnd = primaryEnd.clone().subtract( 1, period );
} else {
secondaryStart =
'week' === period
? primaryStart
.clone()
.subtract( 1, 'years' )
.week( primaryStart.week() )
.startOf( 'week' )
: primaryStart.clone().subtract( 1, 'years' );
secondaryEnd = secondaryStart.clone().add( daysSoFar, 'days' );
secondaryStart = primaryStart.clone().subtract( 1, 'years' );
// Set the end time to 23:59:59.
secondaryEnd = secondaryStart.clone().add( daysSoFar + 1, 'days' ).subtract( 1, 'seconds' );
}
return {
primaryStart,

View File

@ -213,11 +213,8 @@ describe( 'getCurrentPeriod', () => {
it( 'should return correct values for previous_year', () => {
const dateValue = getCurrentPeriod( 'week', 'previous_year' );
const daysSoFar = today.diff( thisWeekStart, 'days' );
const thisWeekLastYearStart = thisWeekStart
.clone()
.subtract( 1, 'years' )
.week( thisWeekStart.week() )
.startOf( 'week' );
// Last year weeks are aligned by calendar date not day of week.
const thisWeekLastYearStart = thisWeekStart.clone().subtract( 1, 'years' );
const todayThisWeekLastYear = thisWeekLastYearStart.clone().add( daysSoFar, 'days' );
expect( thisWeekLastYearStart.isSame( dateValue.secondaryStart, 'day' ) ).toBe( true );
@ -394,12 +391,9 @@ describe( 'getLastPeriod', () => {
it( 'should return correct values for previous_year', () => {
const dateValue = getLastPeriod( 'week', 'previous_year' );
const lastWeekLastYearStart = lastWeekStart
.clone()
.subtract( 1, 'year' )
.week( lastWeekStart.week() )
.startOf( 'week' );
const lastWeekLastYearEnd = lastWeekLastYearStart.clone().endOf( 'week' );
// Last year weeks are aligned by calendar date not day of week.
const lastWeekLastYearStart = lastWeekStart.clone().subtract( 1, 'year' );
const lastWeekLastYearEnd = lastWeekEnd.clone().subtract( 1, 'year' );
expect( lastWeekLastYearStart.isSame( dateValue.secondaryStart, 'day' ) ).toBe( true );
expect( lastWeekLastYearEnd.isSame( dateValue.secondaryEnd, 'day' ) ).toBe( true );