Skip to content

Commit

Permalink
Throw an exception when the date cannot be parsed
Browse files Browse the repository at this point in the history
  • Loading branch information
choval committed May 6, 2020
1 parent 2ad0898 commit 25918c6
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 64 deletions.
3 changes: 3 additions & 0 deletions src/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ public static function datetimeParse($time='now', $timezone=null) : PhpDateTime
$obj = date_create( $time );
}
}
if ($obj === false) {
throw new \RuntimeException('Not a valid date');
}
return $obj;
}

Expand Down
135 changes: 71 additions & 64 deletions tests/0-ParsersTest.php
Original file line number Diff line number Diff line change
@@ -1,84 +1,91 @@
<?php

use Choval\DateTime;
use DateTime as PhpDateTime;
use PHPUnit\Framework\TestCase;
use React\EventLoop\Factory;
use React\Promise\Deferred;
use React\Promise;

use DateTime as PhpDateTime;
use Choval\DateTime;

class ParsersTest extends TestCase {



/**
* Parses timezone parsing
*/
public function testTimezoneParse() {
$rows = [
'America/Argentina/Buenos_Aires',
'-0300',
'-03:00',
-3,
'-3',
];
$offset = -10800;
$date = new PhpDateTime;
date_default_timezone_set($rows[0]);
foreach($rows as $row ) {
$tz = DateTime::timezoneParse( $row );
$this->assertInstanceOf( DateTimeZone::class, $tz );
$this->assertEquals( $offset, $tz->getOffset( $date ) );
class ParsersTest extends TestCase
{
/**
* Parses timezone parsing
*/
public function testTimezoneParse()
{
$rows = [
'America/Argentina/Buenos_Aires',
'-0300',
'-03:00',
-3,
'-3',
];
$offset = -10800;
$date = new PhpDateTime;
date_default_timezone_set($rows[0]);
foreach($rows as $row ) {
$tz = DateTime::timezoneParse( $row );
$this->assertInstanceOf( DateTimeZone::class, $tz );
$this->assertEquals( $offset, $tz->getOffset( $date ) );
}
}
}



/**
* Performs datetime parsing
*/
public function testDatetimeParse() {
$timestamp = 946684800;
$rows = [
$timestamp,
'2000-01-01T00:00:00+00:00',
'2000-01-01 00:00:00 UTC',
'01/01/2000 UTC',
];

$php_obj = new PhpDateTime;
$php_obj->setTimestamp( $timestamp );
$formatted = $php_obj->format('c');

$tz = $php_obj->getTimezone();
foreach($rows as $row) {
$obj = DateTime::datetimeParse( $row , $tz);
$obj->setTimezone($tz);
$this->assertInstanceOf( PhpDateTime::class, $obj );
$this->assertEquals( $formatted, $obj->format('c') );
/**
* Performs datetime parsing
*/
public function testDatetimeParse()
{
$timestamp = 946684800;
$rows = [
$timestamp,
'2000-01-01T00:00:00+00:00',
'2000-01-01 00:00:00 UTC',
'01/01/2000 UTC',
];

$php_obj = new PhpDateTime;
$php_obj->setTimestamp( $timestamp );
$formatted = $php_obj->format('c');

$tz = $php_obj->getTimezone();
foreach($rows as $row) {
$obj = DateTime::datetimeParse( $row , $tz);
$obj->setTimezone($tz);
$this->assertInstanceOf( PhpDateTime::class, $obj );
$this->assertEquals( $formatted, $obj->format('c') );
}
}
}



/**
* Performs interval parsing
*/
public function testIntervalParse() {
$rows = [
'P10DT1H',
'+10 days, 1 hour',
];
/**
* Performs interval parsing
*/
public function testIntervalParse()
{
$rows = [
'P10DT1H',
'+10 days, 1 hour',
];

foreach($rows as $row) {
$obj = DateTime::intervalParse( $row );
$this->assertInstanceOf( DateInterval::class, $obj );
$this->assertEquals( '00-00-10 01:00:00', $obj->format('%Y-%M-%D %H:%I:%S') );
foreach($rows as $row) {
$obj = DateTime::intervalParse( $row );
$this->assertInstanceOf( DateInterval::class, $obj );
$this->assertEquals( '00-00-10 01:00:00', $obj->format('%Y-%M-%D %H:%I:%S') );
}
}
}



/**
* Test failed parse
*/
public function testFailedParse()
{
$this->expectException( \RuntimeException::class );
$obj = new DateTime('non valid');
$out = $obj->format('Y-m-d');
}
}

0 comments on commit 25918c6

Please sign in to comment.