From e1b8084f08221f29b59f6c5003e37e28f86f2d2f Mon Sep 17 00:00:00 2001 From: Luke Carbis Date: Thu, 27 Mar 2014 12:20:31 +1000 Subject: [PATCH 1/4] Issue #353: Date Ranges should honour timezone settings. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The problem here is that the date picker uses the date of the visitor’s computer. This means that we have to take the site GMT offset, and add the local computer’s GMT offset, and then compare the current date with the offset date. --- includes/admin.php | 1 + ui/admin.js | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/includes/admin.php b/includes/admin.php index 3122dce94..c3f766d2c 100644 --- a/includes/admin.php +++ b/includes/admin.php @@ -171,6 +171,7 @@ public static function admin_enqueue_scripts( $hook ) { 'confirm_purge' => __( 'Are you sure you want to delete all Stream activity records from the database? This cannot be undone.', 'stream' ), 'confirm_uninstall' => __( 'Are you sure you want to uninstall and deactivate Stream? This will delete all Stream tables from the database and cannot be undone.', 'stream' ), ), + 'gmt_offset' => get_option('gmt_offset'), 'current_screen' => $hook, 'current_page' => isset( $_GET['paged'] ) ? esc_js( $_GET['paged'] ) : '1', 'current_order' => isset( $_GET['order'] ) ? esc_js( $_GET['order'] ) : 'desc', diff --git a/ui/admin.js b/ui/admin.js index f24f6220e..e15d64346 100644 --- a/ui/admin.js +++ b/ui/admin.js @@ -440,9 +440,27 @@ jQuery(function($){ if ( jQuery.datepicker ) { + // Apply a GMT offset due to Date() using the visitor's local time + var siteGMTOffsetHours = parseFloat( wp_stream.gmt_offset ); + var localGMTOffsetHours = new Date().getTimezoneOffset() / 60 * -1; + var totalGMTOffsetHours = siteGMTOffsetHours - localGMTOffsetHours; + + var localTime = new Date(); + var siteTime = new Date( localTime.getTime() + ( totalGMTOffsetHours * 60 * 60 * 1000 ) ); + var dayOffset = '0'; + + // check if the site date is different from the local date, and set a day offset + if ( localTime.getDate() != siteTime.getDate() || localTime.getMonth() != siteTime.getMonth() ) { + if ( localTime.getTime() < siteTime.getTime() ) { + dayOffset = '+1d'; + } else { + dayOffset = '-1d'; + } + } + datepickers.datepicker({ dateFormat: 'yy/mm/dd', - maxDate: 0, + maxDate: dayOffset, beforeShow: function() { $(this).prop( 'disabled', true ); }, From 7bcce503e8f4d60068e811b4b70438991a31ecb3 Mon Sep 17 00:00:00 2001 From: Luke Carbis Date: Thu, 27 Mar 2014 12:30:16 +1000 Subject: [PATCH 2/4] Fix JS Lint error --- ui/admin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ui/admin.js b/ui/admin.js index e15d64346..f022d00d9 100644 --- a/ui/admin.js +++ b/ui/admin.js @@ -450,7 +450,7 @@ jQuery(function($){ var dayOffset = '0'; // check if the site date is different from the local date, and set a day offset - if ( localTime.getDate() != siteTime.getDate() || localTime.getMonth() != siteTime.getMonth() ) { + if ( localTime.getDate() !== siteTime.getDate() || localTime.getMonth() !== siteTime.getMonth() ) { if ( localTime.getTime() < siteTime.getTime() ) { dayOffset = '+1d'; } else { From e8131747b6606b61fcbd07a07f0b7f4f048c7dcf Mon Sep 17 00:00:00 2001 From: Luke Carbis Date: Thu, 27 Mar 2014 17:33:49 -0700 Subject: [PATCH 3/4] Fixed dropdown menu dates --- includes/admin.php | 2 +- includes/date-interval.php | 49 ++++++++++++++++++++------------------ ui/admin.js | 1 + 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/includes/admin.php b/includes/admin.php index c3f766d2c..30dc19c6d 100644 --- a/includes/admin.php +++ b/includes/admin.php @@ -171,7 +171,7 @@ public static function admin_enqueue_scripts( $hook ) { 'confirm_purge' => __( 'Are you sure you want to delete all Stream activity records from the database? This cannot be undone.', 'stream' ), 'confirm_uninstall' => __( 'Are you sure you want to uninstall and deactivate Stream? This will delete all Stream tables from the database and cannot be undone.', 'stream' ), ), - 'gmt_offset' => get_option('gmt_offset'), + 'gmt_offset' => get_option( 'gmt_offset' ), 'current_screen' => $hook, 'current_page' => isset( $_GET['paged'] ) ? esc_js( $_GET['paged'] ) : '1', 'current_order' => isset( $_GET['order'] ) ? esc_js( $_GET['order'] ) : 'desc', diff --git a/includes/date-interval.php b/includes/date-interval.php index 8d3be01d0..64d07ad84 100644 --- a/includes/date-interval.php +++ b/includes/date-interval.php @@ -31,67 +31,70 @@ public function __construct() { * @return mixed|void */ public function get_predefined_intervals() { + $gmt_offset = get_option( 'gmt_offset' ); return apply_filters( 'wp_stream_predefined_date_intervals', array( 'today' => array( 'label' => esc_html__( 'Today', 'stream' ), - 'start' => Carbon::today(), + 'start' => Carbon::today()->addHours( $gmt_offset ), + 'end' => Carbon::today()->addHours( $gmt_offset ), ), 'yesterday' => array( 'label' => esc_html__( 'Yesterday', 'stream' ), - 'start' => Carbon::today()->subDay(), - 'end' => Carbon::today()->subSecond(), + 'start' => Carbon::today()->addHours( $gmt_offset )->subDay(), + 'end' => Carbon::today()->addHours( $gmt_offset )->subSecond(), ), 'last-7-days' => array( 'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 7 ), - 'start' => Carbon::today()->subDays( 7 ), - 'end' => Carbon::today(), + 'start' => Carbon::today()->addHours( $gmt_offset )->subDays( 7 ), + 'end' => Carbon::today()->addHours( $gmt_offset ), ), 'last-14-days' => array( 'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 14 ), - 'start' => Carbon::today()->subDays( 14 ), - 'end' => Carbon::today(), + 'start' => Carbon::today()->addHours( $gmt_offset )->subDays( 14 ), + 'end' => Carbon::today()->addHours( $gmt_offset ), ), 'last-30-days' => array( 'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 30 ), - 'start' => Carbon::today()->subDays( 30 ), - 'end' => Carbon::today(), + 'start' => Carbon::today()->addHours( $gmt_offset )->subDays( 30 ), + 'end' => Carbon::today()->addHours( $gmt_offset ), ), 'this-month' => array( 'label' => esc_html__( 'This Month', 'stream' ), - 'start' => Carbon::today()->day( 1 ), + 'start' => Carbon::today()->addHours( $gmt_offset )->day( 1 ), ), 'last-month' => array( 'label' => esc_html__( 'Last Month', 'stream' ), - 'start' => Carbon::today()->day( 1 )->subMonth(), - 'end' => Carbon::today()->day( 1 )->subSecond(), + 'start' => Carbon::today()->addHours( $gmt_offset )->day( 1 )->subMonth(), + 'end' => Carbon::today()->addHours( $gmt_offset )->day( 1 )->subSecond(), ), 'last-3-months' => array( 'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 3 ), - 'start' => Carbon::today()->subMonths( 3 ), - 'end' => Carbon::today(), + 'start' => Carbon::today()->addHours( $gmt_offset )->subMonths( 3 ), + 'end' => Carbon::today()->addHours( $gmt_offset ), ), 'last-6-months' => array( 'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 6 ), - 'start' => Carbon::today()->subMonths( 6 ), - 'end' => Carbon::today(), + 'start' => Carbon::today()->addHours( $gmt_offset )->subMonths( 6 ), + 'end' => Carbon::today()->addHours( $gmt_offset ), ), 'last-12-months' => array( 'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 12 ), - 'start' => Carbon::today()->subMonths( 12 ), - 'end' => Carbon::today(), + 'start' => Carbon::today()->addHours( $gmt_offset )->subMonths( 12 ), + 'end' => Carbon::today()->addHours( $gmt_offset ), ), 'this-year' => array( 'label' => esc_html__( 'This Year', 'stream' ), - 'start' => Carbon::today()->day( 1 )->month( 1 ), + 'start' => Carbon::today()->addHours( $gmt_offset )->day( 1 )->month( 1 ), ), 'last-year' => array( 'label' => esc_html__( 'Last Year', 'stream' ), - 'start' => Carbon::today()->day( 1 )->month( 1 )->subYear(), - 'end' => Carbon::today()->day( 1 )->month( 1 )->subSecond(), - ) - ) + 'start' => Carbon::today()->addHours( $gmt_offset )->day( 1 )->month( 1 )->subYear(), + 'end' => Carbon::today()->addHours( $gmt_offset )->day( 1 )->month( 1 )->subSecond(), + ), + ), + $gmt_offset ); } diff --git a/ui/admin.js b/ui/admin.js index f022d00d9..e78a3cd73 100644 --- a/ui/admin.js +++ b/ui/admin.js @@ -461,6 +461,7 @@ jQuery(function($){ datepickers.datepicker({ dateFormat: 'yy/mm/dd', maxDate: dayOffset, + defaultDate: siteTime, beforeShow: function() { $(this).prop( 'disabled', true ); }, From 921f80be0f2df600ae99bb02c25db57b3d27ef87 Mon Sep 17 00:00:00 2001 From: Luke Carbis Date: Thu, 27 Mar 2014 19:41:41 -0700 Subject: [PATCH 4/4] Better timezone support for Carbon --- includes/date-interval.php | 60 +++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/includes/date-interval.php b/includes/date-interval.php index 64d07ad84..3663ac413 100644 --- a/includes/date-interval.php +++ b/includes/date-interval.php @@ -31,70 +31,82 @@ public function __construct() { * @return mixed|void */ public function get_predefined_intervals() { - $gmt_offset = get_option( 'gmt_offset' ); + $timezone = get_option( 'timezone_string' ); + + if ( empty( $timezone ) ) { + $gmt_offset = (int) get_option( 'gmt_offset' ); + $timezone = timezone_name_from_abbr( null, $gmt_offset * 3600, true ); + if ( false === $timezone ) { + $timezone = timezone_name_from_abbr( null, $gmt_offset * 3600, false ); + } + if ( false === $timezone ) { + $timezone = null; + } + } + return apply_filters( 'wp_stream_predefined_date_intervals', array( 'today' => array( 'label' => esc_html__( 'Today', 'stream' ), - 'start' => Carbon::today()->addHours( $gmt_offset ), - 'end' => Carbon::today()->addHours( $gmt_offset ), + 'start' => Carbon::today( $timezone )->startOfDay(), + 'end' => Carbon::today( $timezone )->startOfDay(), ), 'yesterday' => array( 'label' => esc_html__( 'Yesterday', 'stream' ), - 'start' => Carbon::today()->addHours( $gmt_offset )->subDay(), - 'end' => Carbon::today()->addHours( $gmt_offset )->subSecond(), + 'start' => Carbon::today( $timezone )->startOfDay()->subDay(), + 'end' => Carbon::today( $timezone )->startOfDay()->subSecond(), ), 'last-7-days' => array( 'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 7 ), - 'start' => Carbon::today()->addHours( $gmt_offset )->subDays( 7 ), - 'end' => Carbon::today()->addHours( $gmt_offset ), + 'start' => Carbon::today( $timezone )->subDays( 7 ), + 'end' => Carbon::today( $timezone ), ), 'last-14-days' => array( 'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 14 ), - 'start' => Carbon::today()->addHours( $gmt_offset )->subDays( 14 ), - 'end' => Carbon::today()->addHours( $gmt_offset ), + 'start' => Carbon::today( $timezone )->subDays( 14 ), + 'end' => Carbon::today( $timezone ), ), 'last-30-days' => array( 'label' => sprintf( esc_html__( 'Last %d Days', 'stream' ), 30 ), - 'start' => Carbon::today()->addHours( $gmt_offset )->subDays( 30 ), - 'end' => Carbon::today()->addHours( $gmt_offset ), + 'start' => Carbon::today( $timezone )->subDays( 30 ), + 'end' => Carbon::today( $timezone ), ), 'this-month' => array( 'label' => esc_html__( 'This Month', 'stream' ), - 'start' => Carbon::today()->addHours( $gmt_offset )->day( 1 ), + 'start' => Carbon::today( $timezone )->day( 1 ), ), 'last-month' => array( 'label' => esc_html__( 'Last Month', 'stream' ), - 'start' => Carbon::today()->addHours( $gmt_offset )->day( 1 )->subMonth(), - 'end' => Carbon::today()->addHours( $gmt_offset )->day( 1 )->subSecond(), + 'start' => Carbon::today( $timezone )->day( 1 )->subMonth(), + 'end' => Carbon::today( $timezone )->day( 1 )->subSecond(), ), 'last-3-months' => array( 'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 3 ), - 'start' => Carbon::today()->addHours( $gmt_offset )->subMonths( 3 ), - 'end' => Carbon::today()->addHours( $gmt_offset ), + 'start' => Carbon::today( $timezone )->subMonths( 3 ), + 'end' => Carbon::today( $timezone ), ), 'last-6-months' => array( 'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 6 ), - 'start' => Carbon::today()->addHours( $gmt_offset )->subMonths( 6 ), - 'end' => Carbon::today()->addHours( $gmt_offset ), + 'start' => Carbon::today( $timezone )->subMonths( 6 ), + 'end' => Carbon::today( $timezone ), ), 'last-12-months' => array( 'label' => sprintf( esc_html__( 'Last %d Months', 'stream' ), 12 ), - 'start' => Carbon::today()->addHours( $gmt_offset )->subMonths( 12 ), - 'end' => Carbon::today()->addHours( $gmt_offset ), + 'start' => Carbon::today( $timezone )->subMonths( 12 ), + 'end' => Carbon::today( $timezone ), ), 'this-year' => array( 'label' => esc_html__( 'This Year', 'stream' ), - 'start' => Carbon::today()->addHours( $gmt_offset )->day( 1 )->month( 1 ), + 'start' => Carbon::today( $timezone )->day( 1 )->month( 1 ), ), 'last-year' => array( 'label' => esc_html__( 'Last Year', 'stream' ), - 'start' => Carbon::today()->addHours( $gmt_offset )->day( 1 )->month( 1 )->subYear(), - 'end' => Carbon::today()->addHours( $gmt_offset )->day( 1 )->month( 1 )->subSecond(), + 'start' => Carbon::today( $timezone )->day( 1 )->month( 1 )->subYear(), + 'end' => Carbon::today( $timezone )->day( 1 )->month( 1 )->subSecond(), ), ), - $gmt_offset + $timezone ); }