From 9e574a2bd6d083303a5cacc552ad15bd71602449 Mon Sep 17 00:00:00 2001 From: Fred Wu Date: Wed, 30 Jun 2010 18:48:35 +1000 Subject: [PATCH] Refactored the time and timezone code out of Kohana_Log and moved them to Kohana_Date. This way the app and the modules can all benefit from it. --- classes/kohana/date.php | 30 ++++++++++++++++++++++++++++++ classes/kohana/log.php | 24 +----------------------- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/classes/kohana/date.php b/classes/kohana/date.php index f097d63d9..c3182c70f 100644 --- a/classes/kohana/date.php +++ b/classes/kohana/date.php @@ -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 @@ -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 diff --git a/classes/kohana/log.php b/classes/kohana/log.php index ead67f972..7cea86864 100644 --- a/classes/kohana/log.php +++ b/classes/kohana/log.php @@ -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; @@ -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 @@ -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, );