1<?php 2 3// phpcs:disable Generic.Files.LineLength.TooLong 4 5namespace dokuwiki\plugin\struct\meta; 6 7/** 8 * Class DateFormatConverter 9 * 10 * Allows conversion between the two format strings used in PHP. Not all placeholders are available in both 11 * formats. The conversion tries will use similar but not exactly the same placeholders if possible. When no suitable 12 * replacement can be found, the placeholder is removed. 13 * 14 * Do not use this where formats are used in creating machine readable data (like feeds, APIs whatever). This is 15 * only meant for cases where human read output is created. 16 * 17 * @package dokuwiki\plugin\struct\meta 18 */ 19class DateFormatConverter 20{ 21 protected static $strftime = [ 22 // Day 23 '%a' => 'D', 24 // An abbreviated textual representation of the day Sun through Sat 25 '%A' => 'l', 26 // A full textual representation of the day Sunday through Saturday 27 '%d' => 'd', 28 // Two-digit day of the month (with leading zeros) 01 to 31 29 '%e' => 'j', 30 // Day of the month, with a space preceding single digits. Not implemented as described on Windows. See below for more information. 1 to 31 31 '%j' => '', 32 // NOT SUPPORTED Day of the year, 3 digits with leading zeros 001 to 366 33 '%u' => 'N', 34 // ISO-8601 numeric representation of the day of the week 1 (for Monday) through 7 (for Sunday) 35 '%w' => 'w', 36 // Numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday) 37 // Week 38 '%U' => '', 39 // NOT SUPPORTED Week number of the given year, starting with the first Sunday as the first week 13 (for the 13th full week of the year) 40 '%V' => 'W', 41 // ISO-8601:1988 week number of the given year, starting with the first week of the year with at least 4 weekdays, with Monday being the start of the week 01 through 53 (where 53 accounts for an overlapping week) 42 '%W' => '', 43 // NOT SUPPORTED A numeric representation of the week of the year, starting with the first Monday as the first week 46 (for the 46th week of the year beginning with a Monday) 44 // Month 45 '%b' => 'M', 46 // Abbreviated month name, based on the locale Jan through Dec 47 '%B' => 'F', 48 // Full month name, based on the locale January through December 49 '%h' => 'M', 50 // Abbreviated month name, based on the locale (an alias of %b) Jan through Dec 51 '%m' => 'm', 52 // Two digit representation of the month 01 (for January) through 12 (for December) 53 // Year 54 '%C' => '', 55 // NOT SUPPORTED Two digit representation of the century (year divided by 100, truncated to an integer) 19 for the 20th Century 56 '%g' => 'y', 57 // Two digit representation of the year going by ISO-8601:1988 standards (see %V) Example: 09 for the week of January 6, 2009 58 '%G' => 'Y', 59 // The full four-digit version of %g Example: 2008 for the week of January 3, 2009 60 '%y' => 'y', 61 // Two digit representation of the year Example: 09 for 2009, 79 for 1979 62 '%Y' => 'Y', 63 // Four digit representation for the year Example: 2038 64 // Time 65 '%H' => 'H', 66 // Two digit representation of the hour in 24-hour format 00 through 23 67 '%k' => 'G', 68 // Two digit representation of the hour in 24-hour format, with a space preceding single digits 0 through 23 69 '%I' => 'h', 70 // Two digit representation of the hour in 12-hour format 01 through 12 71 '%l' => 'g', 72 // (lower-case 'L') Hour in 12-hour format, with a space preceding single digits 1 through 12 73 '%M' => 'i', 74 // Two digit representation of the minute 00 through 59 75 '%p' => 'A', 76 // UPPER-CASE 'AM' or 'PM' based on the given time Example: AM for 00:31, PM for 22:23 77 '%P' => 'a', 78 // lower-case 'am' or 'pm' based on the given time Example: am for 00:31, pm for 22:23 79 '%r' => 'h:i:s A', 80 // Same as %I:%M:%S %p Example: 09:34:17 PM for 21:34:17 81 '%R' => 'H:i', 82 // Same as %H:%M Example: 00:35 for 12:35 AM, 16:44for 4:44 PM 83 '%S' => 's', 84 // Two digit representation of the second 00 through 59 85 '%T' => 'H:i:s', 86 // Same as %H:%M:%S Example: 21:34:17 for 09:34:17 PM 87 '%X' => 'H:i:s', 88 // Preferred time representation based on locale, without the date Example: 03:59:16 or 15:59:16 89 '%z' => 'z', 90 // The time zone offset. Not implemented as described on Windows. See below for more information. Example: -0500 for US Eastern Time 91 '%Z' => 'T', 92 // The time zone abbreviation. Not implemented as described on Windows. See below for more information. Example: EST for Eastern Time 93 // Time and Date Stamps 94 '%c' => 'D M j H:i:s Y', 95 // Preferred date and time stamp based on locale Example: Tue Feb 5 00:45:10 2009 for February 5, 2009 at 12:45:10 AM 96 '%D' => 'm/d/y', 97 // Same as %m/%d/%y Example: 02/05/09 for February 5, 2009 98 '%F' => 'Y/m/d', 99 // Same as %Y-%m-%d (commonly used in database datestamps) Example: 2009-02-05 for February 5, 2009 100 '%s' => 'U', 101 // Unix Epoch Time timestamp (same as the time() function) Example: 305815200 for September 10, 1979 08:40:00 AM 102 '%x' => 'm/d/y', 103 // Preferred date representation based on locale, without the time Example: 02/05/09 for February 5, 2009 104 // Miscellaneous 105 '%n' => "\n", 106 // A newline character (\n) --- 107 '%t' => "\t", 108 // A Tab character (\t) --- 109 '%%' => '%', 110 ]; 111 112 protected static $date = [ 113 // Day 114 'd' => '%d', 115 // Day of the month, 2 digits with leading zeros 01 to 31 116 'D' => '%a', 117 // A textual representation of a day, three letters Mon through Sun 118 'j' => '%e', 119 // Day of the month without leading zeros 1 to 31 120 'l' => '%A', 121 // (lowercase 'L') A full textual representation of the day of the week Sunday through Saturday 122 'N' => '%u', 123 // ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0) 1 (for Monday) through 7 (for Sunday) 124 'S' => '', 125 // NOT SUPPORTED English ordinal suffix for the day of the month, 2 characters st, nd, rd or th. Works well with j 126 'w' => '%w', 127 // Numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday) 128 'z' => '', 129 // NOT SUPPORTED The day of the year (starting from 0) 0 through 365 130 // Week 131 'W' => '%V', 132 // ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0) Example: 42 (the 42nd week in the year) 133 // Month 134 'F' => '%B', 135 // A full textual representation of a month, such as January or March January through December 136 'm' => '%m', 137 // Numeric representation of a month, with leading zeros 01 through 12 138 'M' => '%b', 139 // A short textual representation of a month, three letters Jan through Dec 140 'n' => '%m', 141 // Numeric representation of a month, without leading zeros 1 through 12 142 't' => '', 143 // NOT SUPPORTED Number of days in the given month 28 through 31 144 // Year 145 'L' => '', 146 // NOT SUPPORTED Whether it's a leap year 1 if it is a leap year, 0 otherwise. 147 'o' => '%g', 148 // ISO-8601 week-numbering year. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0) Examples: 1999or 2003 149 'Y' => '%Y', 150 // A full numeric representation of a year, 4 digits Examples: 1999or 2003 151 'y' => '%y', 152 // A two digit representation of a year Examples: 99 or03 153 // Time 154 'a' => '%P', 155 // Lowercase Ante meridiem and Post meridiem am or pm 156 'A' => '%p', 157 // Uppercase Ante meridiem and Post meridiem AM or PM 158 'B' => '', 159 // NOT SUPPORTED Swatch Internet time 000 through 999 160 'g' => '%l', 161 // 12-hour format of an hour without leading zeros 1 through 12 162 'G' => '%k', 163 // 24-hour format of an hour without leading zeros 0 through 23 164 'h' => '%I', 165 // 12-hour format of an hour with leading zeros 01 through 12 166 'H' => '%H', 167 // 24-hour format of an hour with leading zeros 00 through 23 168 'i' => '%M', 169 // Minutes with leading zeros 00 to 59 170 's' => '%S', 171 // Seconds, with leading zeros 00 through 59 172 'u' => '%s000000', 173 // Microseconds (added in PHP 5.2.2). Note that date() will always generate000000 since it takes an integer parameter, whereas DateTime::format()does support microseconds if DateTime was created with microseconds. Example: 654321 174 // Timezone 175 'e' => '%Z', 176 // Timezone identifier (added in PHP 5.1.0) Examples: UTC,GMT,Atlantic/Azores 177 'I' => '', 178 // NOT SUPPORTED (capital i) Whether or not the date is in daylight saving time 1 if Daylight Saving Time, 0otherwise. 179 'O' => '%z', 180 // Difference to Greenwich time (GMT) in hours Example: +0200 181 'P' => '%z', 182 // Difference to Greenwich time (GMT) with colon between hours and minutes (added in PHP 5.1.3) Example: +02:00 183 'T' => '%Z', 184 // Timezone abbreviation Examples: EST,MDT ... 185 'Z' => '', 186 // NOT SUPPORTED Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive. -43200 through50400 187 // Full Date/Time 188 'c' => '', 189 // NOT SUPPORTED ISO 8601 date (added in PHP 5) 2004-02-12T15:19:21+00:00 190 'r' => '%a, %e %b %Y %H:%M:%S %s', 191 // » RFC 2822 formatted date Example: Thu, 21 Dec 2000 16:01:07 +0200 192 'U' => '%s', 193 ]; 194 195 /** 196 * Convert a strftime format string to a date format string 197 * 198 * @param string $strftime 199 * @return string 200 */ 201 public static function toDate($strftime) 202 { 203 $date = $strftime; 204 205 /* All characters that are not strftime placeholders need to be escaped */ 206 { 207 $datekeys = array_keys(self::$date); 208 // create negative lookbehind regex to match all known date chars that are not a strtime pattern now 209 $from = array_map( 210 function ($in) { 211 return '/(?<!%)' . $in . '/'; 212 }, 213 $datekeys 214 ); 215 // those need to be escaped 216 $to = array_map( 217 function ($in) { 218 return '\\' . $in; 219 }, 220 $datekeys 221 ); 222 // escape date chars 223 $date = preg_replace($from, $to, $date); 224 } 225 226 /* strftime to date conversion */ 227 { 228 $date = str_replace( 229 array_keys(self::$strftime), 230 array_values(self::$strftime), 231 $date 232 ); 233 } 234 235 return $date; 236 } 237 238 /** 239 * Convert a date format string to a strftime format string 240 * 241 * @param string $date 242 * @return string 243 */ 244 public static function toStrftime($date) 245 { 246 /* date to strftime conversion */ 247 { 248 // create negative lookbehind regex to match all unescaped known chars 249 $from = array_keys(self::$date); 250 $from = array_map( 251 function ($in) { 252 return '/(?<!\\\\)' . $in . '/'; 253 }, 254 $from 255 ); 256 $to = array_values(self::$date); 257 258 // percents need escaping: 259 array_unshift($from, '/%/'); 260 array_unshift($to, '%%'); 261 262 // replace all the placeholders 263 $strftime = preg_replace($from, $to, $date); 264 } 265 266 /* unescape date escapes */ 267 { 268 $datekeys = array_keys(self::$date); 269 $from = array_map( 270 function ($in) { 271 return '/\\\\' . $in . '/'; 272 }, 273 $datekeys 274 ); 275 $strftime = preg_replace($from, $datekeys, $strftime); 276 } 277 278 return $strftime; 279 } 280} 281