Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 81 additions & 107 deletions core/Base/Date.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,18 @@ class Date implements Stringable, DialectString
const WEEKDAY_FRIDAY = 5;
const WEEKDAY_SATURDAY = 6;
const WEEKDAY_SUNDAY = 0; // because strftime('%w') is 0 on Sunday

protected $string = null;
protected $int = null;

protected $year = null;
protected $month = null;
protected $day = null;


/**
* @var DateTime
*/
protected $dateTime = null;

/**
* @return Date
**/
public static function create($date)
{
return new self($date);
return new static($date);
}

public static function today($delimiter = '-')
Expand All @@ -53,7 +51,7 @@ public static function today($delimiter = '-')
**/
public static function makeToday()
{
return new self(self::today());
return new static(static::today());
}

/**
Expand All @@ -68,13 +66,13 @@ public static function makeFromWeek($weekNumber, $year = null)

Assert::isTrue(
($weekNumber > 0)
&& ($weekNumber <= self::getWeekCountInYear($year))
&& ($weekNumber <= static::getWeekCountInYear($year))
);

$date =
new self(
new static(
date(
self::getFormat(),
static::getFormat(),
mktime(
0, 0, 0, 1, 1, $year
)
Expand All @@ -85,7 +83,7 @@ public static function makeFromWeek($weekNumber, $year = null)
(
(
$weekNumber - 1
+ (self::getWeekCountInYear($year - 1) == 53 ? 1 : 0)
+ (static::getWeekCountInYear($year - 1) == 53 ? 1 : 0)
)
* 7
) + 1 - $date->getWeekDay();
Expand All @@ -110,10 +108,10 @@ public static function dayDifference(Date $left, Date $right)

public static function compare(Date $left, Date $right)
{
if ($left->int == $right->int)
if ($left->toStamp() == $right->toStamp())
return 0;
else
return ($left->int > $right->int ? 1 : -1);
return ($left->toStamp() > $right->toStamp() ? 1 : -1);
}

public static function getWeekCountInYear($year)
Expand All @@ -129,77 +127,66 @@ public static function getWeekCountInYear($year)

public function __construct($date)
{
if (is_int($date) || is_numeric($date)) { // unix timestamp
$this->string = date($this->getFormat(), $date);
} elseif ($date && is_string($date))
$this->stringImport($date);

if ($this->string === null) {
throw new WrongArgumentException(
"strange input given - '{$date}'"
);
}

$this->import($this->string);
$this->buildInteger();
$this->import($date);
}

public function __sleep()
public function __clone()
{
return array('int');
$this->dateTime = clone $this->dateTime;
}

public function __wakeup()
public function __sleep()
{
$this->import(date($this->getFormat(), $this->int));
return array('dateTime');
}

public function toStamp()
{
return $this->int;
return $this->getDateTime()->getTimestamp();
}

public function toDate($delimiter = '-')
{
return
$this->year
$this->getYear()
.$delimiter
.$this->month
.$this->getMonth()
.$delimiter
.$this->day;
.$this->getDay();
}

public function getYear()
{
return $this->year;
return $this->dateTime->format('Y');
}

public function getMonth()
{
return $this->month;
return $this->dateTime->format('m');
}

public function getDay()
{
return $this->day;
return $this->dateTime->format('d');
}

public function getWeek()
{
return date('W', $this->int);
return date('W', $this->dateTime->getTimestamp());
}

public function getWeekDay()
{
return strftime('%w', $this->int);
return strftime('%w', $this->dateTime->getTimestamp());
}

/**
* @return Date
**/
public function spawn($modification = null)
{
$child = new $this($this->string);

$child = new static($this->toString());

if ($modification)
return $child->modify($modification);
Expand All @@ -214,17 +201,8 @@ public function spawn($modification = null)
public function modify($string)
{
try {
$time = strtotime($string, $this->int);

if ($time === false)
throw new WrongArgumentException(
"modification yielded false '{$string}'"
);

$this->int = $time;
$this->string = date($this->getFormat(), $time);
$this->import($this->string);
} catch (BaseException $e) {
$this->dateTime->modify($string);
} catch (Exception $e) {
throw new WrongArgumentException(
"wrong time string '{$string}'"
);
Expand All @@ -238,9 +216,9 @@ public function getDayStartStamp()
return
mktime(
0, 0, 0,
$this->month,
$this->day,
$this->year
$this->getMonth(),
$this->getDay(),
$this->getYear()
);
}

Expand All @@ -249,9 +227,9 @@ public function getDayEndStamp()
return
mktime(
23, 59, 59,
$this->month,
$this->day,
$this->year
$this->getMonth(),
$this->getDay(),
$this->getYear()
);
}

Expand All @@ -277,12 +255,12 @@ public function getLastDayOfWeek($weekStart = Date::WEEKDAY_MONDAY)

public function toString()
{
return $this->string;
return $this->dateTime->format(static::getFormat());
}

public function toFormatString($format)
{
return date($format, $this->toStamp());
return $this->dateTime->format($format);
}

public function toDialectString(Dialect $dialect)
Expand All @@ -306,57 +284,53 @@ public function toTimestamp()
{
return Timestamp::create($this->toStamp());
}

/**
* @return DateTime|null
*/
public function getDateTime()
{
return $this->dateTime;
}

protected static function getFormat()
{
return 'Y-m-d';
}

/* void */ protected function import($string)


protected function import($date)
{
list($this->year, $this->month, $this->day) =
explode('-', $string, 3);

if (!$this->month || !$this->day)
try{
if (is_int($date) || is_numeric($date)) { // unix timestamp
$this->dateTime = new DateTime(date(static::getFormat(), $date));

} elseif ($date && is_string($date)) {

if (
preg_match('/^(\d{1,4})[-\.](\d{1,2})[-\.](\d{1,2})/', $date, $matches)
) {
Assert::isTrue(
checkdate($matches[2], $matches[3], $matches[1])
);
} elseif (
preg_match('/^(\d{1,2})[-\.](\d{1,2})[-\.](\d{1,4})/', $date, $matches)
) {
Assert::isTrue(
checkdate($matches[2], $matches[2], $matches[3])
);
}

$this->dateTime = new DateTime($date);
}


} catch(Exception $e) {
throw new WrongArgumentException(
'month and day must not be zero'
);

$this->string =
sprintf(
'%04d-%02d-%02d',
$this->year,
$this->month,
$this->day
);

list($this->year, $this->month, $this->day) =
explode('-', $this->string, 3);
}

/* void */ protected function stringImport($string)
{
$matches = array();

if (
preg_match('/^(\d{1,4})-(\d{1,2})-(\d{1,2})$/', $string, $matches)
) {
if (checkdate($matches[2], $matches[3], $matches[1]))
$this->string = $string;

} elseif (($stamp = strtotime($string)) !== false)
$this->string = date($this->getFormat(), $stamp);
}

/* void */ protected function buildInteger()
{
$this->int =
mktime(
0, 0, 0,
$this->month,
$this->day,
$this->year
"strange input given - '{$date}'"
);
}

}
}
?>
Loading