PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » Date and Time » Date » Bug #5426

Cache the weekday and month names in Date_Calc

Details

Request #5426Cache the weekday and month names in Date_Calc
Submitted2005-09-15 23:14 UTC
Fromo dot persson at gmail dot com
StatusSuspended
PackageDate
PHP Version5.1.0
OSDebian GNU/Linux
Roadmaps(Not assigned)

Comments

[2005-09-15 23:14 UTC] o dot persson at gmail dot com

Description:
------------
This is a simple fix for caching weekdays and month names in Date_Calc.

Maybe a nicer approach would be to take advantage of the names that exists in I18Nv2.

Test script:
---------------
Index: ../components/PEAR/Date/Calc.php
===================================================================
--- ../components/PEAR/Date/Calc.php (revision 204)
+++ ../components/PEAR/Date/Calc.php (working copy)
@@ -1586,13 +1586,13 @@
*
* Used to take advantage of the setlocale function to return
* language specific month names.
- * XXX cache values to some global array to avoid preformace
- * hits when called more than once.
*
* @returns array An array of month names
*/
function getMonthNames()
{
+ static $months = array();
+
for($i=1;$i<13;$i++) {
$months[$i] = strftime('%B', mktime(0, 0, 0, $i, 1, 2001));
}
@@ -1604,13 +1604,13 @@
*
* Used to take advantage of the setlocale function to
* return language specific week days
- * XXX cache values to some global array to avoid preformace
- * hits when called more than once.
*
* @returns array An array of week day names
*/
function getWeekDays()
{
+ static $weeksdays = array();
+
for($i=0;$i<7;$i++) {
$weekdays[$i] = strftime('%A', mktime(0, 0, 0, 1, $i, 2001));
}

[2005-09-15 23:17 UTC] o dot persson at gmail dot com

Err, that's completly wrong. I guess I should goto sleep. ;)

Index: ../components/PEAR/Date/Calc.php
===================================================================
--- ../components/PEAR/Date/Calc.php (revision 205)
+++ ../components/PEAR/Date/Calc.php (working copy)
@@ -1586,16 +1586,18 @@
*
* Used to take advantage of the setlocale function to return
* language specific month names.
- * XXX cache values to some global array to avoid preformace
- * hits when called more than once.
*
* @returns array An array of month names
*/
function getMonthNames()
{
- for($i=1;$i<13;$i++) {
- $months[$i] = strftime('%B', mktime(0, 0, 0, $i, 1, 2001));
- }
+ static $months;
+
+ if (!isset($months)) {
+ for($i=1;$i<13;$i++) {
+ $months[$i] = strftime('%B', mktime(0, 0, 0, $i, 1, 2001));
+ }
+ }
return($months);
} // end func getMonthNames

@@ -1604,16 +1606,18 @@
*
* Used to take advantage of the setlocale function to
* return language specific week days
- * XXX cache values to some global array to avoid preformace
- * hits when called more than once.
*
* @returns array An array of week day names
*/
function getWeekDays()
{
- for($i=0;$i<7;$i++) {
- $weekdays[$i] = strftime('%A', mktime(0, 0, 0, 1, $i, 2001));
- }
+ static $weekdays;
+
+ if (!isset($weekdays)) {
+ for($i=0;$i<7;$i++) {
+ $weekdays[$i] = strftime('%A', mktime(0, 0, 0, 1, $i, 2001));
+ }
+ }
return($weekdays);
} // end func getWeekDays