Skip to content

Commit

Permalink
[fixup] time offset
Browse files Browse the repository at this point in the history
  • Loading branch information
kkarpieszuk committed Jan 15, 2023
1 parent a028032 commit c8d656e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
22 changes: 18 additions & 4 deletions app/PriorPrice/HistoryStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ public function get_minimal( int $product_id, int $days = 30 ) : float {

$history = $this->get_history( $product_id );

$this_ = $this;

// Get only $days last items.
$the_last = array_filter(
$history,
static function( $timestamp ) use ( $days ) {
return $timestamp >= ( time() - ( $days * DAY_IN_SECONDS ) );
static function( $timestamp ) use ( $days, $this_ ) {
return $timestamp >= ( $this_->get_time_with_offset() - ( $days * DAY_IN_SECONDS ) );
},
ARRAY_FILTER_USE_KEY
);
Expand Down Expand Up @@ -106,7 +108,7 @@ public function add_price( int $product_id, float $price, bool $on_change_only )
return 0;
}

$history[ time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ] = $price;
$history[ $this->get_time_with_offset() ] = $price;

return $this->save_history( $product_id, $history );
}
Expand Down Expand Up @@ -161,7 +163,7 @@ public function get_history( int $product_id ) : array {
private function fill_empty_history( int $product_id, array $history ) : array {

if ( empty( $history ) ) {
$history[ time() ] = get_post_meta( $product_id, '_price', true );
$history[ $this->get_time_with_offset() ] = get_post_meta( $product_id, '_price', true );

$this->save_history( $product_id, $history );
}
Expand Down Expand Up @@ -206,4 +208,16 @@ static function( $carry, $item ) {
}
);
}

/**
* Get time with offset.
*
* @since 1.6.1
*
* @return int
*/
private function get_time_with_offset() : int {

return time() + ( (int) get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
}
}
5 changes: 5 additions & 0 deletions tests/phpunit/HistoryStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public function test_get_minimal( $history, $expected_minimal ) {
'return' => $history
] );

\WP_Mock::userFunction( 'get_option', [
'args' => [ 'gmt_offset' ],
'return' => 0
] );


$minimal = $subject->get_minimal( $product_id, 30 );

Expand Down

0 comments on commit c8d656e

Please sign in to comment.