From f2212e39359067c2784d430ef557358c6c43fbb8 Mon Sep 17 00:00:00 2001 From: coffeetalkh Date: Fri, 17 Jun 2016 03:42:18 +0430 Subject: [PATCH] Change TimeAgo class design pattern, clean codes and remote temporary events class files --- README.md | 38 +---- composer.json | 2 +- src/CalendarSettings/Hijri.php | 20 +-- src/CalendarSettings/Jalali.php | 40 +++-- src/Config.php | 29 +--- src/Datium.php | 98 ++++++------ src/Events/Events.php | 172 --------------------- src/Events/Global/global.php | 34 ----- src/Events/Localization/ir.php | 20 --- src/Events/Localization/us.php | 18 --- src/Events/iEvents.php | 11 -- src/Lang.php | 7 +- src/TimeAgo.php | 191 ++++++++++++----------- src/config/ago.php | 53 +++++++ src/lang/en/general.php | 23 +-- tests/ConvertTest.php | 6 +- tests/DatiumTest.php | 263 ++++++++++++++++---------------- tests/DayOfTest.php | 4 +- tests/EventsTest.php | 31 ---- tests/LeapTest.php | 4 +- tests/TimeAgoTest.php | 142 ++++++++--------- 21 files changed, 488 insertions(+), 718 deletions(-) delete mode 100644 src/Events/Events.php delete mode 100644 src/Events/Global/global.php delete mode 100644 src/Events/Localization/ir.php delete mode 100644 src/Events/Localization/us.php delete mode 100644 src/Events/iEvents.php create mode 100644 src/config/ago.php delete mode 100644 tests/EventsTest.php diff --git a/README.md b/README.md index e73fc1b..e066d5d 100644 --- a/README.md +++ b/README.md @@ -135,44 +135,10 @@ $diff = Datium::diff( ); echo $diff->days; - // output => 5000 +echo $diff->year . ' year, ' . $diff->month . ' month, ' . $diff->day . ' day '; +// ouput => 13 year, 8 month, 7 day -dd ( $diff ); - -/** - output => object(DateInterval)#3 (15) { - ["y"]=> - int(13) - ["m"]=> - int(8) - ["d"]=> - int(7) - ["h"]=> - int(0) - ["i"]=> - int(0) - ["s"]=> - int(0) - ["weekday"]=> - int(0) - ["weekday_behavior"]=> - int(0) - ["first_last_day_of"]=> - int(0) - ["invert"]=> - int(0) - ["days"]=> - int(5000) - ["special_type"]=> - int(0) - ["special_amount"]=> - int(0) - ["have_weekday_relative"]=> - int(0) - ["have_special_relative"]=> - int(0) -} */ ``` ## Leap year diff --git a/composer.json b/composer.json index c125b71..3ff9643 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ ], "autoload": { "psr-4": { - "OpenCafe\\": [ "src/", "src/Events/" ], + "OpenCafe\\" : "src/", "OpenCafe\\Tools\\": "src/" } } diff --git a/src/CalendarSettings/Hijri.php b/src/CalendarSettings/Hijri.php index f175ccc..ed0083b 100644 --- a/src/CalendarSettings/Hijri.php +++ b/src/CalendarSettings/Hijri.php @@ -279,16 +279,16 @@ 'leap_year' => function ($year) { $result = $this->year % 30; - if (( 2 == $result ) - || ( 5 == $result ) - || ( 7 == $result ) - || ( 10 == $result ) - || ( 13 == $result ) - || ( 16 == $result ) - || ( 18 == $result ) - || ( 21 == $result ) - || ( 24 == $year ) - || ( 26 == $result ) + if (( 2 == $result ) + || ( 5 == $result ) + || ( 7 == $result ) + || ( 10 == $result ) + || ( 13 == $result ) + || ( 16 == $result ) + || ( 18 == $result ) + || ( 21 == $result ) + || ( 24 == $year ) + || ( 26 == $result ) || ( 29 == $result ) ) { return $result; diff --git a/src/CalendarSettings/Jalali.php b/src/CalendarSettings/Jalali.php index 86fc2e1..14de871 100644 --- a/src/CalendarSettings/Jalali.php +++ b/src/CalendarSettings/Jalali.php @@ -296,7 +296,14 @@ */ 'day_of_week' => function ($date_time) { - $days = array( 1 => 'Shanbe', 2 => 'Yekshanbe', 3 => 'Doshanbe', 4 => 'Seshanbe', 5 => 'Chaharshanbe', 6 => 'Panjshanbe', 7 => 'Jome' ); + $days = array( + 1 => 'Shanbe', + 2 => 'Yekshanbe', + 3 => 'Doshanbe', + 4 => 'Seshanbe', + 5 => 'Chaharshanbe', + 6 => 'Panjshanbe', + 7 => 'Jome' ); $configShamsi = include 'Jalali.php'; @@ -304,13 +311,17 @@ $day = $date_time->format('l'); - $day = str_replace($configGregorian[ 'days_of_week' ], $configShamsi[ 'days_of_week' ], $day); + $day = str_replace( + $configGregorian[ 'days_of_week' ], + $configShamsi[ 'days_of_week' ], + $day + ); - foreach ($days as $key => $value) { - if ($day == $value) { - return $key; + foreach ($days as $key => $value) { + if ($day == $value) { + return $key; + } } - } }, @@ -322,16 +333,18 @@ * *\_________________________________________________________/ */ - 'leap_year' => function ( $year ) { + 'leap_year' => function ($year) { $a = 0.025; $b = 266; if ($year > 0) { - $leapDays0 = (($year + 38) % 2820)*0.24219 + $a; // 0.24219 ~ extra days of one year - $leapDays1 = (($year + 39) % 2820)*0.24219 + $a; // 38 days is the difference of epoch to 2820-year cycle - } elseif($year < 0 ) { + $leapDays0 = (($year + 38) % 2820)*0.24219 + $a; + // 0.24219 ~ extra days of one year + $leapDays1 = (($year + 39) % 2820)*0.24219 + $a; + // 38 days is the difference of epoch to 2820-year cycle + } elseif ($year < 0) { $leapDays0 = (($year + 39) % 2820)*0.24219 + $a; $leapDays1 = (($year + 40) % 2820)*0.24219 + $a; } else { @@ -342,10 +355,9 @@ $frac1 = intval(($leapDays1 - intval($leapDays1))*1000); if ($frac0 <= $b && $frac1 > $b) { - return true; - } - else { - return false; + return true; + } else { + return false; } }, diff --git a/src/Config.php b/src/Config.php index d8e1286..ce3beff 100644 --- a/src/Config.php +++ b/src/Config.php @@ -1,43 +1,28 @@ 'Asia/Tehran', - /************************************************************ - * Default Language - ************************************************************ - * + /** * Convert algorith to convert Jalali to Gregorian calerndar - * - *\_________________________________________________________/ */ 'language' => 'en', - /************************************************************ - * Default calendar - ************************************************************ - * + /** * Convert algorith to convert Jalali to Gregorian calerndar - * - *\_________________________________________________________/ */ 'default_calendar' => 'gregorian', - 'date_interval' => array( 'D', 'M', 'Y', 'HT', 'MT', 'ST' ), + 'date_interval' => [ 'D', 'M', 'Y', 'HT', 'MT', 'ST' ], - 'date_simple' => array( 'day', ' month', ' year', ' hour', ' minute', ' second' ), + 'date_simple' => [ 'day', ' month', ' year', ' hour', ' minute', ' second' ], - ); +]; diff --git a/src/Datium.php b/src/Datium.php index a71ee13..25e9e2d 100644 --- a/src/Datium.php +++ b/src/Datium.php @@ -5,7 +5,7 @@ * @category Core * @package OpenCafe\Datium * @author Mehdi Hosseini - * @license icense https://opensource.org/licenses/MIT + * @license License https://opensource.org/licenses/MIT * @link https://github.com/opencafe/datium * @since Aug 17, 2015 */ @@ -20,8 +20,6 @@ use OpenCafe\Tools\Lang; use OpenCafe\Tools\TimeAgo; -use OpenCafe\Datium; - /** * Main Datium class * @@ -120,36 +118,36 @@ public function __construct() $this->calendar_type = 'gregorian'; switch (Datium::$call_type) { - case 'now': - $this->date_time = new DateTime('now'); + case 'now': + $this->date_time = new DateTime('now'); - $this->gregorian_DayofWeek = $this->date_time->format('w'); + $this->gregorian_DayofWeek = $this->date_time->format('w'); - break; + break; - case 'make': - $this->date_time = new DateTime('now'); + case 'make': + $this->date_time = new DateTime('now'); - $this->date_time->setDate( - self::$array_date[ 'year' ], - self::$array_date[ 'month' ], - self::$array_date[ 'day' ] - ); + $this->date_time->setDate( + self::$array_date[ 'year' ], + self::$array_date[ 'month' ], + self::$array_date[ 'day' ] + ); - $this->date_time->setTime( - self::$array_date[ 'hour' ], - self::$array_date[ 'minute' ], - self::$array_date[ 'second' ] - ); + $this->date_time->setTime( + self::$array_date[ 'hour' ], + self::$array_date[ 'minute' ], + self::$array_date[ 'second' ] + ); - $this->gregorian_DayofWeek = $this->date_time->format('w'); + $this->gregorian_DayofWeek = $this->date_time->format('w'); - break; + break; - case 'set': - $this->date_time = Datium::$static_date_time; + case 'set': + $this->date_time = Datium::$static_date_time; - $this->gregorian_DayofWeek = $this->date_time->format('w'); + $this->gregorian_DayofWeek = $this->date_time->format('w'); } $this->convert_calendar = new Convert(); @@ -326,7 +324,21 @@ public function to($calendar) public static function diff($start, $end) { - return date_diff($start, $end); + $difference = date_diff($start, $end); + + $difference->second = $difference->s; + + $difference->minute = $difference->i; + + $difference->hour = $difference->h; + + $difference->day = $difference->d; + + $difference->month = $difference->m; + + $difference->year = $difference->y; + + return $difference; } @@ -348,16 +360,14 @@ public function add($value) $unit = 'P'; - if( strpos($this->date_interval_expression, 'T') ) { - - $this->date_interval_expression= str_replace( - 'T', - '', - $this->date_interval_expression - ); - - $unit = 'PT'; + if (strpos($this->date_interval_expression, 'T')) { + $this->date_interval_expression= str_replace( + 'T', + '', + $this->date_interval_expression + ); + $unit = 'PT'; } $this->date_interval_expression = str_replace( @@ -392,16 +402,14 @@ public function sub($value) $unit = 'P'; - if( strpos($this->date_interval_expression, 'T') ) { - - $this->date_interval_expression= str_replace( - 'T', - '', - $this->date_interval_expression - ); - - $unit = 'PT'; + if (strpos($this->date_interval_expression, 'T')) { + $this->date_interval_expression= str_replace( + 'T', + '', + $this->date_interval_expression + ); + $unit = 'PT'; } $this->date_interval_expression = str_replace( @@ -442,9 +450,9 @@ public function leap() public function ago() { - $this->ago = new TimeAgo( $this->date_time, $this->language ); + $this->ago = new TimeAgo($this->date_time, $this->language); - return $this->ago->get(); + return $this->ago; } diff --git a/src/Events/Events.php b/src/Events/Events.php deleted file mode 100644 index e4972b7..0000000 --- a/src/Events/Events.php +++ /dev/null @@ -1,172 +0,0 @@ -convert = new Convert; - - $this->date_time = new DateTime(); - - return $this; - - } - - /************************************************************ - * Fetch Events array from own array file - ************************************************************ - * - * @since Oct 25, 2015 - * - *\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ - */ - private function fetch($path) - { - - $this->events = include $path; - - $this->interval = DateInterval::createFromDateString('1 day'); - - $this->period = new DatePeriod(Events::$date_start, $this->interval, Events::$date_end); - - foreach ($this->period as $dt) { - if (isset($this->events[ 'events' ][ intval($dt->format('m')) ][ intval($dt->format('d')) ])) { - $this->result[ $dt->format('Y-m-d') ][] = $this->events[ 'events' ][ intval($dt->format('m')) ][ intval($dt->format('d')) ]; - } - } - - } - - /************************************************************ - * Return Array of Events - ************************************************************ - * - * @since Oct 18, 2015 - * @return array - * - *\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ - */ - public function get() - { - - if (! empty($this->result)) { - foreach ($this->result as $key => $day) { - if (! ( $key > Events::$date_start->format('Y-m-d') && $key < Events::$date_end->format('Y-m-d') )) { - unset($this->result[ $key ]); - } - } - } else { - $this->result = array(); - } - - return $this->result; - - } - - /************************************************************ - * Return local events - with day start and end as an array - ************************************************************ - * - * @since Oct 10, 2015 - * @param string $country_code - * - *\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ - */ - public function local($country_code = "ir") - { - - /* - * Capitalize the first character of $country_code according the file - * structure. - */ - $country_code = strtolower($country_code = 'ir'); - - $this->local = include 'Localization/' . $country_code . '.php'; - - foreach ($this->local[ 'events' ] as $month => $events) { - foreach ($events as $day => $event) { - $this->date_time = new DateTime(); - - $this->date_time->setDate(Events::$date_start->format('Y'), $month, $day); - - switch ($this->local[ 'default_calendar' ]) { - case 'jalali': - $this->date_time->setDate(1394, $month, $day); - - $this->date_time = Datium::create($this->date_time)->from('jalali')->to('gregorian')->object(); //$this->convert->jalaliToGregorian( $this->date_time ); - - break; - - case 'hijri': - $this->date_time = Datium::create($this->date_time)->from('hijri')->to('gregorian')->object(); // $this->convert->hijriToGregorian( $this->date_time ); - - break; - } - - $this->result[ $this->date_time->format('Y-m-d') ][] = $event; - } - } - - ksort($this->result); - - return $this; - - } - - public function weekend() - { - - return 0; - - } - - public function relagious() - { - - return 0; - - } - - public function international() - { - - $this->fetch('Global/global.php'); - - return $this; - - } -} diff --git a/src/Events/Global/global.php b/src/Events/Global/global.php deleted file mode 100644 index fae802c..0000000 --- a/src/Events/Global/global.php +++ /dev/null @@ -1,34 +0,0 @@ - 'gregorian', - - /** - * International days should typed as a Gregorian calendar dates and set as - * as a arraye value, data provided by united nations website - * - * @link http://www.un.org/en/sections/observances/international-days/ - */ - 'events' => array( - 1 => array( 27 => 'International Day of Commemoration in Memory of the Victims of the Holocaust' ), - 2 => array( - 4 => 'World Cancer Day', - 6 => 'International Day of Zero Tolerance to Female Genital Mutilation', - 13 => 'World Radio Day', - 20 => 'World Day of Social Justice', - 21 => 'International Mother Language Day', - ), - 3 => array( - 1 => 'Zero Discrimination Day', - 3 => 'World Wildlife Day', - 8 => 'International Women\'s Day', - 20 => 'International Day of Happiness', - 21 => 'International Day for the Elimination of Racial Discrimination' - ) - ), - -); diff --git a/src/Events/Localization/ir.php b/src/Events/Localization/ir.php deleted file mode 100644 index 9b77c6a..0000000 --- a/src/Events/Localization/ir.php +++ /dev/null @@ -1,20 +0,0 @@ - 'jalali', - - /** - * Iran Events should typed as a Jalali calendar dates and set as - * as a arraye value - */ - 'events' => array( - 1 => array( 1 => 'nowruz', 2 => 'nowruz', 3 => 'nowruz', 5 => 'nowruz' ), - 10 => array( 22 => 'Iran revelution day' ), - 12 => array( 29 => 'Iran oil national day' ) - ), - -); diff --git a/src/Events/Localization/us.php b/src/Events/Localization/us.php deleted file mode 100644 index e3f3d16..0000000 --- a/src/Events/Localization/us.php +++ /dev/null @@ -1,18 +0,0 @@ - 'gregorian', - - 'events' => array( - - 4 => array( 22 => 'Earth day' ), - - 12 => array( 25 => 'Christmas' ) - - ) - -); diff --git a/src/Events/iEvents.php b/src/Events/iEvents.php deleted file mode 100644 index e44b464..0000000 --- a/src/Events/iEvents.php +++ /dev/null @@ -1,11 +0,0 @@ -language = $lang; + /** + * TimeAgo Class constructure + * + * @param DateTime $time_difference The time to calculate with now + * @param string $lang Language + */ + public function __construct($time_difference, $lang ) + { - $time_ago = strtotime($timestamp->format('Y-m-d H:i:s')); + $this->language = $lang; - $cur_time = time(); + $now = Datium::now()->object(); - $time_elapsed = $cur_time - strtotime($timestamp->format('Y-m-d H:i:s')); + $this->output = Datium::diff( $now, $time_difference ); - $this->second = $time_elapsed ; + $this->year = $this->output->year; - $this->minute = round($time_elapsed / 60 ); + $this->month = $this->output->month; - $this->quarter = round($time_elapsed / 900 ); + $this->day = $this->output->day; - $this->half = round($time_elapsed / 1800 ); + $this->hour = $this->output->hour; - $this->hour = round($time_elapsed / 3600); + $this->minute = $this->output->minute; - $this->day = round($time_elapsed / 86400 ); + $this->second = $this->output->second; - $this->week = round($time_elapsed / 604800); + } - $this->month = round($time_elapsed / 2600640 ); + /** + * Return difference period as an object + * + * @return object + */ + public function all() + { - $this->year = round($time_elapsed / 31207680 ); + return $result->output; - } + } - public function get() { + /** + * Read ago config file + * + * @param integer $date date index in ago config file + * @param string $type duration type on ago config file + */ + public function read( $value, $type ) + { + + $config = include 'config/ago.php'; + + if( array_key_exists( $value, $config[ $type ] ) ) { + + return Lang::get( $this->language, $config[ $type ][ $value ] ); + + } else { + + return $value . " " . Lang::get( $this->language, $config[ $type ][ '*' ] ); + + } - // Seconds - if($this->second <= 60) { - return Lang::get($this->language, "just-now" ); - } - //Minutes - else if($this->minute <=60){ - if($this->minute==1){ - return Lang::get($this->language, 'one-minute') . " " . - Lang::get($this->language, 'ago'); - } - else{ - return "$this->minute " . Lang::get($this->language, 'minutes') - . " " . - Lang::get($this->language, 'ago'); - } - } - //Hours - else if($this->hour <=24){ - if($this->hour==1){ - return Lang::get($this->language, 'an-hour') . " " . - Lang::get($this->language, 'ago'); - }else{ - return "$this->hour " . Lang::get($this->language, 'hours') - . " " . - Lang::get($this->language, 'ago'); - } - } - //Days - else if($this->day <= 7){ - if($this->day==1){ - return Lang::get($this->language, 'yesterday'); - }else{ - return "$this->day " . Lang::get($this->language, 'days') - . " " . - Lang::get($this->language, 'ago'); - } - } - //Weeks - else if($this->week <= 4.3){ - if($this->week==1){ - return Lang::get($this->language, 'a-week') . " " . - Lang::get($this->language, 'ago'); - }else{ - return "$this->week " . Lang::get($this->language, 'weeks') - . " " . - Lang::get($this->language, 'ago'); - } } - //Months - else if($this->month <=12){ - if($this->month==1){ - return Lang::get($this->language, 'a-month') . " " . - Lang::get($this->language, 'ago'); - }else{ - return "$this->month " . Lang::get($this->language, 'months') - . " " . - Lang::get($this->language, 'ago'); - } + + /** + * Show priority of duration + * + * @param integer $date date index in ago config file + * @param string $type duration type on ago config file + */ + public function priority( $date, $type ) + { + + if( $date != 0 ) { + + return $this->read( $date, $type ); + + } else { + + return false; + + } + } - //Years - else{ - if($this->year==1){ - return Lang::get($this->language, 'one-year') . " " . - Lang::get($this->language, 'ago'); - }else{ - return "$this->year " . Lang::get($this->language, 'years') - . " " . - Lang::get($this->language, 'ago'); + + /** + * Return fainal TimeAgo result + * + * @return string + */ + public function get() + { + + $duration = [ + 'year' => $this->year, + 'month' => $this->month, + 'day' => $this->day, + 'hour' => $this->hour, + 'minute' => $this->minute, + 'second' => $this->second + ]; + + foreach( $duration as $index => $value ) { + + if( $this->priority( $value, $index ) != false ) { + + return $this->priority( $value, $index ); + } - } - } + } + } } diff --git a/src/config/ago.php b/src/config/ago.php new file mode 100644 index 0000000..1bcf5ff --- /dev/null +++ b/src/config/ago.php @@ -0,0 +1,53 @@ + [ + + 1 => 'just-now', + + '*' => 'seconds-ago' + + ], + + 'minute' => [ + + 1 => 'one-minute-ago', + + '*' => 'minutes-ago' + + ], + + 'hour' => [ + + 1 => 'an-hour-ago', + + '*' => 'hours-ago' + + ], + + 'day' => [ + + 1 => 'yesterday', + + '*' => 'days-ago' + + ], + + 'month' => [ + + 1 => 'a-month-ago', + + '*' => 'months-ago' + + ], + + 'year' => [ + + 1 => 'one-year-ago', + + '*' => 'years-ago' + + ] + +]; diff --git a/src/lang/en/general.php b/src/lang/en/general.php index 0408581..eb3131a 100644 --- a/src/lang/en/general.php +++ b/src/lang/en/general.php @@ -38,16 +38,17 @@ 'Tir' => 'Tir', 'ago' => 'ago', 'just-now' => 'just now', - 'one-minute' => 'one minute', - 'minutes' => 'minutes', - 'an-hour' => 'an hour', - 'hours' => 'hours', + 'seconds-ago' => 'seconds ago', + 'one-minute-ago' => 'one minute ago', + 'minutes-ago' => 'minutes ago', + 'an-hour-ago' => 'an hour ago', + 'hours-ago' => 'hours ago', 'yesterday' => 'yesterday', - 'days' => 'days', - 'a-week' => 'a week', - 'weeks' => 'weeks', - 'a-month' => 'a month', - 'months' => 'months', - 'one-year' => 'one year', - 'years' => 'years' + 'days-ago' => 'days ago', + 'a-week-ago' => 'a week ago', + 'weeks-ago' => 'weeks ago', + 'a-month-ago' => 'a month ago', + 'months-ago' => 'months ago', + 'one-year-ago' => 'one year ago', + 'years-ago' => 'years ago' ]; diff --git a/tests/ConvertTest.php b/tests/ConvertTest.php index f7ba242..fb9ecf8 100644 --- a/tests/ConvertTest.php +++ b/tests/ConvertTest.php @@ -1,8 +1,10 @@ assertEquals( - '2016-01-01 00:00:00', - Datium::create(2016, 1, 1, 0, 0, 0)->get() - ); - - $this->assertEquals( - '2016-01-01 01:00:00', - Datium::create(2016, 1, 1, 0, 0, 0)->add('1 hour')->get() - ); - - $this->assertEquals( - '2016-01-02 01:00:00', - Datium::create(2016, 1, 1, 0, 0, 0)->add('25 hour')->get() - ); - - $this->assertEquals( - '2016-01-01 00:01:00', - Datium::create(2016, 1, 1, 0, 0, 0)->add('1 minute')->get() - ); - - $this->assertEquals( - '2016-01-01 01:01:00', - Datium::create(2016, 1, 1, 0, 0, 0)->add('61 minute')->get() - ); - - $this->assertEquals( - '2016-01-01 00:00:01', - Datium::create(2016, 1, 1, 0, 0, 0)->add('1 second')->get() - ); - - $this->assertEquals( - '2016-01-01 00:01:01', - Datium::create(2016, 1, 1, 0, 0, 0)->add('61 second')->get() - ); - - $this->assertEquals( - '2016-01-02 00:00:00', - Datium::create(2016, 1, 1, 0, 0, 0)->add('1 day')->get() - ); - - $this->assertEquals( - '2016-01-11 00:00:00', - Datium::create(2016, 1, 1, 0, 0, 0)->add('10 day')->get() - ); - - $this->assertEquals( - '2016-02-01 00:00:00', - Datium::create(2016, 1, 1, 0, 0, 0)->add('1 month')->get() - ); - - $this->assertEquals( - '2016-11-01 00:00:00', - Datium::create(2016, 1, 1, 0, 0, 0)->add('10 month')->get() - ); - - $this->assertEquals( - '2017-01-01 00:00:00', - Datium::create(2016, 1, 1, 0, 0, 0)->add('1 year')->get() - ); - - $this->assertEquals( - '2116-01-01 00:00:00', - Datium::create(2016, 1, 1, 0, 0, 0)->add('100 year')->get() - ); + $this->assertEquals( + '2016-01-01 00:00:00', + Datium::create(2016, 1, 1, 0, 0, 0)->get() + ); + + $this->assertEquals( + '2016-01-01 01:00:00', + Datium::create(2016, 1, 1, 0, 0, 0)->add('1 hour')->get() + ); + + $this->assertEquals( + '2016-01-02 01:00:00', + Datium::create(2016, 1, 1, 0, 0, 0)->add('25 hour')->get() + ); + + $this->assertEquals( + '2016-01-01 00:01:00', + Datium::create(2016, 1, 1, 0, 0, 0)->add('1 minute')->get() + ); + + $this->assertEquals( + '2016-01-01 01:01:00', + Datium::create(2016, 1, 1, 0, 0, 0)->add('61 minute')->get() + ); + + $this->assertEquals( + '2016-01-01 00:00:01', + Datium::create(2016, 1, 1, 0, 0, 0)->add('1 second')->get() + ); + + $this->assertEquals( + '2016-01-01 00:01:01', + Datium::create(2016, 1, 1, 0, 0, 0)->add('61 second')->get() + ); + + $this->assertEquals( + '2016-01-02 00:00:00', + Datium::create(2016, 1, 1, 0, 0, 0)->add('1 day')->get() + ); + + $this->assertEquals( + '2016-01-11 00:00:00', + Datium::create(2016, 1, 1, 0, 0, 0)->add('10 day')->get() + ); + + $this->assertEquals( + '2016-02-01 00:00:00', + Datium::create(2016, 1, 1, 0, 0, 0)->add('1 month')->get() + ); + + $this->assertEquals( + '2016-11-01 00:00:00', + Datium::create(2016, 1, 1, 0, 0, 0)->add('10 month')->get() + ); + + $this->assertEquals( + '2017-01-01 00:00:00', + Datium::create(2016, 1, 1, 0, 0, 0)->add('1 year')->get() + ); + + $this->assertEquals( + '2116-01-01 00:00:00', + Datium::create(2016, 1, 1, 0, 0, 0)->add('100 year')->get() + ); } public function testSubDateTime() { - $this->assertEquals( - '2016-01-01 00:00:00', - Datium::create(2016, 1, 1, 1, 0, 0)->sub('1 hour')->get() - ); - - $this->assertEquals( - '2016-01-01 00:00:00', - Datium::create(2016, 1, 2, 1, 0, 0)->sub('25 hour')->get() - ); - - $this->assertEquals( - '2016-01-01 00:00:00', - Datium::create(2016, 1, 1, 0, 1, 0)->sub('1 minute')->get() - ); - - $this->assertEquals( - '2016-01-01 00:00:00', - Datium::create(2016, 1, 1, 1, 1, 0)->sub('61 minute')->get() - ); - - $this->assertEquals( - '2016-01-01 00:00:00', - Datium::create(2016, 1, 1, 0, 0, 1)->sub('1 second')->get() - ); - - $this->assertEquals( - '2016-01-01 00:00:00', - Datium::create(2016, 1, 1, 0, 1, 1)->sub('61 second')->get() - ); - - $this->assertEquals( - '2016-01-01 00:00:00', - Datium::create(2016, 1, 1, 0, 0, 0)->get() - ); - - $this->assertEquals( - '2016-01-01 00:00:00', - Datium::create(2016, 1, 2, 0, 0, 0)->sub('1 day')->get() - ); - - $this->assertEquals( - '2016-01-01 00:00:00', - Datium::create(2016, 1, 11, 0, 0, 0)->sub('10 day')->get() - ); - - $this->assertEquals( - '2016-01-01 00:00:00', - Datium::create(2016, 2, 1, 0, 0, 0)->sub('1 month')->get() - ); - - $this->assertEquals( - '2016-01-01 00:00:00', - Datium::create(2016, 11, 1, 0, 0, 0)->sub('10 month')->get() - ); - - $this->assertEquals( - '2016-01-01 00:00:00', - Datium::create(2017, 1, 1, 0, 0, 0)->sub('1 year')->get() - ); - - $this->assertEquals( - '2016-01-01 00:00:00', - Datium::create(2116, 1, 1, 0, 0, 0)->sub('100 year')->get() - ); + $this->assertEquals( + '2016-01-01 00:00:00', + Datium::create(2016, 1, 1, 1, 0, 0)->sub('1 hour')->get() + ); + + $this->assertEquals( + '2016-01-01 00:00:00', + Datium::create(2016, 1, 2, 1, 0, 0)->sub('25 hour')->get() + ); + + $this->assertEquals( + '2016-01-01 00:00:00', + Datium::create(2016, 1, 1, 0, 1, 0)->sub('1 minute')->get() + ); + + $this->assertEquals( + '2016-01-01 00:00:00', + Datium::create(2016, 1, 1, 1, 1, 0)->sub('61 minute')->get() + ); + + $this->assertEquals( + '2016-01-01 00:00:00', + Datium::create(2016, 1, 1, 0, 0, 1)->sub('1 second')->get() + ); + + $this->assertEquals( + '2016-01-01 00:00:00', + Datium::create(2016, 1, 1, 0, 1, 1)->sub('61 second')->get() + ); + + $this->assertEquals( + '2016-01-01 00:00:00', + Datium::create(2016, 1, 1, 0, 0, 0)->get() + ); + + $this->assertEquals( + '2016-01-01 00:00:00', + Datium::create(2016, 1, 2, 0, 0, 0)->sub('1 day')->get() + ); + + $this->assertEquals( + '2016-01-01 00:00:00', + Datium::create(2016, 1, 11, 0, 0, 0)->sub('10 day')->get() + ); + + $this->assertEquals( + '2016-01-01 00:00:00', + Datium::create(2016, 2, 1, 0, 0, 0)->sub('1 month')->get() + ); + + $this->assertEquals( + '2016-01-01 00:00:00', + Datium::create(2016, 11, 1, 0, 0, 0)->sub('10 month')->get() + ); + + $this->assertEquals( + '2016-01-01 00:00:00', + Datium::create(2017, 1, 1, 0, 0, 0)->sub('1 year')->get() + ); + + $this->assertEquals( + '2016-01-01 00:00:00', + Datium::create(2116, 1, 1, 0, 0, 0)->sub('100 year')->get() + ); } diff --git a/tests/DayOfTest.php b/tests/DayOfTest.php index 2468b44..23c6ffa 100644 --- a/tests/DayOfTest.php +++ b/tests/DayOfTest.php @@ -1,8 +1,10 @@ assertEquals( Datium::between( Datium::now()->object() , Datium::now()->add('2 month')->object() )->events()->international()->get() ); - // - // } -} diff --git a/tests/LeapTest.php b/tests/LeapTest.php index 4784cbc..16b3dfd 100644 --- a/tests/LeapTest.php +++ b/tests/LeapTest.php @@ -1,8 +1,10 @@ assertEquals( - 'just now', - Datium::now()->sub('1 second')->ago() - ); - - $this->assertEquals( - 'just now', - Datium::now()->sub('59 second')->ago() - ); - - $this->assertEquals( - 'just now', - Datium::now()->sub('1 minute')->ago() - ); - - $this->assertEquals( - 'one minute ago', - Datium::now()->sub('1 minute')->sub('1 second')->ago() - ); - - $this->assertEquals( - '59 minutes ago', - Datium::now()->sub('59 minute')->sub('1 second')->ago() - ); - - $this->assertEquals( - '59 minutes ago', - Datium::now()->sub('59 minute')->sub('1 second')->ago() - ); - - $this->assertEquals( - 'an hour ago', - Datium::now()->sub('1 hour')->sub('1 minute')->ago() - ); - - $this->assertEquals( - '10 hours ago', - Datium::now()->sub('10 hour')->ago() - ); - - $this->assertEquals( - 'yesterday', - Datium::now()->sub('1 day')->sub('1 hour')->ago() - ); - - $this->assertEquals( - '3 days ago', - Datium::now()->sub('3 day')->ago() - ); - - $this->assertEquals( - 'a month ago', - Datium::now()->sub('1 month')->sub('1 day')->ago() - ); - - $this->assertEquals( - '2 months ago', - Datium::now()->sub('2 month')->sub('1 day')->ago() - ); - - $this->assertEquals( - 'one year ago', - Datium::now()->sub('1 month')->sub('1 year')->ago() - ); - - $this->assertEquals( - '2 years ago', - Datium::now()->sub('1 month')->sub('2 year')->ago() - ); + $this->assertEquals( + 'just now', + Datium::now()->sub('1 second')->ago()->get() + ); + + $this->assertEquals( + '59 seconds ago', + Datium::now()->sub('59 second')->ago()->get() + ); + + $this->assertEquals( + 'one minute ago', + Datium::now()->sub('1 minute')->ago()->get() + ); + + $this->assertEquals( + 'one minute ago', + Datium::now()->sub('1 minute')->sub('1 second')->ago()->get() + ); + + $this->assertEquals( + '59 minutes ago', + Datium::now()->sub('59 minute')->sub('1 second')->ago()->get() + ); + + $this->assertEquals( + '59 minutes ago', + Datium::now()->sub('59 minute')->sub('1 second')->ago()->get() + ); + + $this->assertEquals( + 'an hour ago', + Datium::now()->sub('1 hour')->sub('1 minute')->ago()->get() + ); + + $this->assertEquals( + '10 hours ago', + Datium::now()->sub('10 hour')->ago()->get() + ); + + $this->assertEquals( + 'yesterday', + Datium::now()->sub('1 day')->sub('1 hour')->ago()->get() + ); + + $this->assertEquals( + '3 days ago', + Datium::now()->sub('3 day')->ago()->get() + ); + + $this->assertEquals( + 'a month ago', + Datium::now()->sub('1 month')->sub('1 day')->ago()->get() + ); + + $this->assertEquals( + '2 months ago', + Datium::now()->sub('2 month')->sub('1 day')->ago()->get() + ); + + $this->assertEquals( + 'one year ago', + Datium::now()->sub('1 month')->sub('1 year')->ago()->get() + ); + + $this->assertEquals( + '2 years ago', + Datium::now()->sub('1 month')->sub('2 year')->ago()->get() + ); } }