ICanBoogie/CLDR v1.5.0
  • Namespace
  • Class

Namespaces

  • ICanBoogie
    • CLDR

Classes

  • Calendar
  • CalendarCollection
  • Currency
  • CurrencyCollection
  • CurrencyFormatter
  • DateFormatter
  • DateTimeFormatter
  • FileProvider
  • ListFormatter
  • Locale
  • LocaleCollection
  • LocalizedCurrency
  • LocalizedDateTime
  • LocalizedListFormatter
  • LocalizedLocale
  • LocalizedNumberFormatter
  • LocalizedObject
  • LocalizedObjectWithFormatter
  • LocalizedTerritory
  • NumberFormatter
  • NumberPattern
  • NumberPatternParser
  • Numbers
  • ProviderCollection
  • RedisProvider
  • Repository
  • RunTimeProvider
  • Supplemental
  • Territory
  • TerritoryCollection
  • TimeFormatter
  • WebProvider

Interfaces

  • Exception
  • Formatter
  • LocalizeAwareInterface
  • Provider

Traits

  • CodePropertyTrait
  • CollectionTrait
  • LocalePropertyTrait
  • ProviderStorageBinding
  • RepositoryPropertyTrait

Exceptions

  • ResourceNotFound
  • TerritoryNotDefined
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 
<?php

/*
 * This file is part of the ICanBoogie package.
 *
 * (c) Olivier Laviale <olivier.laviale@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace ICanBoogie\CLDR;

use ICanBoogie\Accessor\AccessorTrait;

/**
 * Provides date and time localization.
 *
 * The class allows you to format dates and times in a locale-sensitive manner using
 * {@link http://www.unicode.org/reports/tr35/#Date_Format_Patterns Unicode format patterns}.
 *
 * @property-read Calendar $calendar The calendar used by the formatter.
 */
class DateTimeFormatter implements Formatter
{
    use AccessorTrait;

    /**
     * Pattern characters mapping to the corresponding translator methods.
     *
     * @var array
     */
    static protected $formatters = [

        'G' => 'format_era',
        'y' => 'format_year',
//      'Y' => Year (in "Week of Year" based calendars).
//      'u' => Extended year.
        'Q' => 'format_quarter',
        'q' => 'format_standalone_quarter',
        'M' => 'format_month',
        'L' => 'format_standalone_month',
//      'l' => Special symbol for Chinese leap month, used in combination with M. Only used with the Chinese calendar.
        'w' => 'format_week_of_year',
        'W' => 'format_week_of_month',
        'd' => 'format_day_of_month',
        'D' => 'format_day_of_year',
        'F' => 'format_day_of_week_in_month',

        'h' => 'format_hour12',
        'H' => 'format_hour24',
        'm' => 'format_minutes',
        's' => 'format_seconds',
        'E' => 'format_day_in_week',
        'c' => 'format_day_in_week',
        'e' => 'format_day_in_week',
        'a' => 'format_period',
        'k' => 'format_hour_in_day',
        'K' => 'format_hour_in_period',
        'z' => 'format_timezone',
        'Z' => 'format_timezone',
        'v' => 'format_timezone'

    ];

    /**
     * Parses the datetime format pattern.
     *
     * @param string $pattern The pattern to be parsed.
     *
     * @return array Tokenized parsing result.
     */
    static protected function tokenize($pattern)
    {
        static $formats = [];

        if (isset($formats[$pattern]))
        {
            return $formats[$pattern];
        }

        $tokens = [];
        $is_literal = false;
        $literal = '';

        for ($i = 0, $n = strlen($pattern) ; $i < $n ; ++$i)
        {
            $c = $pattern{$i};

            if ($c === "'")
            {
                if ($i < $n-1 && $pattern{$i+1} === "'")
                {
                    $tokens[] = "'";
                    $i++;
                }
                else if ($is_literal)
                {
                    $tokens[] = $literal;
                    $literal = '';
                    $is_literal = false;
                }
                else
                {
                    $is_literal = true;
                    $literal = '';
                }
            }
            else if ($is_literal)
            {
                $literal .= $c;
            }
            else
            {
                for ($j = $i + 1 ; $j < $n ; ++$j)
                {
                    if ($pattern{$j} !== $c) break;
                }

                $l = $j-$i;
                $p = str_repeat($c, $l);

                $tokens[] = isset(self::$formatters[$c]) ? [ self::$formatters[$c], $p, $l ] : $p;

                $i = $j - 1;
            }
        }

        if ($literal)
        {
            $tokens[] = $literal;
        }

        return $formats[$pattern] = $tokens;
    }

