Skip to content

Commit

Permalink
Store modification times in UTC instead of local time
Browse files Browse the repository at this point in the history
  • Loading branch information
sheabunge committed Jan 25, 2020
1 parent 2620aba commit 6be5040
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
15 changes: 11 additions & 4 deletions php/class-snippet.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ private function prepare_modified( $modified ) {

/* if the supplied value is probably a timestamp, attempt to convert it to a string */
if ( is_numeric( $modified ) ) {
return date( self::DATE_FORMAT, $modified );
return gmdate( self::DATE_FORMAT, $modified );
}

/* if the supplied value is a string, check it is not just the default value */
Expand All @@ -348,11 +348,18 @@ private function prepare_modified( $modified ) {
return null;
}

/**
* Retrieve the current date and time in MySQL format.
*/
public static function current_date() {
return gmdate( Code_Snippet::DATE_FORMAT );
}

/**
* Update the last modification date to the current time.
*/
public function update_modified() {
$this->modified = date( Code_Snippet::DATE_FORMAT );
$this->modified = self::current_date();
}

/**
Expand Down Expand Up @@ -428,7 +435,7 @@ private function get_shared_network() {
* @return int Timestamp value.
*/
private function get_modified_timestamp() {
$datetime = DateTime::createFromFormat( self::DATE_FORMAT, $this->modified );
$datetime = DateTime::createFromFormat( self::DATE_FORMAT, $this->modified, new DateTimeZone( 'UTC' ) );
return $datetime ? $datetime->getTimestamp() : 0;
}

Expand Down Expand Up @@ -457,7 +464,7 @@ private function get_modified_local() {
$timezone = new DateTimeZone( $timezone );
}

$datetime = DateTime::createFromFormat( self::DATE_FORMAT, $this->modified );
$datetime = DateTime::createFromFormat( self::DATE_FORMAT, $this->modified, new DateTimeZone( 'UTC' ) );
$datetime->setTimezone( $timezone );
return $datetime;
}
Expand Down
6 changes: 3 additions & 3 deletions php/snippet-ops.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ function activate_snippet( $id, $multisite = null ) {

$wpdb->update(
$table,
array( 'active' => '1', 'modified' => date( Code_Snippet::DATE_FORMAT ) ),
array( 'active' => '1', 'modified' => Code_Snippet::current_date() ),
array( 'id' => $id ),
array( '%d', '%s' ),
array( '%d' )
Expand Down Expand Up @@ -271,7 +271,7 @@ function activate_snippets( array $ids, $multisite = null ) {
$ids_format = implode( ',', array_fill( 0, count( $valid_ids ), '%d' ) );
$sql = sprintf( 'UPDATE %s SET active = 1 AND modified = %%s WHERE id IN (%s);', $table, $ids_format );

array_unshift( $valid_ids, date( Code_Snippet::DATE_FORMAT ) );
array_unshift( $valid_ids, Code_Snippet::current_date() );
$wpdb->query( $wpdb->prepare( $sql, $valid_ids ) );
array_shift( $valid_ids );

Expand Down Expand Up @@ -305,7 +305,7 @@ function deactivate_snippet( $id, $multisite = null ) {

$wpdb->update(
$table,
array( 'active' => '0', 'modified' => date( Code_Snippet::DATE_FORMAT ) ),
array( 'active' => '0', 'modified' => Code_Snippet::current_date() ),
array( 'id' => $id ),
array( '%d', '%s' ),
array( '%d' )
Expand Down

0 comments on commit 6be5040

Please sign in to comment.