From a55ce6c34cf8bbedcb61198ceef3c25fd1a0ef6c Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Mon, 8 Dec 2014 10:36:09 -0600
Subject: [PATCH] i18n: Convert datetime formats on upgrade to ICU

---
 include/class.auth.php                        |  3 +-
 include/class.config.php                      | 30 +++-----
 include/class.staff.php                       |  4 -
 .../i18n/en_US/help/tips/settings.system.yaml | 14 ++--
 .../streams/core/b26f29a6-00000000.task.php   | 73 +++++++++++++++++++
 5 files changed, 94 insertions(+), 30 deletions(-)
 create mode 100644 include/upgrader/streams/core/b26f29a6-00000000.task.php

diff --git a/include/class.auth.php b/include/class.auth.php
index ed9a73731..4cbbea035 100644
--- a/include/class.auth.php
+++ b/include/class.auth.php
@@ -101,8 +101,7 @@ class ClientCreateRequest {
         $this_form = UserForm::getUserForm()->getForm($this->getInfo());
         $bk = $this->getBackend();
         $defaults = array(
-            'timezone_id' => $cfg->getDefaultTimezoneId(),
-            'dst' => $cfg->observeDaylightSaving(),
+            'timezone' => $cfg->getDefaultTimezone(),
             'username' => $this->getUsername(),
         );
         if ($bk->supportsInteractiveAuthentication())
diff --git a/include/class.config.php b/include/class.config.php
index 66a607241..d47d4144f 100644
--- a/include/class.config.php
+++ b/include/class.config.php
@@ -178,17 +178,6 @@ class OsticketConfig extends Config {
                     $this->config[$key] = array('value'=>$value);
         }
 
-        //Get the default time zone
-        // We can't JOIN timezone table above due to upgrade support.
-        if ($this->get('default_timezone_id')) {
-            if (!$this->exists('tz_offset'))
-                $this->persist('tz_offset',
-                    Timezone::getOffsetById($this->get('default_timezone_id')));
-        } else
-            // Previous osTicket versions saved the offset value instead of
-            // a timezone instance. This is compatibility for the upgrader
-            $this->persist('tz_offset', 0);
-
         return true;
     }
 
@@ -275,9 +264,6 @@ class OsticketConfig extends Config {
     }
 
     /* Date & Time Formats */
