Skip to content

Commit

Permalink
Refactored the time and timezone code out of Kohana_Log and moved the…
Browse files Browse the repository at this point in the history
…m to Kohana_Date. This way the app and the modules can all benefit from it.
  • Loading branch information
Fred Wu authored and BRMatt committed Sep 5, 2010
1 parent db2b1a9 commit 9e574a2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
30 changes: 30 additions & 0 deletions classes/kohana/date.php
Expand Up @@ -17,6 +17,16 @@ class Kohana_Date {
const DAY = 86400;
const HOUR = 3600;
const MINUTE = 60;

/**
* @var string timestamp format
*/
public static $timestamp_format = 'Y-m-d H:i:s';

/**
* @var string timezone for dates logged
*/
public static $timezone;

/**
* Returns the offset (in seconds) between two time zones. Use this to
Expand Down Expand Up @@ -518,5 +528,25 @@ public static function dos2unix($timestamp = FALSE)

return mktime($hrs, $min, $sec, $mon, $day, $year + 1980);
}

/**
* Returns a date/time string with the specified timestamp format
*
* $time = Date::formatted_time('5 minutes ago');
*
* @see http://php.net/manual/en/datetime.construct.php
* @param integer DOS timestamp
* @return integer
*/
public static function formatted_time($datetime_str = 'now', $timestamp_format = NULL)
{
$timestamp_format = $timestamp_format == NULL ? self::$timestamp_format : $timestamp_format;

$time = new DateTime($datetime_str, new DateTimeZone(
Date::$timezone ? Date::$timezone : date_default_timezone_get()
));

return $time->format(Date::$timestamp_format);
}

} // End date
24 changes: 1 addition & 23 deletions classes/kohana/log.php
Expand Up @@ -12,16 +12,6 @@
*/
class Kohana_Log {

/**
* @var string timestamp format
*/
public static $timestamp = 'Y-m-d H:i:s';

/**
* @var string timezone for dates logged
*/
public static $timezone;

// Singleton static instance
private static $_instance;

Expand Down Expand Up @@ -104,18 +94,6 @@ public function detach(Kohana_Log_Writer $writer)
*/
public function add($type, $message, array $values = NULL)
{
if (self::$timezone)
{
// Display the time according to the given timezone
$time = new DateTime('now', new DateTimeZone(self::$timezone));
$time = $time->format(self::$timestamp);
}
else
{
// Display the time in the current locale timezone
$time = date(self::$timestamp);
}

if ($values)
{
// Insert the values into the message
Expand All @@ -125,7 +103,7 @@ public function add($type, $message, array $values = NULL)
// Create a new message and timestamp it
$this->_messages[] = array
(
'time' => $time,
'time' => Date::formatted_time(),
'type' => $type,
'body' => $message,
);
Expand Down

0 comments on commit 9e574a2

Please sign in to comment.