Skip to content

Commit

Permalink
Change TimeAgo class design pattern, clean codes and remote temporary…
Browse files Browse the repository at this point in the history
… events class files
  • Loading branch information
imehdihosseini committed Jun 16, 2016
1 parent 5030dac commit f2212e3
Show file tree
Hide file tree
Showing 21 changed files with 488 additions and 718 deletions.
38 changes: 2 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"autoload": {
"psr-4": {
"OpenCafe\\": [ "src/", "src/Events/" ],
"OpenCafe\\" : "src/",
"OpenCafe\\Tools\\": "src/"
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/CalendarSettings/Hijri.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
40 changes: 26 additions & 14 deletions src/CalendarSettings/Jalali.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,21 +296,32 @@
*/
'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';

$configGregorian = include 'Gregorian.php';

$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;
}
}
}

},

Expand All @@ -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 {
Expand All @@ -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;
}

},
Expand Down
29 changes: 7 additions & 22 deletions src/Config.php
Original file line number Diff line number Diff line change
@@ -1,43 +1,28 @@
<?php

return array(
return [

/************************************************************
* Default Time Zone
************************************************************
*
/**
* Define your default time zone
*
*\_________________________________________________________/
*/
'timezone' => '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' ],

);
];
98 changes: 53 additions & 45 deletions src/Datium.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @category Core
* @package OpenCafe\Datium
* @author Mehdi Hosseini <[email protected]>
* @license icense https://opensource.org/licenses/MIT
* @license License https://opensource.org/licenses/MIT
* @link https://github.com/opencafe/datium
* @since Aug 17, 2015
*/
Expand All @@ -20,8 +20,6 @@
use OpenCafe\Tools\Lang;
use OpenCafe\Tools\TimeAgo;

use OpenCafe\Datium;

/**
* Main Datium class
*
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;

}

Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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;

}

Expand Down
Loading

0 comments on commit f2212e3

Please sign in to comment.