-    function observeDaylightSaving() {
-        return ($this->get('enable_daylight_saving'));
-    }
     function getTimeFormat() {
         if ($this->get('date_formats') == 'custom')
             return $this->get('time_format');
@@ -286,6 +272,18 @@ class OsticketConfig extends Config {
     function isForce24HourTime() {
         return $this->get('date_formats') == '24';
     }
+    /**
+     * getDateFormat
+     *
+     * Retrieve the current date format for the system, as a string, and in
+     * the intl (icu) format.
+     *
+     * Parameters:
+     * $propogate - (boolean:default=false), if set and the configuration
+     *      indicates default date and time formats (ie. not custom), then
+     *      the intl date formatter will be queried to find the pattern used
+     *      internally for the current locale settings.
+     */
     function getDateFormat($propogate=false) {
         if ($this->get('date_formats') == 'custom')
             return $this->get('date_format');
@@ -336,10 +334,6 @@ class OsticketConfig extends Config {
         return rtrim($this->getUrl(),'/');
     }
 
-    function getTZOffset() {
-        return $this->get('tz_offset');
-    }
-
     function getPageSize() {
         return $this->get('max_page_size');
     }
diff --git a/include/class.staff.php b/include/class.staff.php
index e78c88dfd..33f9cef7f 100644
--- a/include/class.staff.php
+++ b/include/class.staff.php
@@ -150,10 +150,6 @@ class Staff extends AuthenticatedUser {
         return $this->isPasswdResetDue();
     }
 
-    function observeDaylight() {
-        return $this->ht['daylight_saving']?true:false;
-    }
-
     function getRefreshRate() {
         return $this->ht['auto_refresh_rate'];
     }
diff --git a/include/i18n/en_US/help/tips/settings.system.yaml b/include/i18n/en_US/help/tips/settings.system.yaml
index 138b6cdb8..cfd4d3ef6 100644
--- a/include/i18n/en_US/help/tips/settings.system.yaml
+++ b/include/i18n/en_US/help/tips/settings.system.yaml
@@ -81,13 +81,15 @@ default_name_formatting:
 date_time_options:
     title: Date &amp; Time Options
     content: >
-        The following settings define the Date &amp; Time settings for
-        Clients. You can change how these appear by following the PHP date
-        format characters. The dates shown below simply illustrate the
-        result of their corresponding values.
+        The following settings define the default settings for Date &amp;
+        Time settings for the help desk. You can choose to use the locale
+        defaults for the selected locale or use customize the formats to
+        meet your unique requirements. Refer to the ICU format strings as a
+        reference for customization.  The dates shown below simply
+        illustrate the result of their corresponding values.
     links:
-      - title: See the PHP Date Formatting Table
-        href: http://www.php.net/manual/en/function.date.php
+      - title: See the ICU Date Formatting Table
+        href: http://userguide.icu-project.org/formatparse/datetime
 
 languages:
     title: System Languages
diff --git a/include/upgrader/streams/core/b26f29a6-00000000.task.php b/include/upgrader/streams/core/b26f29a6-00000000.task.php
new file mode 100644
index 000000000..dcf1fe680
--- /dev/null
+++ b/include/upgrader/streams/core/b26f29a6-00000000.task.php
@@ -0,0 +1,73 @@
+<?php
+
+class IntlMigrator extends MigrationTask {
+    static $dateToIntl = array(
+        'd' => 'dd',
+        'D' => 'EEE',
+        'j' => 'd',
+        'l' => 'EEEE',
+        'N' => 'e',
+        'w' => 'c',
+        'z' => 'D',
+
+        'W' => 'w',
+
+        'F' => 'MMMM',
+        'm' => 'MM',
+        'M' => 'MMM',
+        'n' => 'M',
+
+        'o' => 'Y',
+        'Y' => 'y',
+        'y' => 'yy',
+
+        'A' => 'a',
+        'g' => 'h',
+        'G' => 'H',
+        'h' => 'hh',
+        'H' => 'HH',
+        'i' => 'mm',
+        's' => 'ss',
+        'u' => 'SSSSSS',
+
+        'e' => 'VV',
+        'O' => 'ZZZ',
+        'P' => 'ZZZZZ',
+        'T' => 'z',
+
+        'c' => "yyyy-MM-dd'T'HH:mm:ssXXXXX",
+        'r' => 'EEE, d MMM yyyy HH:mm:ss XXXXX',
+    );
+
+    function run($max_time) {
+        global $cfg;
+
+        // Detect rough install date — rationale: the schema_signature is
+        // touched by the database migrater; however the updated timestamp
+        // associated with it is not touched.
+        $install_date = $cfg->lastModified('schema_signature');
+        $touched = false;
+
+        // Upgrade date formats
+        foreach (
+            array('datetime_format', 'daydatetime_format', 'time_format', 'date_format')
+            as $key
+        ) {
+            $new_format = '';
+            $format = $cfg->get($key);
+            foreach (str_split($format) as $char) {
+                $new_format .= @$dateToIntl[$char] ?: $char;
+            }
+            $cfg->set($key, $new_format);
+
+            // Consider the last-updated time of the key to see if the
+            // format has been modified since installation
+            $touched |= $cfg->lastModified($key) != $install_date;
+        }
+
+        // Add in new custom date format flag
+        $cfg->set('date_formats', $touched ? 'custom' : '' );
+    }
+}
+
+return 'IntlMigrator';
-- 
GitLab