    /**
     * The calendar used to format the datetime.
     *
     * @var Calendar
     */
    protected $calendar;

    /**
     * @return Calendar
     */
    protected function get_calendar()
    {
        return $this->calendar;
    }

    /**
     * Initializes the {@link $calendar} property.
     *
     * @param Calendar $calendar
     */
    public function __construct(Calendar $calendar)
    {
        $this->calendar = $calendar;
    }

    /**
     * Alias to the {@link format()} method.
     *
     * @param mixed $datetime
     * @param string $pattern_or_width_or_skeleton
     *
     * @return string
     */
    public function __invoke($datetime, $pattern_or_width_or_skeleton)
    {
        return $this->format($datetime, $pattern_or_width_or_skeleton);
    }

    /**
     * Formats a date according to a pattern.
     *
     * <pre>
     * <?php
     *
     * $datetime_formatter = $repository->locales['en']->calendar->datetime_formatter;
     * $datetime = '2013-11-02 22:23:45';
     *
     * echo $datetime_formatter($datetime, "MMM d, y");                 // November 2, 2013 at 10:23:45 PM
     * echo $datetime_formatter($datetime, "MMM d, y 'at' hh:mm:ss a"); // November 2, 2013 at 10:23:45 PM
     * echo $datetime_formatter($datetime, 'full');                     // Saturday, November 2, 2013 at 10:23:45 PM CET
     * echo $datetime_formatter($datetime, 'long');                     // November 2, 2013 at 10:23:45 PM CET
     * echo $datetime_formatter($datetime, 'medium');                   // Nov 2, 2013, 10:23:45 PM
     * echo $datetime_formatter($datetime, 'short');                    // 11/2/13, 10:23 PM
     * echo $datetime_formatter($datetime, ':Ehm');                     // Sat 10:23 PM
     * </pre>
     *
     * @param \DateTimeInterface|string|int $datetime The datetime to format.
     * @param string $pattern_or_width_or_skeleton The datetime can be formatted using a pattern,
     * a width or a skeleton. The following width are defined: "full", "long", "medium" and "short".
     * To format the datetime using a so-called "skeleton", the skeleton identifier must be
     * prefixed with the colon sign ":" e.g. ":Ehm". The skeleton identifies the patterns defined
     * under `availableFormats`.
     *
     * @return string The formatted date time.
     *
     * @see http://www.unicode.org/reports/tr35/#Date_Format_Patterns
     */
    public function format($datetime, $pattern_or_width_or_skeleton)
    {
        $datetime = $this->ensure_datetime($datetime);
        $datetime = new DateTimeAccessor($datetime);
        $pattern = $this->resolve_pattern($pattern_or_width_or_skeleton);
        $tokens = self::tokenize($pattern);

        $rc = '';

        foreach ($tokens as $token)
        {
            if (is_array($token)) // a callback: method name, sub-pattern
            {
                $token = $this->{$token[0]}($datetime, $token[1], $token[2]);
            }

            $rc .= $token;
        }

        return $rc;
    }

    /**
     * Resolves the specified pattern, which can be a width, a skeleton or an actual pattern.
     *
     * @param string $pattern_or_width_or_skeleton
     *
     * @return string
     */
    protected function resolve_pattern($pattern_or_width_or_skeleton)
    {
        $pattern = $pattern_or_width_or_skeleton;

        if ($pattern_or_width_or_skeleton{0} === ':')
        {
            $skeleton = substr($pattern, 1);
            $available_formats = $this->calendar['dateTimeFormats']['availableFormats'];

            if (isset($available_formats[$skeleton]))
            {
                $pattern = $available_formats[$skeleton];
            }
        }
        else if (in_array($pattern = $pattern_or_width_or_skeleton, [ 'full', 'long', 'medium', 'short' ]))
        {
            $calendar = $this->calendar;
            $width = $pattern_or_width_or_skeleton;
            $datetime_pattern = $calendar['dateTimeFormats'][$width];
            $date_pattern = $calendar['dateFormats'][$width];
            $time_pattern = $calendar['timeFormats'][$width];
            $pattern = strtr($datetime_pattern, [ '{1}' => $date_pattern, '{0}' => $time_pattern ]);
        }

        return $pattern;
    }

    /**
     * Resolves widths (full, long, medium, short) into a pattern.
     *
     * @param string $pattern_or_width_or_skeleton
     * @param string $from Width Source e.g. "timeFormats".
     *
     * @return string
     */
    protected function resolve_width($pattern_or_width_or_skeleton, $from)
    {
        static $widths = [ 'full', 'long', 'medium', 'short' ];

        if (in_array($pattern_or_width_or_skeleton, $widths))
        {
            return $this->calendar[$from][$pattern_or_width_or_skeleton];
        }

        return $pattern_or_width_or_skeleton;
    }

    /*
     * era (G)
     */

    /**
     * Era - Replaced with the Era string for the current date. One to three letters for the
     * abbreviated form, four letters for the long form, five for the narrow form. [1..3,4,5]
     *
     * @param DateTimeAccessor $datetime
     * @param string $pattern a pattern.
     * @param int $length Number of repetition.
     *
     * @return string era
     * @todo How to support multiple Eras?, e.g. Japanese.
     */
    protected function format_era(DateTimeAccessor $datetime, $pattern, $length)
    {
        $era = ($datetime->year > 0) ? 1 : 0;

        switch($length)
        {
            case 1:
            case 2:
            case 3: return $this->calendar->abbreviated_eras[$era];
            case 4: return $this->calendar->wide_eras[$era];
            case 5: return $this->calendar->narrow_eras[$era];
        }
    }

    /*
     * year (y)
     */

    /**
     * Year. Normally the length specifies the padding, but for two letters it also specifies the
     * maximum length. [1..n]
     *
     * @param DateTimeAccessor $datetime
     * @param string $pattern a pattern.
     * @param int $length Number of repetition.
     *
     * @return string formatted year
     */
    protected function format_year(DateTimeAccessor $datetime, $pattern, $length)
    {
        $year = $datetime->year;

        if ($length == 2)
        {
            $year = $year % 100;
        }

        return str_pad($year, $length, '0', STR_PAD_LEFT);
    }

    /*
     * quarter (Q,q)
     */

    /**
     * Quarter - Use one or two "Q" for the numerical quarter, three for the abbreviation, or four
     * for the full (wide) name. [1..2,3,4]
     *
     * @param DateTimeAccessor $datetime Datetime.
     * @param string $pattern Pattern.
     * @param int $length Number of repetition.
     *
     * @return string
     */
    protected function format_quarter(DateTimeAccessor $datetime, $pattern, $length)
    {
        $quarter = $datetime->quarter;

        switch ($length)
        {
            case 1: return $quarter;
            case 2: return str_pad($quarter, 2, '0', STR_PAD_LEFT);
            case 3: return $this->calendar->abbreviated_quarters[$quarter];
            case 4: return $this->calendar->wide_quarters[$quarter];
        }
    }

    /**
     * Stand-Alone Quarter - Use one or two "q" for the numerical quarter, three for the
     * abbreviation, or four for the full (wide) name. [1..2,3,4]
     *
     * @param DateTimeAccessor $datetime Datetime.
     * @param string $pattern a pattern.
     * @param int $length Number of repetition.
     *
     * @return string
     */
    protected function format_standalone_quarter(DateTimeAccessor $datetime, $pattern, $length)
    {
        $quarter = $datetime->quarter;

        switch ($length)
        {
            case 1: return $quarter;
            case 2: return str_pad($quarter, 2, '0', STR_PAD_LEFT);
            case 3: return $this->calendar->standalone_abbreviated_quarters[$quarter];
            case 4: return $this->calendar->standalone_wide_quarters[$quarter];
        }
    }

    /*
     * month (M|L)
     */

    /**
     * Month - Use one or two "M" for the numerical month, three for the abbreviation, four for
     * the full name, or five for the narrow name. [1..2,3,4,5]
     *
     * @param DateTimeAccessor $datetime
     * @param string $pattern a pattern.
     * @param int $length Number of repetition.
     *
     * @return string
     */
    protected function format_month(DateTimeAccessor $datetime, $pattern, $length)
    {
        $month = $datetime->month;

        switch ($length)
        {
            case 1: return $month;
            case 2: return str_pad($month, 2, '0', STR_PAD_LEFT);
            case 3: return $this->calendar->abbreviated_months[$month];
            case 4: return $this->calendar->wide_months[$month];
            case 5: return $this->calendar->narrow_months[$month];
        }
    }

    /**
     * Stand-Alone Month - Use one or two "L" for the numerical month, three for the abbreviation,
     * or four for the full (wide) name, or 5 for the narrow name. [1..2,3,4,5]
     *
     * @param DateTimeAccessor $datetime
     * @param string $pattern a pattern.
     * @param int $length Number of repetition.
     *
     * @return string formatted month.
     */
    protected function format_standalone_month(DateTimeAccessor $datetime, $pattern, $length)
    {
        $month = $datetime->month;

        switch ($length)
        {
            case 1: return $month;
            case 2: return str_pad($month, 2, '0', STR_PAD_LEFT);
            case 3: return $this->calendar->standalone_abbreviated_months[$month];
            case 4: return $this->calendar->standalone_wide_months[$month];
            case 5: return $this->calendar->standalone_narrow_months[$month];
        }
    }

    /*
     * week (w|W)
     */

    /**
     * Week of Year. [1..2]
     *
     * @param DateTimeAccessor $datetime
     * @param string $pattern a pattern.
     * @param int $length Number of repetition.
     *
     * @return integer
     */
    protected function format_week_of_year(DateTimeAccessor $datetime, $pattern, $length)
    {
        if ($length > 2)
        {
            return 0;
        }

        $week = $datetime->week;

        return $length == 1 ? $week : str_pad($week, 2, '0', STR_PAD_LEFT);
    }

    /**
     * Week of Month. [1]
     *
     * @param DateTimeAccessor $datetime
     * @param string $pattern a pattern.
     * @param int $length Number of repetition.
     *
     * @return int|false Week of month, of `false` if `$length` is greater than 1.
     */
    protected function format_week_of_month(DateTimeAccessor $datetime, $pattern, $length)
    {
        if ($length == 1)
        {
            return ceil($datetime->day / 7);
        }

        return false;
    }

    /*
     * day (d,D,F)
     */

    /**
     * Date - Day of the month. [1..2]
     *
     * @param DateTimeAccessor $datetime
     * @param string $pattern a pattern.
     * @param int $length Number of repetition.
     *
     * @return string day of the month
     */
    protected function format_day_of_month(DateTimeAccessor $datetime, $pattern, $length)
    {
        $day = $datetime->day;

        if ($length == 1)
        {
            return $day;
        }
        else if ($length == 2)
        {
            return str_pad($day, 2, '0', STR_PAD_LEFT);
        }
    }

    /**
     * Day of year. [1..3]
     *
     * @param DateTimeAccessor $datetime
     * @param string $pattern a pattern.
     * @param int $length Number of repetition.
     *
     * @return string
     */
    protected function format_day_of_year(DateTimeAccessor $datetime, $pattern, $length)
    {
        $day = $datetime->year_day;

        if ($length > 3)
        {
            return '';
        }

        return str_pad($day, $length, '0', STR_PAD_LEFT);
    }

    /**
     * Day of Week in Month. The example is for the 2nd Wed in July. [1]
     *
     * @param DateTimeAccessor $datetime
     * @param string $pattern a pattern.
     * @param int $length Number of repetition.
     *
     * @return int|false Day of week in month, or `false` if `$length` is greater than 1.
     */
    protected function format_day_of_week_in_month(DateTimeAccessor $datetime, $pattern, $length)
    {
        if ($length == 1)
        {
            return floor(($datetime->day + 6) / 7);
        }

        return false;
    }

    /*
     * weekday (E,e,c)
     */

    /**
     * Day of week - Use one through three letters for the short day, or four for the full name,
     * five for the narrow name, or six for the short name. [1..3,4,5,6]
     *
     * @param DateTimeAccessor $datetime
     * @param string $pattern a pattern.
     *
     * @return string
     */
    protected function format_day_in_week(DateTimeAccessor $datetime, $pattern)
    {
        static $translate = [

            1 => 'mon',
            2 => 'tue',
            3 => 'wed',
            4 => 'thu',
            5 => 'fri',
            6 => 'sat',
            7 => 'sun'

        ];

        $day = $datetime->weekday;

        switch ($pattern)
        {
            case 'E':
            case 'EE':
            case 'EEE':
            case 'eee':
                return $this->calendar->abbreviated_days[$translate[$day]];

            case 'EEEE':
            case 'eeee':
                return $this->calendar->wide_days[$translate[$day]];

            case 'EEEEE':
            case 'eeeee':
                return $this->calendar->narrow_days[$translate[$day]];

            case 'EEEEEE':
            case 'eeeeee':
                return $this->calendar->short_days[$translate[$day]];

            case 'e':
            case 'ee':
            case 'c':
                return $day;

            case 'ccc':
                return $this->calendar->standalone_abbreviated_days[$translate[$day]];

            case 'cccc':
                return $this->calendar->standalone_wide_days[$translate[$day]];

            case 'ccccc':
                return $this->calendar->standalone_narrow_days[$translate[$day]];

            case 'cccccc':
                return $this->calendar->standalone_short_days[$translate[$day]];
        }
    }

    /*
     * period (a)
     */

    /**
     * AM or PM. [1]
     *
     * @param DateTimeAccessor $datetime
     * @param string $pattern a pattern.
     * @param int $length Number of repetition.
     *
     * @return string AM or PM designator
     */
    protected function format_period(DateTimeAccessor $datetime, $pattern, $length)
    {
        return $this->calendar['dayPeriods']['format']['abbreviated'][$datetime->hour < 12 ? 'am' : 'pm'];
    }

    /*
     * hour (h,H,K,k)
     */

    /**
     * Hour [1-12]. When used in skeleton data or in a skeleton passed in an API for flexible data
     * pattern generation, it should match the 12-hour-cycle format preferred by the locale
     * (h or K); it should not match a 24-hour-cycle format (H or k). Use hh for zero
     * padding. [1..2]
     *
     * @param DateTimeAccessor $datetime
     * @param string $pattern a pattern.
     * @param int $length Number of repetition.
     *
     * @return string
     */
    protected function format_hour12(DateTimeAccessor $datetime, $pattern, $length)
    {
        $hour = $datetime->hour;
        $hour = ($hour == 12) ? 12 : $hour % 12;

        if ($length == 1)
        {
            return $hour;
        }
        else if ($length == 2)
        {
            return str_pad($hour, 2, '0', STR_PAD_LEFT);
        }
    }

    /**
     * Hour [0-23]. When used in skeleton data or in a skeleton passed in an API for flexible
     * data pattern generation, it should match the 24-hour-cycle format preferred by the
     * locale (H or k); it should not match a 12-hour-cycle format (h or K). Use HH for zero
     * padding. [1..2]
     *
     * @param DateTimeAccessor $datetime
     * @param string $pattern a pattern.
     * @param int $length Number of repetition.
     *
     * @return string
     */
    protected function format_hour24(DateTimeAccessor $datetime, $pattern, $length)
    {
        $hour = $datetime->hour;

        if ($length == 1)
        {
            return $hour;
        }
        else if ($length == 2)
        {
            return str_pad($hour, 2, '0', STR_PAD_LEFT);
        }
    }

    /**
     * Hour [0-11]. When used in a skeleton, only matches K or h, see above. Use KK for zero
     * padding. [1..2]
     *
     * @param DateTimeAccessor $datetime
     * @param string $pattern A pattern.
     * @param int $length Number of repetition.
     *
     * @return integer hours in AM/PM format.
     */
    protected function format_hour_in_period(DateTimeAccessor $datetime, $pattern, $length)
    {
        $hour = $datetime->hour % 12;

        if ($length == 1)
        {
            return $hour;
        }
        else if ($length == 2)
        {
            return str_pad($hour, 2, '0', STR_PAD_LEFT);
        }
    }

    /**
     * Hour [1-24]. When used in a skeleton, only matches k or H, see above. Use kk for zero
     * padding. [1..2]
     *
     * @param DateTimeAccessor $datetime
     * @param string $pattern a pattern.
     * @param int $length Number of repetition.
     *
     * @return integer
     */
    protected function format_hour_in_day(DateTimeAccessor $datetime, $pattern, $length)
    {
        $hour = $datetime->hour;

        if ($hour == 0)
        {
            $hour = 24;
        }

        if ($length == 1)
        {
            return $hour;
        }
        else if ($length == 2)
        {
            return str_pad($hour, 2, '0', STR_PAD_LEFT);
        }
    }

    /*
     * minute (m)
     */

    /**
     * Minute. Use one or two "m" for zero padding.
     *
     * @param DateTimeAccessor $datetime
     * @param string $pattern a pattern.
     * @param int $length Number of repetition
     *
     * @return string minutes.
     */
    protected function format_minutes(DateTimeAccessor $datetime, $pattern, $length)
    {
        $minutes = $datetime->minute;

        if ($length == 1)
        {
            return $minutes;
        }
        else if ($length == 2)
        {
            return str_pad($minutes, 2, '0', STR_PAD_LEFT);
        }
    }

    /*
     * second
     */

    /**
     * Second. Use one or two "s" for zero padding.
     *
     * @param DateTimeAccessor $datetime
     * @param string $pattern a pattern.
     * @param int $length Number of repetition.
     *
     * @return string seconds
     */
    protected function format_seconds(DateTimeAccessor $datetime, $pattern, $length)
    {
        $seconds = $datetime->second;

        if ($length == 1)
        {
            return $seconds;
        }
        else if ($length == 2)
        {
            return str_pad($seconds, 2, '0', STR_PAD_LEFT);
        }
    }

    /*
     * zone (z,Z,v)
     */

    /**
     * Time Zone.
     *
     * @param DateTimeAccessor $datetime
     * @param string $pattern a pattern.
     * @param int $length Number of repetition.
     *
     * @return string time zone
     */
    protected function format_timezone(DateTimeAccessor $datetime, $pattern, $length)
    {
        if ($pattern{0} === 'z' || $pattern{0} === 'v')
        {
            $str = $datetime->format('T');

            return $str === 'Z' ? 'UTC' : $str;
        }
        else if ($pattern{0} === 'Z')
        {
            return $datetime->format('O');
        }
    }

    /**
     * @param \DateTimeInterface|string|int $datetime
     *
     * @return \DateTimeInterface
     */
    private function ensure_datetime($datetime)
    {
        if ($datetime instanceof \DateTimeInterface)
        {
            return $datetime;
        }

        return new \DateTime(is_numeric($datetime) ? "@$datetime" : (string) $datetime);
    }
}
ICanBoogie/CLDR v1.5.0 API documentation generated by ApiGen