Skip to content

Commit

Permalink
Performance improvements. V2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
sallar committed Sep 2, 2013
1 parent 03cdacb commit 4966033
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 66 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jDateTime
Jalali (Shamsi) DadeTime class written in PHP, Supports year higher than 2038.
[![Build Status](https://travis-ci.org/sallar/jDateTime.png?branch=master)](https://travis-ci.org/sallar/jDateTime)

##About v2.1.6
##About v2.2.0

PHP's default `date` function does not support years higher than 2038, so the `DateTime` class was introduced in PHP5 to solve this problem and provide more sophisticated date methods. Iranian users have been using an old `jdate` function to convert Gregorian date to the Jalali equivalent, which is completely based on the old php `date` function so its pretty much out-dated.

Expand Down
149 changes: 84 additions & 65 deletions jdatetime.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@
* @license http://opensource.org/licenses/mit-license.php The MIT License
* @link https://github.com/sallar/jDateTime
* @see DateTime
* @version 2.1.6
* @version 2.2.0
*/
class jDateTime
{

/**
* Defaults
*/
private static $jalali = true; //Use Jalali Date, If set to false, falls back to gregorian
private static $convert = true; //Convert numbers to Farsi characters in utf-8
private static $jalali = true; //Use Jalali Date, If set to false, falls back to gregorian
private static $convert = true; //Convert numbers to Farsi characters in utf-8
private static $timezone = null; //Timezone String e.g Asia/Tehran, Defaults to Server Timezone Settings
private static $temp = array();

Expand All @@ -68,9 +68,9 @@ class jDateTime
*/
public function __construct($convert = null, $jalali = null, $timezone = null)
{
if ( $jalali !== null ) self::$jalali = ($jalali === false) ? false : true;
if ( $convert !== null ) self::$convert = ($convert === false) ? false : true;
if ( $timezone !== null ) self::$timezone = ($timezone != null) ? $timezone : null;
if ( $jalali !== null ) self::$jalali = (bool) $jalali;
if ( $convert !== null ) self::$convert = (bool) $convert;
if ( $timezone !== null ) self::$timezone = $timezone;
}

/**
Expand All @@ -93,7 +93,7 @@ public function __construct($convert = null, $jalali = null, $timezone = null)
public static function date($format, $stamp = false, $convert = null, $jalali = null, $timezone = null)
{
//Timestamp + Timezone
$stamp = ($stamp != false) ? $stamp : time();
$stamp = ($stamp !== false) ? $stamp : time();
$timezone = ($timezone != null) ? $timezone : ((self::$timezone != null) ? self::$timezone : date_default_timezone_get());
$obj = new DateTime('@' . $stamp, new DateTimeZone($timezone));
$obj->setTimezone(new DateTimeZone($timezone));
Expand Down Expand Up @@ -131,7 +131,7 @@ public static function date($format, $stamp = false, $convert = null, $jalali =
switch ($key) {
//Day
case 'd':
$v = sprintf("%02d", $jday);
$v = sprintf('%02d', $jday);
break;
case 'D':
$v = self::getDayNames($obj->format('D'), true);
Expand Down Expand Up @@ -169,7 +169,7 @@ public static function date($format, $stamp = false, $convert = null, $jalali =
$v = self::getMonthNames($jmonth);
break;
case 'm':
$v = sprintf("%02d", $jmonth);
$v = sprintf('%02d', $jmonth);
break;
case 'M':
$v = self::getMonthNames($jmonth, true);
Expand Down Expand Up @@ -204,11 +204,11 @@ public static function date($format, $stamp = false, $convert = null, $jalali =
break;
//Full Dates
case 'c':
$v = $jyear.'-'.sprintf("%02d", $jmonth).'-'.sprintf("%02d", $jday).'T';
$v = $jyear.'-'.sprintf('%02d', $jmonth).'-'.sprintf('%02d', $jday).'T';
$v .= $obj->format('H').':'.$obj->format('i').':'.$obj->format('s').$obj->format('P');
break;
case 'r':
$v = self::getDayNames($obj->format('D'), true).', '.sprintf("%02d", $jday).' '.self::getMonthNames($jmonth, true);
$v = self::getDayNames($obj->format('D'), true).', '.sprintf('%02d', $jday).' '.self::getMonthNames($jmonth, true);
$v .= ' '.$jyear.' '.$obj->format('H').':'.$obj->format('i').':'.$obj->format('s').' '.$obj->format('P');
break;
//Timezone
Expand Down Expand Up @@ -284,23 +284,23 @@ public static function gDate($format, $stamp = false, $timezone = null)
public static function strftime($format, $stamp = false, $convert = null, $jalali = null, $timezone = null)
{
$str_format_code = array(
"%a", "%A", "%d", "%e", "%j", "%u", "%w",
"%U", "%V", "%W",
"%b", "%B", "%h", "%m",
"%C", "%g", "%G", "%y", "%Y",
"%H", "%I", "%l", "%M", "%p", "%P", "%r", "%R", "%S", "%T", "%X", "%z", "%Z",
"%c", "%D", "%F", "%s", "%x",
"%n", "%t", "%%"
'%a', '%A', '%d', '%e', '%j', '%u', '%w',
'%U', '%V', '%W',
'%b', '%B', '%h', '%m',
'%C', '%g', '%G', '%y', '%Y',
'%H', '%I', '%l', '%M', '%p', '%P', '%r', '%R', '%S', '%T', '%X', '%z', '%Z',
'%c', '%D', '%F', '%s', '%x',
'%n', '%t', '%%'
);

$date_format_code = array(
"D", "l", "d", "j", "z", "N", "w",
"W", "W", "W",
"M", "F", "M", "m",
"y", "y", "y", "y", "Y",
"H", "h", "g", "i", "A", "a", "h:i:s A", "H:i", "s", "H:i:s", "h:i:s", "H", "H",
"D j M H:i:s", "d/m/y", "Y-m-d", "U", "d/m/y",
"\n", "\t", "%"
'D', 'l', 'd', 'j', 'z', 'N', 'w',
'W', 'W', 'W',
'M', 'F', 'M', 'm',
'y', 'y', 'y', 'y', 'Y',
'H', 'h', 'g', 'i', 'A', 'a', 'h:i:s A', 'H:i', 's', 'H:i:s', 'h:i:s', 'H', 'H',
'D j M H:i:s', 'd/m/y', 'Y-m-d', 'U', 'd/m/y',
'\n', '\t', '%'
);

//Change Strftime format to Date format
Expand Down Expand Up @@ -348,7 +348,7 @@ public static function mktime($hour, $minute, $second, $month, $day, $year, $jal
}

//Create a new object and set the timezone if available
$date = $year.'-'.sprintf("%02d", $month).'-'.sprintf("%02d", $day).' '.$hour.':'.$minute.':'.$second;
$date = $year.'-'.sprintf('%02d', $month).'-'.sprintf('%02d', $day).' '.$hour.':'.$minute.':'.$second;

if ( self::$timezone != null || $timezone != null ) {
$obj = new DateTime($date, new DateTimeZone(($timezone != null) ? $timezone : self::$timezone));
Expand All @@ -358,7 +358,7 @@ public static function mktime($hour, $minute, $second, $month, $day, $year, $jal
}

//Return
return $obj->format("U");
return $obj->format('U');
}

/**
Expand Down Expand Up @@ -395,7 +395,7 @@ public static function checkdate($month, $day, $year, $jalali = null)
{
$epoch = self::mktime(0, 0, 0, $month, $day, $year);

if( self::date("Y-n-j", $epoch,false) == "$year-$month-$day" ) {
if( self::date('Y-n-j', $epoch,false) == "$year-$month-$day" ) {
$ret = true;
}
else{
Expand All @@ -413,66 +413,85 @@ public static function checkdate($month, $day, $year, $jalali = null)

/**
* System Helpers below
*
* ------------------------------------------------------
*/

/**
* Filters out an array
*/
private static function filterArray($needle, $heystack, $always = array())
{
foreach($heystack as $k => $v)
{
if( !in_array($v, $needle) && !in_array($v, $always) )
unset($heystack[$k]);
}

return $heystack;
return array_intersect(array_merge($needle, $always), $heystack);
}

/**
* Returns correct names for week days
*/
private static function getDayNames($day, $shorten = false, $len = 1, $numeric = false)
{
$ret = '';
switch ( strtolower($day) ) {
case 'sat': case 'saturday': $ret = 'شنبه'; $n = 1; break;
case 'sun': case 'sunday': $ret = 'یکشنبه'; $n = 2; break;
case 'mon': case 'monday': $ret = 'دوشنبه'; $n = 3; break;
case 'tue': case 'tuesday': $ret = 'سه شنبه'; $n = 4; break;
case 'wed': case 'wednesday': $ret = 'چهارشنبه'; $n = 5; break;
case 'thu': case 'thursday': $ret = 'پنجشنبه'; $n = 6; break;
case 'fri': case 'friday': $ret = 'جمعه'; $n = 7; break;
}
return ($numeric) ? $n : (($shorten) ? mb_substr($ret, 0, $len, 'UTF-8') : $ret);
$days = array(
'sat' => array(1, 'شنبه'),
'sun' => array(2, 'یکشنبه'),
'mon' => array(3, 'دوشنبه'),
'tue' => array(4, 'سه شنبه'),
'wed' => array(5, 'چهارشنبه'),
'thu' => array(6, 'پنجشنبه'),
'fri' => array(7, 'جمعه')
);

$day = substr(strtolower($day), 0, 3);
$day = $days[$day];

return ($numeric) ? $day[0] : (($shorten) ? self::substr($day[1], 0, $len) : $day[1]);
}

/**
* Returns correct names for months
*/
private static function getMonthNames($month, $shorten = false, $len = 3)
{
$ret = '';
switch ( $month ) {
case '1': $ret = 'فروردین'; break;
case '2': $ret = 'اردیبهشت'; break;
case '3': $ret = 'خرداد'; break;
case '4': $ret = 'تیر'; break;
case '5': $ret = 'مرداد'; break;
case '6': $ret = 'شهریور'; break;
case '7': $ret = 'مهر'; break;
case '8': $ret = 'آبان'; break;
case '9': $ret = 'آذر'; break;
case '10': $ret = 'دی'; break;
case '11': $ret = 'بهمن'; break;
case '12': $ret = 'اسفند'; break;
}
return ($shorten) ? mb_substr($ret, 0, $len, 'UTF-8') : $ret;
// Convert
$months = array(
'فروردین', 'اردیبهشت', 'خرداد', 'تیر', 'مرداد', 'شهریور', 'مهر', 'آبان', 'آذر', 'دی', 'بهمن', 'اسفند'
);
$ret = $months[$month - 1];

// Return
return ($shorten) ? self::substr($ret, 0, $len) : $ret;
}

/**
* Converts latin numbers to farsi script
*/
private static function convertNumbers($matches)
{
$farsi_array = array("۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹");
$english_array = array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
$farsi_array = array('۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹');
$english_array = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');

return str_replace($english_array, $farsi_array, $matches);
}

/**
* Division
*/
private static function div($a, $b)
{
return (int) ($a / $b);
}

/**
* Substring helper
*/
private static function substr($str, $start, $len)
{
if( function_exists('mb_substr') ){
return mb_substr($str, $start, $len, 'UTF-8');
}
else{
return substr($str, $start, $len * 2);
}
}

/**
* Gregorian to Jalali Conversion
* Copyright (C) 2000 Roozbeh Pournader and Mohammad Toossi
Expand Down

0 comments on commit 4966033

Please sign in to comment.