Ticket #40653: current-time2.patch
File current-time2.patch, 3.8 KB (added by , 6 years ago) |
---|
-
src/wp-includes/functions.php
IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8
49 49 * Retrieve the current time based on specified type. 50 50 * 51 51 * The 'mysql' type will return the time in the format for MySQL DATETIME field. 52 * The 'timestamp' type will return the current timestamp .52 * The 'timestamp' type will return the current timestamp or a sum of timestamp and timezone offset, depending on `$gmt`. 53 53 * Other strings will be interpreted as PHP date formats (e.g. 'Y-m-d'). 54 54 * 55 55 * If $gmt is set to either '1' or 'true', then both types will use GMT time. … … 60 60 * @param string $type Type of time to retrieve. Accepts 'mysql', 'timestamp', or PHP date 61 61 * format string (e.g. 'Y-m-d'). 62 62 * @param int|bool $gmt Optional. Whether to use GMT timezone. Default false. 63 * 63 64 * @return int|string Integer if $type is 'timestamp', string otherwise. 64 65 */ 65 66 function current_time( $type, $gmt = 0 ) { 66 switch ( $type ) { 67 case 'mysql': 68 return ( $gmt ) ? gmdate( 'Y-m-d H:i:s' ) : gmdate( 'Y-m-d H:i:s', ( time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ) ); 69 case 'timestamp': 70 return ( $gmt ) ? time() : time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ); 71 default: 72 return ( $gmt ) ? gmdate( $type ) : gmdate( $type, time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ); 67 68 // Don't use non-gmt timestamp, unless you know the difference and really need to. 69 if ( 'timestamp' === $type || 'U' === $type ) { 70 return $gmt ? time() : time() + (int) ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ); 73 71 } 72 73 if ( 'mysql' === $type ) { 74 $type = 'Y-m-d H:i:s'; 75 } 76 77 $timezone = $gmt ? new DateTimeZone( 'UTC' ) : wp_timezone(); 78 $datetime = new DateTime( 'now', $timezone ); 79 80 return $datetime->format( $type ); 74 81 } 75 82 76 83 /** -
tests/phpunit/tests/date/currentTime.php
IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8
21 21 $this->assertEquals( gmdate( $format ), current_time( $format, true ) ); 22 22 $this->assertEquals( $datetime->format( $format ), current_time( $format ) ); 23 23 } 24 25 public function test_should_return_wp_timestamp() { 26 27 update_option( 'timezone_string', 'Europe/Kiev' ); 28 $timestamp = time(); 29 $datetime = new DateTime( '@' . $timestamp ); 30 $datetime->setTimezone( wp_timezone() ); 31 $wp_timestamp = $timestamp + $datetime->getOffset(); 32 33 $this->assertEquals( $timestamp, current_time( 'timestamp', true ), '', 2 ); 34 $this->assertEquals( $timestamp, current_time( 'U', true ), '', 2 ); 35 $this->assertEquals( $wp_timestamp, current_time( 'timestamp' ), '', 2 ); 36 $this->assertEquals( $wp_timestamp, current_time( 'U' ), '', 2 ); 37 $this->assertInternalType( 'int', current_time( 'timestamp' ) ); 38 } 39 40 public function test_should_return_correct_local_time() { 41 42 update_option( 'timezone_string', 'Europe/Kiev' ); 43 $timestamp = time(); 44 $datetime_local = new DateTime( '@' . $timestamp ); 45 $datetime_local->setTimezone( wp_timezone() ); 46 $datetime_utc = new DateTime( '@' . $timestamp ); 47 $datetime_utc->setTimezone( new DateTimeZone( 'UTC' ) ); 48 49 $this->assertEquals( $datetime_local->format( DATE_W3C ), current_time( DATE_W3C ), '', 2 ); 50 $this->assertEquals( $datetime_utc->format( DATE_W3C ), current_time( DATE_W3C, true ), '', 2 ); 51 } 24 52 }