Example #1
0
 /**
  * Get a list of localized timezone names
  *
  * @return array
  */
 public static function getList()
 {
     $xoops = \Xoops::getInstance();
     $locale = \Xoops\Locale::getCurrent();
     $key = ['system', 'lists', 'timezone', $locale];
     //$xoops->cache()->delete($key);
     $timeZones = $xoops->cache()->cacheRead($key, function () {
         $timeZones = array();
         $territories = Territory::getContinentsAndCountries();
         $maxLen = 0;
         $utcDtz = new \DateTimeZone('UTC');
         foreach ($territories as $byContinent) {
             $continent = $byContinent['name'];
             foreach ($byContinent['children'] as $cCode => $cName) {
                 $allZones = $utcDtz->listIdentifiers(\DateTimeZone::PER_COUNTRY, $cCode);
                 foreach ($allZones as $zone) {
                     $maxLen = max(strlen($zone), $maxLen);
                     $name = Calendar::getTimezoneExemplarCity($zone);
                     if (!isset($timeZones[$zone]) && !empty($name)) {
                         $timeZones[$zone] = $continent . '/' . $name;
                     }
                 }
             }
         }
         \XoopsLocale::asort($timeZones);
         $default = array('UTC' => Calendar::getTimezoneNameNoLocationSpecific(new \DateTimeZone('GMT')));
         $timeZones = array_merge($default, $timeZones);
         return $timeZones;
     });
     return $timeZones;
 }
Example #2
0
 /**
  * Get a list of localized month names
  *
  * @param string $width The format name; it can be 'wide' (eg 'January'),
  *                      'abbreviated' (eg 'Jan') or 'narrow' (eg 'J').
  *
  * @return array
  */
 public static function getList($width = 'wide')
 {
     $months = array();
     for ($month = 1; $month <= 12; ++$month) {
         $months[$month] = Calendar::getMonthName($month, $width);
     }
     return $months;
 }
Example #3
0
 /**
  * parse a time input according to a locale and apply it to a DateTime object
  *
  * @param \DateTime $datetime datetime to apply time to
  * @param string    $input    localized time string
  * @param string    $locale   optional locale to use, leave blank to use current
  *
  * @return void
  *
  * @throws \Punic\Exception\BadArgumentType
  * @throws \Punic\Exception\ValueNotInList
  */
 protected static function parseInputTime(\DateTime $datetime, $input, $locale = '')
 {
     $timeFormat = Calendar::getTimeFormat('short', $locale);
     $am = Calendar::getDayperiodName('am', 'wide', $locale);
     $pm = Calendar::getDayperiodName('pm', 'wide', $locale);
     $clock12 = Calendar::has12HoursClock($locale);
     $hour = 0;
     $minute = 0;
     $second = 0;
     $order = [];
     $formatChars = static::utf8StringToChars($timeFormat);
     $state = 'non';
     $newstate = $state;
     foreach ($formatChars as $char) {
         switch ($char) {
             case 'h':
             case 'H':
                 $newstate = 'h';
                 break;
             case 'm':
                 $newstate = 'm';
                 break;
             case 'a':
             default:
                 $newstate = 'non';
                 break;
         }
         if ($newstate !== $state) {
             if (in_array($newstate, ['h', 'm'])) {
                 $order[] = $newstate;
             }
             $state = $newstate;
         }
     }
     $pieces = [];
     $pieceIndex = -1;
     $inputChars = static::utf8StringToChars($input);
     $state = 'non';
     $newstate = $state;
     foreach ($inputChars as $char) {
         switch ($char) {
             case '0':
             case '1':
             case '2':
             case '3':
             case '4':
             case '5':
             case '6':
             case '7':
             case '8':
             case '9':
                 $newstate = 'digit';
                 break;
             default:
                 $newstate = 'non';
                 break;
         }
         if ($newstate !== $state) {
             if ($newstate === 'digit') {
                 $pieces[++$pieceIndex] = $char;
             }
             $state = $newstate;
         } elseif ($state === 'digit') {
             $pieces[$pieceIndex] .= $char;
         }
     }
     foreach ($pieces as $i => $piece) {
         $piece = (int) ltrim($piece, '0');
         switch ($order[$i]) {
             case 'h':
                 $hour = $piece;
                 break;
             case 'm':
                 $minute = $piece;
                 break;
         }
     }
     if ($clock12) {
         if ($hour == 12 && false !== mb_strpos($input, $am)) {
             $hour = 0;
         }
         if (false !== mb_strpos($input, $pm)) {
             $hour += 12;
         }
     }
     $datetime->setTime($hour, $minute, $second);
 }
Example #4
0
 /**
  * Function to display formatted times in user timezone
  *
  * @param mixed  $time
  * @param string $format Format codes ()
  *                       's' or 'short'  - short;
  *                       'm' or 'medium' - medium;
  *                       'l' or 'long'   - long;
  *                       'c' or 'custom' - format determined according to interval to present;
  *                       'e' or 'elapse' - Elapsed;
  *                       'mysql' - Y-m-d H:i:s;
  *                       'rss'
  *
  * @return string
  */
 public static function formatTimestamp($time, $format = 'l')
 {
     $workingTime = Time::cleanTime($time);
     switch (strtolower($format)) {
         case 'short':
         case 's':
             return Time::formatDateTime($workingTime, 'short');
         case 'medium':
         case 'm':
             return Time::formatDateTime($workingTime, 'medium');
         case 'long':
         case 'l':
             return Time::formatDateTime($workingTime, 'long');
         case 'full':
         case 'f':
             return Time::formatDateTime($workingTime, 'full');
         case 'custom':
         case 'c':
             $specialName = Calendar::getDateRelativeName($workingTime, true);
             if ($specialName != '') {
                 return $specialName;
             }
             // no break - fall through
         // no break - fall through
         case 'elapse':
         case 'e':
             return Time::describeRelativeInterval($workingTime);
         case 'short-date':
             return Time::formatDate($workingTime, 'short');
         case 'short-time':
             return Time::formatTime($workingTime, 'short');
         case 'medium-date':
             return Time::formatDate($workingTime, 'medium');
         case 'medium-time':
             return Time::formatTime($workingTime, 'medium');
         case 'long-date':
             return Time::formatDate($workingTime, 'long');
         case 'long-time':
             return Time::formatTime($workingTime, 'long');
         case 'full-date':
             return Time::formatDate($workingTime, 'full');
         case 'full-time':
             return Time::formatTime($workingTime, 'full');
         case 'rss':
             $workingTime->setTimezone(new \DateTimeZone('UTC'));
             return $workingTime->format($workingTime::RSS);
         case 'mysql':
             $workingTime->setTimezone(new \DateTimeZone('UTC'));
             return $workingTime->format('Y-m-d H:i:s');
         default:
             if ($format != '') {
                 return $workingTime->format($format);
             }
             return Time::formatDateTime($workingTime, 'long');
             break;
     }
 }