summaryrefslogtreecommitdiffstats
path: root/vendor/gipfl/format
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 12:44:51 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 12:44:51 +0000
commita1ec78bf0dc93d0e05e5f066f1949dc3baecea06 (patch)
treeee596ce1bc9840661386f96f9b8d1f919a106317 /vendor/gipfl/format
parentInitial commit. (diff)
downloadicingaweb2-module-incubator-a1ec78bf0dc93d0e05e5f066f1949dc3baecea06.tar.xz
icingaweb2-module-incubator-a1ec78bf0dc93d0e05e5f066f1949dc3baecea06.zip
Adding upstream version 0.20.0.upstream/0.20.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/gipfl/format')
-rw-r--r--vendor/gipfl/format/composer.json28
-rw-r--r--vendor/gipfl/format/src/LocalDateFormat.php41
-rw-r--r--vendor/gipfl/format/src/LocalTimeFormat.php184
-rw-r--r--vendor/gipfl/format/src/LocaleAwareness.php94
4 files changed, 347 insertions, 0 deletions
diff --git a/vendor/gipfl/format/composer.json b/vendor/gipfl/format/composer.json
new file mode 100644
index 0000000..10c3b90
--- /dev/null
+++ b/vendor/gipfl/format/composer.json
@@ -0,0 +1,28 @@
+{
+ "name": "gipfl/format",
+ "description": "Arbitrary collection of Format helpers",
+ "type": "library",
+ "license": "MIT",
+ "authors": [
+ {
+ "name": "Thomas Gelf",
+ "email": "thomas@gelf.net"
+ }
+ ],
+ "config": {
+ "sort-packages": true
+ },
+ "autoload": {
+ "psr-4": {
+ "gipfl\\Format\\": "src"
+ }
+ },
+ "require": {
+ "php": ">=5.6.0",
+ "ext-intl": "*"
+ },
+ "require-dev": {
+ "phpunit/phpunit": "^9.3 || ^7.5 || ^6.5 || ^5.7",
+ "squizlabs/php_codesniffer": "^3.6"
+ }
+}
diff --git a/vendor/gipfl/format/src/LocalDateFormat.php b/vendor/gipfl/format/src/LocalDateFormat.php
new file mode 100644
index 0000000..60a29e4
--- /dev/null
+++ b/vendor/gipfl/format/src/LocalDateFormat.php
@@ -0,0 +1,41 @@
+<?php
+
+namespace gipfl\Format;
+
+use IntlDateFormatter;
+
+class LocalDateFormat
+{
+ use LocaleAwareness;
+
+ /** @var IntlDateFormatter */
+ protected $formatter;
+
+ /**
+ * @param $time
+ * @return string
+ */
+ public function getFullDay($time)
+ {
+ return $this->formatter()->format($time);
+ }
+
+ protected function formatter()
+ {
+ if ($this->formatter === null) {
+ $this->formatter = new IntlDateFormatter(
+ $this->getLocale(),
+ IntlDateFormatter::FULL,
+ IntlDateFormatter::NONE
+ );
+ $this->formatter->setTimeZone($this->getTimezone());
+ }
+
+ return $this->formatter;
+ }
+
+ protected function reset()
+ {
+ $this->formatter = null;
+ }
+}
diff --git a/vendor/gipfl/format/src/LocalTimeFormat.php b/vendor/gipfl/format/src/LocalTimeFormat.php
new file mode 100644
index 0000000..1ea3ce7
--- /dev/null
+++ b/vendor/gipfl/format/src/LocalTimeFormat.php
@@ -0,0 +1,184 @@
+<?php
+
+namespace gipfl\Format;
+
+use IntlDateFormatter;
+use RuntimeException;
+
+class LocalTimeFormat
+{
+ use LocaleAwareness;
+
+ /** @var IntlDateFormatter */
+ protected $formatter;
+
+ /**
+ * For available symbols please see:
+ * https://unicode-org.github.io/icu/userguide/format_parse/datetime/#date-field-symbol-table
+ *
+ * @param int|float $time Hint: also supports DateTime, DateTimeInterface since 7.1.5
+ * @return string
+ */
+ public function format($time, $pattern)
+ {
+ $result = $this->formatter($pattern)->format($time);
+ if ($result === false) {
+ throw new RuntimeException(sprintf(
+ 'Failed to format %s as "%s": %s (%d)',
+ $time,
+ $pattern,
+ $this->formatter->getErrorMessage(),
+ $this->formatter->getErrorCode()
+ ));
+ }
+
+ return $result;
+ }
+
+ /**
+ * @param $time
+ * @return string
+ */
+ public function getWeekOfYear($time)
+ {
+ return $this->format($time, 'ww');
+ }
+
+ /**
+ * @param $time
+ * @return int
+ */
+ public function getNumericWeekOfYear($time)
+ {
+ return (int) $this->format($time, 'w');
+ }
+
+ /**
+ * @param $time
+ * @return string
+ */
+ public function getDayInMonth($time)
+ {
+ return $this->format($time, 'dd');
+ }
+
+ /**
+ * @param $time
+ * @return int
+ */
+ public function getNumericDayInMonth($time)
+ {
+ return (int) $this->format($time, 'd');
+ }
+
+ /**
+ * @param $time
+ * @return string
+ */
+ public function getWeekdayName($time)
+ {
+ return $this->format($time, 'cccc');
+ }
+
+ /**
+ * @param $time
+ * @return string
+ */
+ public function getShortWeekdayName($time)
+ {
+ return $this->format($time, 'ccc');
+ }
+
+ /**
+ * e.g. September
+ *
+ * @param $time
+ * @return string
+ */
+ public function getMonthName($time)
+ {
+ return $this->format($time, 'LLLL');
+ }
+
+ /**
+ * e.g. Sep
+ *
+ * @param $time
+ * @return string
+ */
+ public function getShortMonthName($time)
+ {
+ return $this->format($time, 'LLL');
+ }
+
+ /**
+ * e.g. 2021
+ * @param $time
+ * @return string
+ */
+ public function getYear($time)
+ {
+ return $this->format($time, 'y');
+ }
+
+ /**
+ * e.g. 21
+ *
+ * @param $time
+ * @return string
+ */
+ public function getShortYear($time)
+ {
+ return $this->format($time, 'yy');
+ }
+
+ /**
+ * e.g. 21:50:12
+ *
+ * @param $time
+ * @return string
+ */
+ public function getTime($time)
+ {
+ if ($this->wantsAmPm()) {
+ return $this->format($time, 'h:mm:ss a');
+ }
+
+ return $this->format($time, 'H:mm:ss');
+ }
+
+ /**
+ * e.g. 21:50
+ *
+ * @param $time
+ * @return string
+ */
+ public function getShortTime($time)
+ {
+ if ($this->wantsAmPm()) {
+ return $this->format($time, 'K:mm a');
+ }
+
+ return $this->format($time, 'H:mm');
+ }
+
+ protected function formatter($pattern)
+ {
+ if ($this->formatter === null) {
+ $this->formatter = new IntlDateFormatter(
+ $this->getLocale(),
+ IntlDateFormatter::GREGORIAN,
+ IntlDateFormatter::GREGORIAN
+ );
+ $this->formatter->setTimeZone($this->getTimezone());
+ }
+ $this->formatter->setPattern($pattern);
+
+ return $this->formatter;
+ }
+
+ protected function reset()
+ {
+ $this->formatter = null;
+ }
+}
diff --git a/vendor/gipfl/format/src/LocaleAwareness.php b/vendor/gipfl/format/src/LocaleAwareness.php
new file mode 100644
index 0000000..cb1a417
--- /dev/null
+++ b/vendor/gipfl/format/src/LocaleAwareness.php
@@ -0,0 +1,94 @@
+<?php
+
+namespace gipfl\Format;
+
+use DateTimeZone;
+use IntlTimeZone;
+use RuntimeException;
+
+trait LocaleAwareness
+{
+ /** @var string */
+ protected $locale;
+
+ /** @var DateTimeZone|IntlTimeZone */
+ protected $timezone;
+
+ /**
+ * @param string $locale
+ * @return void
+ */
+ public function setLocale($locale)
+ {
+ if ($this->locale !== $locale) {
+ $this->locale = (string) $locale;
+ $this->reset();
+ }
+ }
+
+ /**
+ * @param DateTimeZone|IntlTimeZone $timezone
+ * @return void
+ */
+ public function setTimezone($timezone)
+ {
+ // Hint: type checking is delegated to timeZonesAreEqual
+ if (self::timeZonesAreEqual($this->timezone, $timezone)) {
+ return;
+ }
+
+ $this->timezone = $timezone;
+ $this->reset();
+ }
+
+ protected function wantsAmPm()
+ {
+ // TODO: complete this list
+ return in_array($this->getLocale(), ['en_US', 'en_US.UTF-8']);
+ }
+
+ protected function isUsEnglish()
+ {
+ return in_array($this->getLocale(), ['en_US', 'en_US.UTF-8']);
+ }
+
+ protected function getLocale()
+ {
+ if ($this->locale === null) {
+ $this->locale = setlocale(LC_TIME, 0) ?: 'C';
+ }
+
+ return $this->locale;
+ }
+
+ protected function getTimezone()
+ {
+ if ($this->timezone === null) {
+ $this->timezone = new DateTimeZone(date_default_timezone_get());
+ }
+
+ return $this->timezone;
+ }
+
+ protected static function timeZonesAreEqual($left, $right)
+ {
+ if ($left === null && $right !== null) {
+ return false;
+ }
+ if ($left !== null && $right === null) {
+ return false;
+ }
+ if ($left instanceof DateTimeZone) {
+ return $right instanceof DateTimeZone && $left->getName() === $right->getName();
+ }
+ if ($left instanceof IntlTimeZone) {
+ return $right instanceof IntlTimeZone && $left->getID() === $right->getID();
+ }
+
+ throw new RuntimeException(sprintf(
+ 'Valid timezone expected, got left=%s, right=%s',
+ is_object($left) ? get_class($left) : gettype($left),
+ is_object($right) ? get_class($right) : gettype($right)
+ ));
+ }
+}