Last active
July 30, 2016 14:15
-
-
Save sirbrillig/9a6fa335fd368031b8e76edec4d582ac to your computer and use it in GitHub Desktop.
Convert a string timezone offset to a formatted string timezone offset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use PHPUnit\Framework\TestCase; | |
class MyTime { | |
/** | |
* Convert a float string timezone offset to a DateTimeZone | |
* | |
* @param string $number The initial offset float, as a string. | |
* @return DateTimeZone The converted DateTimeZone | |
*/ | |
public function get_timezone_from_string( $time_string ) { | |
$offset = $this->get_time_offset_from_number( $time_string ); | |
return new DateTimeZone( 'Etc/GMT' . $offset ); | |
} | |
/** | |
* Convert a string timezone offset to a formatted string timezone offset | |
* | |
* Converts the form '-6.5' to '-6:30' | |
* | |
* @param string $number The initial offset. | |
* @return string The formatted offset | |
*/ | |
public function get_time_offset_from_number( $number ) { | |
$offset = $number * 1; // type conversion: string to int/float | |
if ( ! $offset ) { | |
return null; | |
} | |
if ( is_int( $offset ) ) { | |
return $number; | |
} | |
$hours = $offset < 0 ? '-' . abs( (int) $offset ) : '+' . (int) $offset; | |
$minutes = abs( fmod( $offset, 1 ) ) * 60; | |
return $hours . ':' . $minutes; | |
} | |
} | |
class MyTimeTest extends TestCase { | |
public function testGetZoneWithNegative() { | |
$time = new MyTime(); | |
$zone = $time->get_timezone_from_string( '-6' ); | |
$this->assertEquals( new DateTimeZone( 'Etc/GMT-6' ), $zone ); | |
} | |
public function testGetOffsetWithNegative() { | |
$time = new MyTime(); | |
$offset = $time->get_time_offset_from_number( '-6' ); | |
$this->assertEquals( '-6', $offset ); | |
} | |
public function testGetZoneWithPositive() { | |
$time = new MyTime(); | |
$zone = $time->get_timezone_from_string( '+6' ); | |
$this->assertEquals( new DateTimeZone( 'Etc/GMT+6' ), $zone ); | |
} | |
public function testGetOffsetWithPositive() { | |
$time = new MyTime(); | |
$offset = $time->get_time_offset_from_number( '+6' ); | |
$this->assertEquals( '+6', $offset ); | |
} | |
public function testGetOffsetWithFraction() { | |
$time = new MyTime(); | |
$offset = $time->get_time_offset_from_number( '+6.5' ); | |
$this->assertEquals( '+6:30', $offset ); | |
} | |
public function testGetOffsetWithNegativeFraction() { | |
$time = new MyTime(); | |
$offset = $time->get_time_offset_from_number( '-6.5' ); | |
$this->assertEquals( '-6:30', $offset ); | |
} | |
public function testGetOffsetWithZero() { | |
$time = new MyTime(); | |
$offset = $time->get_time_offset_from_number( '0' ); | |
$this->assertEquals( null, $offset ); | |
} | |
public function testGetZoneWithZero() { | |
$time = new MyTime(); | |
$zone = $time->get_timezone_from_string( '0' ); | |
$this->assertEquals( new DateTimeZone( 'Etc/GMT' ), $zone ); | |
} | |
public function testGetOffsetWithPositiveZero() { | |
$time = new MyTime(); | |
$offset = $time->get_time_offset_from_number( '+0' ); | |
$this->assertEquals( null, $offset ); | |
} | |
public function testGetOffsetWithEmptyString() { | |
$time = new MyTime(); | |
$offset = $time->get_time_offset_from_number( '' ); | |
$this->assertEquals( null, $offset ); | |
} | |
public function testGetOffsetWithSmallFraction() { | |
$time = new MyTime(); | |
$offset = $time->get_time_offset_from_number( '0.5' ); | |
$this->assertEquals( '+0:30', $offset ); | |
} | |
public function testGetOffsetWithNegativeSmallFraction() { | |
$time = new MyTime(); | |
$offset = $time->get_time_offset_from_number( '-0.5' ); | |
$this->assertEquals( '-0:30', $offset ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment