Browse Source

Revert "Use IntlDateFormatter" as it breaks because of unexisting locales
in some obscure place. Not stable nor necessary enough to go into 1.10.x.
Should go to 2.0.

This reverts commit aaf5d4019e007b0e310aa75a33d0e8edfe9e53ab.

Yannick Warnier 10 years ago
parent
commit
b429b9ad88
1 changed files with 93 additions and 46 deletions
  1. 93 46
      main/inc/lib/internationalization.lib.php

+ 93 - 46
main/inc/lib/internationalization.lib.php

@@ -664,8 +664,11 @@ function api_strtotime($time, $timezone = null) {
  *
  * @link http://php.net/manual/en/function.strftime.php
  */
-function api_format_date($time, $format = null, $language = null)
-{
+function api_format_date($time, $format = null, $language = null) {
+
+    $system_timezone = date_default_timezone_get();
+    date_default_timezone_set(_api_get_timezone());
+
     if (is_string($time)) {
         $time = strtotime($time);
     }
@@ -680,72 +683,116 @@ function api_format_date($time, $format = null, $language = null)
     if (is_int($format)) {
         switch ($format) {
             case DATE_FORMAT_ONLY_DAYNAME:
-                $datetype = IntlDateFormatter::SHORT;
-                $timetype = IntlDateFormatter::NONE;
+                $date_format = get_lang('dateFormatOnlyDayName', '', $language);
+                if (INTL_INSTALLED) {
+        			$datetype = IntlDateFormatter::SHORT;
+        			$timetype = IntlDateFormatter::NONE;
+        		}
                 break;
             case DATE_FORMAT_NUMBER_NO_YEAR:
-                $datetype = IntlDateFormatter::SHORT;
-                $timetype = IntlDateFormatter::NONE;
-                break;
-            case DATE_FORMAT_NUMBER:
-                $datetype = IntlDateFormatter::SHORT;
-                $timetype = IntlDateFormatter::NONE;
+                $date_format = get_lang('dateFormatShortNumberNoYear', '', $language);
+        		if (INTL_INSTALLED) {
+        			$datetype = IntlDateFormatter::SHORT;
+        			$timetype = IntlDateFormatter::NONE;
+        		}
                 break;
+        	case DATE_FORMAT_NUMBER:
+        		$date_format = get_lang('dateFormatShortNumber', '', $language);
+        		if (INTL_INSTALLED) {
+        			$datetype = IntlDateFormatter::SHORT;
+        			$timetype = IntlDateFormatter::NONE;
+        		}
+        		break;
             case TIME_NO_SEC_FORMAT:
-                $datetype = IntlDateFormatter::NONE;
-                $timetype = IntlDateFormatter::SHORT;
+                $date_format = get_lang('timeNoSecFormat', '', $language);
+                if (INTL_INSTALLED) {
+                    $datetype = IntlDateFormatter::NONE;
+                    $timetype = IntlDateFormatter::SHORT;
+                }
                 break;
             case DATE_FORMAT_SHORT:
-                $datetype = IntlDateFormatter::LONG;
-                $timetype = IntlDateFormatter::NONE;
+                $date_format = get_lang('dateFormatShort', '', $language);
+                if (INTL_INSTALLED) {
+                    $datetype = IntlDateFormatter::LONG;
+                    $timetype = IntlDateFormatter::NONE;
+                }
                 break;
             case DATE_FORMAT_LONG:
-                $datetype = IntlDateFormatter::FULL;
-                $timetype = IntlDateFormatter::NONE;
+                $date_format = get_lang('dateFormatLong', '', $language);
+                if (INTL_INSTALLED) {
+                    $datetype = IntlDateFormatter::FULL;
+                    $timetype = IntlDateFormatter::NONE;
+                }
                 break;
             case DATE_TIME_FORMAT_LONG:
-                $datetype = IntlDateFormatter::FULL;
-                $timetype = IntlDateFormatter::SHORT;
+                $date_format = get_lang('dateTimeFormatLong', '', $language);
+                if (INTL_INSTALLED) {
+                    $datetype = IntlDateFormatter::FULL;
+                    $timetype = IntlDateFormatter::SHORT;
+                }
                 break;
             case DATE_FORMAT_LONG_NO_DAY:
-                $datetype = IntlDateFormatter::FULL;
-                $timetype = IntlDateFormatter::SHORT;
+                $date_format = get_lang('dateFormatLongNoDay', '', $language);
+                if (INTL_INSTALLED) {
+                    $datetype = IntlDateFormatter::FULL;
+                    $timetype = IntlDateFormatter::SHORT;
+                }
                 break;
-            case DATE_TIME_FORMAT_SHORT:
-                $datetype = IntlDateFormatter::FULL;
-                $timetype = IntlDateFormatter::SHORT;
+			case DATE_TIME_FORMAT_SHORT:
+                $date_format = get_lang('dateTimeFormatShort', '', $language);
+                if (INTL_INSTALLED) {
+                    $datetype = IntlDateFormatter::FULL;
+                    $timetype = IntlDateFormatter::SHORT;
+                }
                 break;
-            case DATE_TIME_FORMAT_SHORT_TIME_FIRST:
-                $datetype = IntlDateFormatter::FULL;
-                $timetype = IntlDateFormatter::SHORT;
+			case DATE_TIME_FORMAT_SHORT_TIME_FIRST:
+                $date_format = get_lang('dateTimeFormatShortTimeFirst', '', $language);
+                if (INTL_INSTALLED) {
+                    $datetype = IntlDateFormatter::FULL;
+                    $timetype = IntlDateFormatter::SHORT;
+                }
                 break;
             case DATE_TIME_FORMAT_LONG_24H:
-                $datetype = IntlDateFormatter::FULL;
-                $timetype = IntlDateFormatter::SHORT;
+                $date_format = get_lang('dateTimeFormatLong24H', '', $language);
+                if (INTL_INSTALLED) {
+                    $datetype = IntlDateFormatter::FULL;
+                    $timetype = IntlDateFormatter::SHORT;
+                }
                 break;
             default:
-                $datetype = IntlDateFormatter::FULL;
-                $timetype = IntlDateFormatter::SHORT;
+                $date_format = get_lang('dateTimeFormatLong', '', $language);
+                if (INTL_INSTALLED) {
+                    $datetype = IntlDateFormatter::FULL;
+                    $timetype = IntlDateFormatter::SHORT;
+                }
         }
+    } else {
+        $date_format = $format;
     }
 
-    // Use ICU
-    if (is_null($language)) {
-        $language = api_get_language_isocode();
-    }
-
-    $date_formatter = new IntlDateFormatter(
-        $language,
-        $datetype,
-        $timetype,
-        date_default_timezone_get()
-    );
-
-    $formatted_date = api_to_system_encoding(
-        $date_formatter->format($time),
-        'UTF-8'
-    );
+    if (0) {
+        //if using PHP 5.3 format dates like: $dateFormatShortNumber, can't be used
+        //
+        // Use ICU
+        if (is_null($language)) {
+            $language = api_get_language_isocode();
+        }
+        $date_formatter = new IntlDateFormatter($language, $datetype, $timetype, date_default_timezone_get());
+        //$date_formatter->setPattern($date_format);
+        $formatted_date = api_to_system_encoding($date_formatter->format($time), 'UTF-8');
 
+    } else {
+        // We replace %a %A %b %B masks of date format with translated strings
+        $translated = &_api_get_day_month_names($language);
+        $date_format = str_replace(array('%A', '%a', '%B', '%b'),
+        array($translated['days_long'][(int)strftime('%w', $time )],
+            $translated['days_short'][(int)strftime('%w', $time)],
+            $translated['months_long'][(int)strftime('%m', $time) - 1],
+            $translated['months_short'][(int)strftime('%m', $time) - 1]),
+        $date_format);
+        $formatted_date = api_to_system_encoding(strftime($date_format, $time), 'UTF-8');
+    }
+    date_default_timezone_set($system_timezone);
     return $formatted_date;
 }