cvs: pear /I18Nv2 I18Nv2.php Locale.php

From: Date: Mon, 21 Feb 2005 13:44:04 +0000
Subject: cvs: pear /I18Nv2 I18Nv2.php Locale.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-29616@lists.php.net to get a copy of this message
mike Mon Feb 21 08:44:04 2005 EDT Modified files: /pear/I18Nv2 I18Nv2.php Locale.php Log: * implemented paranoid mode (resets the global locale to C) http://cvs.php.net/diff.php/pear/I18Nv2/I18Nv2.php?r1=1.28&r2=1.29&ty=u Index: pear/I18Nv2/I18Nv2.php diff -u pear/I18Nv2/I18Nv2.php:1.28 pear/I18Nv2/I18Nv2.php:1.29 --- pear/I18Nv2/I18Nv2.php:1.28 Tue Feb 1 04:09:07 2005 +++ pear/I18Nv2/I18Nv2.php Mon Feb 21 08:44:04 2005 @@ -11,7 +11,7 @@ // | Copyright (c) 2004 Michael Wallner <mike@iworks.at> | // +----------------------------------------------------------------------+ // -// $Id: I18Nv2.php,v 1.28 2005/02/01 09:09:07 mike Exp $ +// $Id: I18Nv2.php,v 1.29 2005/02/21 13:44:04 mike Exp $ /** * I18Nv2 @@ -26,7 +26,7 @@ * I18Nv2 - Internationalization v2 * * @author Michael Wallner <mike@php.net> - * @version $Revision: 1.28 $ + * @version $Revision: 1.29 $ * @package I18Nv2 * @access public * @static @@ -171,9 +171,10 @@ * @static * @access public * @return object I18Nv2_Locale - * @param string $locale + * @param string $locale The locale to use. + * @param bool $paranoid Whether to operate in paranoid mode. */ - function &createLocale($locale = null) + function &createLocale($locale = null, $paranoid = false) { require_once 'I18Nv2/Locale.php'; return new I18Nv2_Locale($locale); http://cvs.php.net/diff.php/pear/I18Nv2/Locale.php?r1=1.28&r2=1.29&ty=u Index: pear/I18Nv2/Locale.php diff -u pear/I18Nv2/Locale.php:1.28 pear/I18Nv2/Locale.php:1.29 --- pear/I18Nv2/Locale.php:1.28 Thu Feb 10 11:17:25 2005 +++ pear/I18Nv2/Locale.php Mon Feb 21 08:44:04 2005 @@ -11,7 +11,7 @@ // | Copyright (c) 2004 Michael Wallner <mike@iworks.at> | // +----------------------------------------------------------------------+ // -// $Id: Locale.php,v 1.28 2005/02/10 16:17:25 mike Exp $ +// $Id: Locale.php,v 1.29 2005/02/21 13:44:04 mike Exp $ /** * I18Nv2::Locale @@ -47,7 +47,7 @@ * I18Nv2_Locale * * @author Michael Wallner <mike@php.net> - * @version $Revision: 1.28 $ + * @version $Revision: 1.29 $ * @access public * @package I18Nv2 */ @@ -190,15 +190,32 @@ var $cache = array(); /** + * Whether to reset the global locale after each call + * + * @access protected + * @var bool + */ + var $paranoid = false; + + /** + * Store system locale for paranoid mode + * + * @access protected + * @var string + */ + var $usedLocale = ''; + + /** * Constructor * * @access public * @param string $locale */ - function I18Nv2_Locale($locale = null) + function I18Nv2_Locale($locale = null, $paranoid = false) { $locale or $locale = I18Nv2::lastLocale(0, 'locale'); $this->setLocale($locale); + $this->setParanoid($paranoid); } /** @@ -238,7 +255,7 @@ function initialize($locale) { $this->initialized = $locale; - $usedLocale = I18Nv2::setLocale($locale); + $this->usedLocale = I18Nv2::setLocale($locale); $jan = $mon = mktime(1,1,1,1,1,1990); $feb = $tue = mktime(1,1,1,2,6,1990); @@ -253,7 +270,7 @@ $nov = mktime(1,1,1,11,1,1990); $dec = mktime(1,1,1,12,1,1990); - if (!$this->loadCache($usedLocale)) { + if (!$this->loadCache($this->usedLocale)) { $this->days = array( strftime('%A', $sun), strftime('%A', $mon), @@ -381,11 +398,16 @@ $this->timeFormats[I18Nv2_DATETIME_FULL], ); } - $this->updateCache($usedLocale); + $this->updateCache($this->usedLocale); } $this->setDefaults(); - return $usedLocale; + + if ($this->paranoid) { + setlocale(LC_ALL, 'C'); + } + + return $this->usedLocale; } /** @@ -463,6 +485,21 @@ } /** + * Set Paranoid Mode + * + * Whether to reset to the C-locale after every call. + * + * @access public + * @return void + * @param bool $paranoid Whether to enable paranoid mode. + */ + function setParanoid($paranoid = false) + { + $this->paranoid = (bool) $paranoid; + $paranoid and setLocale(LC_ALL, 'C'); + } + + /** * Set currency format * * @access public @@ -694,7 +731,10 @@ { $format = isset($overrideFormat) ? $this->dateFormats[$overrideFormat] : $this->currentDateFormat; - return strftime($format, isset($timestamp) ? $timestamp : time()); + $this->paramoid and setLocale(LC_ALL, $this->usedLocale); + $date = strftime($format, isset($timestamp) ? $timestamp : time()); + $this->paranoid and setLocale(LC_ALL, 'C'); + return $date; } /** @@ -709,7 +749,9 @@ { $format = isset($overrideFormat) ? $this->timeFormats[$overrideFormat] : $this->currentTimeFormat; - return strftime($format, isset($timestamp) ? $timestamp : time()); + $this->paranoid and setLocale(LC_ALL, $this->usedLocale); + $date = strftime($format, isset($timestamp) ? $timestamp : time()); + $this->paranoid and setLocale(LC_ALL, 'C'); } /** @@ -725,7 +767,10 @@ $format = isset($overrideFormat) ? $this->dateTimeFormats[$overrideFormat] : $this->currentDateTimeFormat; - return strftime($format, isset($timestamp) ? $timestamp : time()); + $this->paranoid and setLocale(LC_ALL, $this->usedLocale); + $date = strftime($format, isset($timestamp) ? $timestamp : time()); + $this->paranoid and setLocale(LC_ALL, 'C'); + return $date; } /** @@ -737,7 +782,10 @@ */ function time($timestamp = null) { - return strftime('%X', isset($timestamp) ? $timestamp : time()); + $this->paranoid and setLocale(LC_ALL, $this->usedLocale); + $time = strftime('%X', isset($timestamp) ? $timestamp : time()); + $this->paranoid and setLocale(LC_ALL, 'C'); + return $time; } /** @@ -749,7 +797,9 @@ */ function date($timestamp = null) { - return strftime('%x', isset($timestamp) ? $timestamp : time()); + $this->paranoid and setLocale(LC_ALL, $this->usedLocale); + $date = strftime('%x', isset($timestamp) ? $timestamp : time()); + $this->paranoid and setLocale(LC_ALL, 'C'); } /**

« previous php.pear.cvs (#29616) next »