<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[45908] trunk: Date/Time: Revamp `mysql2date()` to use `wp_date()` and handle invalid input in a consistent manner.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/45908">45908</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/45908","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>SergeyBiryukov</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2019-08-29 05:06:15 +0000 (Thu, 29 Aug 2019)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Date/Time: Revamp `mysql2date()` to use `wp_date()` and handle invalid input in a consistent manner.

Add unit tests, improve documentation.

Props Rarst, pbearne.
Fixes <a href="https://core.trac.wordpress.org/ticket/28992">#28992</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesfunctionsphp">trunk/src/wp-includes/functions.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsdatemysql2datephp">trunk/tests/phpunit/tests/date/mysql2date.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesfunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/functions.php       2019-08-29 02:39:21 UTC (rev 45907)
+++ trunk/src/wp-includes/functions.php 2019-08-29 05:06:15 UTC (rev 45908)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -8,20 +8,24 @@
</span><span class="cx" style="display: block; padding: 0 10px"> require( ABSPATH . WPINC . '/option.php' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Convert given date string into a different format.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Convert given MySQL date string into a different format.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * $format should be either a PHP date format string, e.g. 'U' for a Unix
- * timestamp, or 'G' for a Unix timestamp assuming that $date is GMT.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * `$format` should be a PHP date format string.
+ * 'U' and 'G' formats will return a sum of timestamp with timezone offset.
+ * `$date` is expected to be local time in MySQL format (`Y-m-d H:i:s`).
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * If $translate is true then the given date and format string will
- * be passed to date_i18n() for translation.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Historically UTC time could be passed to the function to produce Unix timestamp.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * If `$translate` is true then the given date and format string will
+ * be passed to `wp_date()` for translation.
+ *
</ins><span class="cx" style="display: block; padding: 0 10px">  * @since 0.71
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @param string $format    Format of the date to return.
</span><span class="cx" style="display: block; padding: 0 10px">  * @param string $date      Date string to convert.
</span><span class="cx" style="display: block; padding: 0 10px">  * @param bool   $translate Whether the return date should be translated. Default true.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @return string|int|bool Formatted date string or Unix timestamp. False if $date is empty.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @return string|int|false Formatted date string or sum of Unix timestamp and timezone offset.
+ *                          False on failure.
</ins><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function mysql2date( $format, $date, $translate = true ) {
</span><span class="cx" style="display: block; padding: 0 10px">        if ( empty( $date ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -28,21 +32,22 @@
</span><span class="cx" style="display: block; padding: 0 10px">                return false;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( 'G' == $format ) {
-               return strtotime( $date . ' +0000' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $datetime = date_create( $date, wp_timezone() );
+
+       if ( false === $datetime ) {
+               return false;
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $i = strtotime( $date );
-
-       if ( 'U' == $format ) {
-               return $i;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Returns a sum of timestamp with timezone offset. Ideally should never be used.
+       if ( 'G' === $format || 'U' === $format ) {
+               return $datetime->getTimestamp() + $datetime->getOffset();
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        if ( $translate ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                return date_i18n( $format, $i );
-       } else {
-               return gmdate( $format, $i );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         return wp_date( $format, $datetime->getTimestamp() );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       return $datetime->format( $format );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -58,8 +63,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 1.0.0
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param string   $type Type of time to retrieve. Accepts 'mysql', 'timestamp', or PHP date
- *                       format string (e.g. 'Y-m-d').
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param string   $type Type of time to retrieve. Accepts 'mysql', 'timestamp',
+ *                       or PHP date format string (e.g. 'Y-m-d').
</ins><span class="cx" style="display: block; padding: 0 10px">  * @param int|bool $gmt  Optional. Whether to use GMT timezone. Default false.
</span><span class="cx" style="display: block; padding: 0 10px">  * @return int|string Integer if $type is 'timestamp', string otherwise.
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -151,10 +156,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @global WP_Locale $wp_locale WordPress date and time locale object.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @param string   $format                Format to display the date.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param int|bool $timestamp_with_offset Optional. A sum of Unix timestamp and timezone offset in seconds.
- *                                        Default false.
- * @param bool     $gmt                   Optional. Whether to use GMT timezone. Only applies if timestamp is
- *                                        not provided. Default false.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param int|bool $timestamp_with_offset Optional. A sum of Unix timestamp and timezone offset
+ *                                        in seconds. Default false.
+ * @param bool     $gmt                   Optional. Whether to use GMT timezone. Only applies
+ *                                        if timestamp is not provided. Default false.
</ins><span class="cx" style="display: block; padding: 0 10px">  * @return string The date, translated if locale specifies it.
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function date_i18n( $format, $timestamp_with_offset = false, $gmt = false ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -204,14 +209,15 @@
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * This is a newer function, intended to replace `date_i18n()` without legacy quirks in it.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Note that, unlike `date_i18n()`, this function accepts a true Unix timestamp, not summed with timezone offset.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Note that, unlike `date_i18n()`, this function accepts a true Unix timestamp, not summed
+ * with timezone offset.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 5.3.0
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @param string       $format    PHP date format.
</span><span class="cx" style="display: block; padding: 0 10px">  * @param int          $timestamp Optional. Unix timestamp. Defaults to current time.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param DateTimeZone $timezone  Optional. Timezone to output result in. Defaults to timezone from site settings.
- *
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param DateTimeZone $timezone  Optional. Timezone to output result in. Defaults to timezone
+ *                                from site settings.
</ins><span class="cx" style="display: block; padding: 0 10px">  * @return string The date, translated if locale specifies it.
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function wp_date( $format, $timestamp = null, $timezone = null ) {
</span></span></pre></div>
<a id="trunktestsphpunittestsdatemysql2datephp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/date/mysql2date.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/date/mysql2date.php                             (rev 0)
+++ trunk/tests/phpunit/tests/date/mysql2date.php       2019-08-29 05:06:15 UTC (rev 45908)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,71 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * @group date
+ * @group datetime
+ */
+class Tests_Date_mysql2date extends WP_UnitTestCase {
+
+       function tearDown() {
+               date_default_timezone_set( 'UTC' );
+
+               parent::tearDown();
+       }
+
+       /**
+        * @ticket 28992
+        */
+       function test_mysql2date_should_format_time() {
+               $timezone = 'Europe/Kiev';
+               update_option( 'timezone_string', $timezone );
+               $datetime = new DateTime( 'now', new DateTimeZone( $timezone ) );
+               $rfc3339  = $datetime->format( DATE_RFC3339 );
+               $mysql    = $datetime->format( 'Y-m-d H:i:s' );
+
+               $this->assertEquals( $rfc3339, mysql2date( DATE_RFC3339, $mysql ) );
+               $this->assertEquals( $rfc3339, mysql2date( DATE_RFC3339, $mysql, false ) );
+       }
+
+       /**
+        * @ticket 28992
+        */
+       function test_mysql2date_should_format_time_with_changed_time_zone() {
+               $timezone = 'Europe/Kiev';
+               date_default_timezone_set( $timezone );
+               update_option( 'timezone_string', $timezone );
+               $datetime = new DateTime( 'now', new DateTimeZone( $timezone ) );
+               $rfc3339  = $datetime->format( DATE_RFC3339 );
+               $mysql    = $datetime->format( 'Y-m-d H:i:s' );
+
+               $this->assertEquals( $rfc3339, mysql2date( DATE_RFC3339, $mysql ) );
+               $this->assertEquals( $rfc3339, mysql2date( DATE_RFC3339, $mysql, false ) );
+       }
+
+       /**
+        * @ticket 28992
+        */
+       function test_mysql2date_should_return_wp_timestamp() {
+               $timezone = 'Europe/Kiev';
+               update_option( 'timezone_string', $timezone );
+               $datetime     = new DateTime( 'now', new DateTimeZone( $timezone ) );
+               $wp_timestamp = $datetime->getTimestamp() + $datetime->getOffset();
+               $mysql        = $datetime->format( 'Y-m-d H:i:s' );
+
+               $this->assertEquals( $wp_timestamp, mysql2date( 'U', $mysql, false ) );
+               $this->assertEquals( $wp_timestamp, mysql2date( 'G', $mysql, false ) );
+       }
+
+       /**
+        * @ticket 28992
+        */
+       function test_mysql2date_should_return_unix_timestamp_for_gmt_time() {
+               $timezone = 'Europe/Kiev';
+               update_option( 'timezone_string', $timezone );
+               $datetime  = new DateTime( 'now', new DateTimeZone( 'UTC' ) );
+               $timestamp = $datetime->getTimestamp();
+               $mysql     = $datetime->format( 'Y-m-d H:i:s' );
+
+               $this->assertEquals( $timestamp, mysql2date( 'U', $mysql, false ) );
+               $this->assertEquals( $timestamp, mysql2date( 'G', $mysql, false ) );
+       }
+}
</ins></span></pre>
</div>
</div>

</body>
</html>