1 | <?php |
||
2 | /* |
||
3 | * This file is part of dispositif/wikibot application (@github) |
||
4 | * 2019/2023 © Philippe/Irønie <[email protected]> |
||
5 | * For the full copyright and MIT license information, view the license file. |
||
6 | */ |
||
7 | |||
8 | declare(strict_types=1); |
||
9 | |||
10 | namespace App\Domain\Utils; |
||
11 | |||
12 | use DateTime; |
||
13 | use DateTimeZone; |
||
14 | |||
15 | /** |
||
16 | * see TYPO https://fr.wikipedia.org/wiki/Wikip%C3%A9dia:AutoWikiBrowser/Typos#Dates |
||
17 | * Class DateUtil. |
||
18 | */ |
||
19 | class DateUtil |
||
20 | { |
||
21 | /** |
||
22 | * "09 mai 2019" => DateTime |
||
23 | */ |
||
24 | public static function simpleFrench2object(string $string): ?DateTime |
||
25 | { |
||
26 | $string = self::french2English(trim($string)); |
||
27 | $dateTime = DateTime::createFromFormat('d F Y', $string); |
||
28 | |||
29 | return $dateTime ?: null; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
30 | } |
||
31 | |||
32 | /** |
||
33 | * "01 janvier 2020 à 17:44 (CET)" => DateTime. |
||
34 | */ |
||
35 | public static function fromWikiSignature(string $string): ?DateTime |
||
36 | { |
||
37 | // 1 janvier 2020 à 17:44 (CET) => 1 January 2020 à 17:44 (CET) |
||
38 | $string = self::french2English(trim($string)); |
||
39 | |||
40 | $timezone = new DateTimeZone('Europe/Paris'); // CET, CEST |
||
41 | if (preg_match('/\(UTC\)/', (string) $string)) { |
||
42 | $timezone = new DateTimeZone('UTC'); |
||
43 | } |
||
44 | // strip "(CET)", "(CEST)", "(UTC)"... |
||
45 | $string = preg_replace('/\([A-Z]{3,4}\)/', '', (string) $string); |
||
46 | // convert fuseau ? https://stackoverflow.com/questions/5746531/utc-date-time-string-to-timezone |
||
47 | |||
48 | return DateTime::createFromFormat('d F Y \à H\:i', trim($string), $timezone) ?: null; |
||
49 | } |
||
50 | |||
51 | public static function english2french(string $dateStr): string |
||
52 | { |
||
53 | return str_replace( |
||
54 | [ |
||
55 | 'January', |
||
56 | 'February', |
||
57 | 'March', |
||
58 | 'April', |
||
59 | 'May', |
||
60 | 'June', |
||
61 | 'July', |
||
62 | 'August', |
||
63 | 'September', |
||
64 | 'October', |
||
65 | 'November', |
||
66 | 'December', |
||
67 | ], |
||
68 | [ |
||
69 | 'janvier', |
||
70 | 'février', |
||
71 | 'mars', |
||
72 | 'avril', |
||
73 | 'mai', |
||
74 | 'juin', |
||
75 | 'juillet', |
||
76 | 'août', |
||
77 | 'septembre', |
||
78 | 'octobre', |
||
79 | 'novembre', |
||
80 | 'décembre', |
||
81 | ], |
||
82 | $dateStr |
||
83 | ); |
||
84 | } |
||
85 | |||
86 | public static function french2English(string $date): string |
||
87 | { |
||
88 | return str_replace( |
||
89 | [ |
||
90 | 'janvier', |
||
91 | 'février', |
||
92 | 'mars', |
||
93 | 'avril', |
||
94 | 'mai', |
||
95 | 'juin', |
||
96 | 'juillet', |
||
97 | 'août', |
||
98 | 'septembre', |
||
99 | 'octobre', |
||
100 | 'novembre', |
||
101 | 'décembre', |
||
102 | ], |
||
103 | [ |
||
104 | 'January', |
||
105 | 'February', |
||
106 | 'March', |
||
107 | 'April', |
||
108 | 'May', |
||
109 | 'June', |
||
110 | 'July', |
||
111 | 'August', |
||
112 | 'September', |
||
113 | 'October', |
||
114 | 'November', |
||
115 | 'December', |
||
116 | ], |
||
117 | $date |
||
118 | ); |
||
119 | } |
||
120 | } |
||
121 |