]> git.mxchange.org Git - friendica.git/blob - src/Util/DateTimeFormat.php
Add license info at Friendica PHP files
[friendica.git] / src / Util / DateTimeFormat.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Util;
23
24 use Friendica\Core\Logger;
25 use DateTime;
26 use DateTimeZone;
27 use Exception;
28
29 /**
30  * Temporal class
31  */
32 class DateTimeFormat
33 {
34         const ATOM = 'Y-m-d\TH:i:s\Z';
35         const MYSQL = 'Y-m-d H:i:s';
36         const HTTP = 'D, d M Y H:i:s \G\M\T';
37
38         /**
39          * convert() shorthand for UTC.
40          *
41          * @param string $time   A date/time string
42          * @param string $format DateTime format string or Temporal constant
43          * @return string
44          * @throws Exception
45          */
46         public static function utc($time, $format = self::MYSQL)
47         {
48                 return self::convert($time, 'UTC', 'UTC', $format);
49         }
50
51         /**
52          * convert() shorthand for local.
53          *
54          * @param string $time   A date/time string
55          * @param string $format DateTime format string or Temporal constant
56          * @return string
57          * @throws Exception
58          */
59         public static function local($time, $format = self::MYSQL)
60         {
61                 return self::convert($time, date_default_timezone_get(), 'UTC', $format);
62         }
63
64         /**
65          * convert() shorthand for timezoned now.
66          *
67          * @param        $timezone
68          * @param string $format DateTime format string or Temporal constant
69          * @return string
70          * @throws Exception
71          */
72         public static function timezoneNow($timezone, $format = self::MYSQL)
73         {
74                 return self::convert('now', $timezone, 'UTC', $format);
75         }
76
77         /**
78          * convert() shorthand for local now.
79          *
80          * @param string $format DateTime format string or Temporal constant
81          * @return string
82          * @throws Exception
83          */
84         public static function localNow($format = self::MYSQL)
85         {
86                 return self::local('now', $format);
87         }
88
89         /**
90          * convert() shorthand for UTC now.
91          *
92          * @param string $format DateTime format string or Temporal constant
93          * @return string
94          * @throws Exception
95          */
96         public static function utcNow($format = self::MYSQL)
97         {
98                 return self::utc('now', $format);
99         }
100
101         /**
102          * General purpose date parse/convert/format function.
103          *
104          * @param string $s       Some parseable date/time string
105          * @param string $tz_to   Destination timezone
106          * @param string $tz_from Source timezone
107          * @param string $format  Output format recognised from php's DateTime class
108          *                        http://www.php.net/manual/en/datetime.format.php
109          *
110          * @return string Formatted date according to given format
111          * @throws Exception
112          */
113         public static function convert($s = 'now', $tz_to = 'UTC', $tz_from = 'UTC', $format = self::MYSQL)
114         {
115                 // Defaults to UTC if nothing is set, but throws an exception if set to empty string.
116                 // Provide some sane defaults regardless.
117                 if ($tz_from === '') {
118                         $tz_from = 'UTC';
119                 }
120
121                 if ($tz_to === '') {
122                         $tz_to = 'UTC';
123                 }
124
125                 if (($s === '') || (!is_string($s))) {
126                         $s = 'now';
127                 }
128
129                 /*
130                  * Slight hackish adjustment so that 'zero' datetime actually returns what is intended
131                  * otherwise we end up with -0001-11-30 ...
132                  * add 32 days so that we at least get year 00, and then hack around the fact that
133                  * months and days always start with 1.
134                  */
135                 if (substr($s, 0, 10) <= '0001-01-01') {
136                         if ($s < '0000-00-00') {
137                                 $s = '0000-00-00';
138                         }
139                         $d = new DateTime($s . ' + 32 days', new DateTimeZone('UTC'));
140                         return str_replace('1', '0', $d->format($format));
141                 }
142
143                 try {
144                         $from_obj = new DateTimeZone($tz_from);
145                 } catch (Exception $e) {
146                         $from_obj = new DateTimeZone('UTC');
147                 }
148
149                 try {
150                         $d = new DateTime($s, $from_obj);
151                 } catch (Exception $e) {
152                         Logger::log('DateTimeFormat::convert: exception: ' . $e->getMessage());
153                         $d = new DateTime('now', $from_obj);
154                 }
155
156                 try {
157                         $to_obj = new DateTimeZone($tz_to);
158                 } catch (Exception $e) {
159                         $to_obj = new DateTimeZone('UTC');
160                 }
161
162                 $d->setTimeZone($to_obj);
163
164                 return $d->format($format);
165         }
166
167         /**
168          * Checks, if the given string is a date with the pattern YYYY-MM
169          *
170          * @param string $dateString The given date
171          *
172          * @return boolean True, if the date is a valid pattern
173          */
174         public function isYearMonth(string $dateString)
175         {
176                 // Check format (2019-01, 2019-1, 2019-10)
177                 if (!preg_match('/^([12]\d{3}-(1[0-2]|0[1-9]|\d))$/', $dateString)) {
178                         return false;
179                 }
180
181                 $date = DateTime::createFromFormat('Y-m', $dateString);
182
183                 if (!$date) {
184                         return false;
185                 }
186
187                 try {
188                         $now = new DateTime();
189                 } catch (\Throwable $t) {
190                         return false;
191                 }
192
193                 if ($date > $now) {
194                         return false;
195                 }
196
197                 return true;
198         }
199
200         /**
201          * Checks, if the given string is a date with the pattern YYYY-MM-DD
202          *
203          * @param string $dateString The given date
204          *
205          * @return boolean True, if the date is a valid pattern
206          */
207         public function isYearMonthDay(string $dateString)
208         {
209                 $date = DateTime::createFromFormat('Y-m-d', $dateString);
210                 if (!$date) {
211                         return false;
212                 }
213
214                 if (DateTime::getLastErrors()['error_count'] || DateTime::getLastErrors()['warning_count']) {
215                         return false;
216                 }
217
218                 return true;
219         }
220 }