PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.32.17
E2Pdf – Export Pdf Tool for WordPress v1.32.17
1.32.49 1.32.48 1.32.43 1.32.40 1.32.34 1.32.32 1.32.31 1.32.26 1.32.22 1.32.23 1.32.18 1.32.17 1.32.15 trunk 1.00.00 1.00.13 1.01.01 1.02.02 1.03.07 1.04.07 1.05.03 1.06.02 1.07.11 1.08.00 1.08.06 All 72 releases
e2pdf / vendors / svggraph / DateTimeFormatter.php

DateTimeFormatter.php in E2Pdf – Export Pdf Tool for WordPress 1.32.17, at vendors/svggraph/DateTimeFormatter.php

148 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Copyright (C) 2021-2022 Graham Breach
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 /**
19 * For more information, please contact <graham@goat1000.com>
20 */
21
22 namespace Goat1000\SVGGraph;
23
24 /**
25 * Class for formatting date/time values
26 */
27 class DateTimeFormatter {
28
29 protected $timezone = null;
30 protected $localize = false;
31 protected $idf = null;
32
33 public function __construct()
34 {
35 $this->timezone = new \DateTimeZone(date_default_timezone_get());
36
37 // see if output needs localization
38 if(extension_loaded('intl')) {
39 $locale = setlocale(LC_TIME, 0);
40 if($locale && $locale !== 'C' && $locale !== 'POSIX' &&
41 strpos($locale, 'en_') === false) {
42 $this->localize = true;
43 $this->idf = new \IntlDateFormatter($locale,
44 \IntlDateFormatter::FULL, \IntlDateFormatter::FULL);
45 }
46 }
47 }
48
49 /**
50 * Returns the formatted, localized date/time
51 */
52 public function format($dt, $fmt, $strip_offset = false)
53 {
54 $datetime = clone $dt;
55 $datetime->setTimezone($this->timezone);
56
57 if($strip_offset) {
58 $offset = $this->timezone->getOffset($datetime);
59 if($offset < 0)
60 $datetime->modify($offset . ' second');
61 else
62 $datetime->modify('+' . $offset . ' second');
63 }
64
65 if(!$this->localize)
66 return $datetime->format($fmt);
67
68 // DateTime class doesn't do localization, so these fields are passed to
69 // IntlDateFormatter instead
70 $map = [
71 'D' => 'E',
72 'l' => 'EEEE',
73 'M' => 'MMM',
74 'F' => 'MMMM',
75 ];
76
77 $result = '';
78 $unixtime = $datetime->format('U');
79 for($i = 0; $i < strlen($fmt); ++$i) {
80 $char = $fmt[$i];
81 if(isset($map[$char])) {
82 $this->idf->setPattern($map[$char]);
83 $result .= $this->idf->format($unixtime);
84 } else {
85 $result .= $datetime->format($fmt[$i]);
86 }
87 }
88 return $result;
89 }
90
91 /**
92 * Returns the list of day names
93 */
94 public function getLongDays()
95 {
96 return $this->getDateStrings('l', 'd');
97 }
98
99 /**
100 * Returns the list of abbreviated day names
101 */
102 public function getShortDays()
103 {
104 return $this->getDateStrings('D', 'd');
105 }
106
107 /**
108 * Returns the list of month names
109 */
110 public function getLongMonths()
111 {
112 return $this->getDateStrings('F', 'm');
113 }
114
115 /**
116 * Returns the list of abbreviated month names
117 */
118 public function getShortMonths()
119 {
120 return $this->getDateStrings('M', 'm');
121 }
122
123 /**
124 * Returns a list of day or month strings using IntlDateFormatter to localize
125 */
126 private function getDateStrings($fmt, $inc)
127 {
128 // 1978 started on a Sunday
129 $dt = new \DateTime('1978-01-01T12:00:00Z');
130 if($inc == 'm') {
131 $count = 12;
132 $offset = 'month';
133 } else {
134 $count = 7;
135 $offset = 'day';
136 }
137
138 $strings = [];
139 for($i = 0; $i < $count; ++$i) {
140 if($i)
141 $dt->modify('+1 ' . $offset);
142 $strings[] = $this->format($dt, $fmt);
143 }
144 return $strings;
145 }
146 }
147
148