diff --git a/library/log4php/pattern/LoggerPatternConverterDate.php b/library/log4php/pattern/LoggerPatternConverterDate.php new file mode 100644 --- /dev/null +++ b/library/log4php/pattern/LoggerPatternConverterDate.php @@ -0,0 +1,91 @@ + self::DATE_FORMAT_ISO8601, + 'ABSOLUTE' => self::DATE_FORMAT_ABSOLUTE, + 'DATE' => self::DATE_FORMAT_DATE, + ); + + private $useLocalDate = false; + + public function activateOptions() { + + // Parse the option (date format) + if (!empty($this->option)) { + if (isset($this->specials[$this->option])) { + $this->format = $this->specials[$this->option]; + } else { + $this->format = $this->option; + } + } + + // Check whether the pattern contains milliseconds (u) + if (preg_match('/(?format)) { + $this->useLocalDate = true; + } + } + + public function convert(LoggerLoggingEvent $event) { + if ($this->useLocalDate) { + return $this->date($this->format, $event->getTimeStamp()); + } + return date($this->format, $event->getTimeStamp()); + } + + /** + * Currently, PHP date() function always returns zeros for milliseconds (u) + * on Windows. This is a replacement function for date() which correctly + * displays milliseconds on all platforms. + * + * It is slower than PHP date() so it should only be used if necessary. + */ + private function date($format, $utimestamp) { + $timestamp = floor($utimestamp); + $ms = floor(($utimestamp - $timestamp) * 1000); + $ms = str_pad($ms, 3, '0', STR_PAD_LEFT); + + return date(preg_replace('`(?