Skip to content

Commit

Permalink
Support German month names.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pag-Man committed Mar 2, 2019
1 parent ef624e2 commit 7ddf00b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Date/De.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function handle()
{
$extractions = [];

// d.m.Y
preg_match_all('/\b[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{4}\b/', $this->text, $matches);

foreach ($matches[0] as $date) {
Expand All @@ -23,6 +24,36 @@ public function handle()
}
}

// d. F Y
preg_match_all('/\b[0-9]{1,2}\. [A-Z][a-z]+ [0-9]{4}\b/', $this->text, $matches);

$search = [];
$replace = [];

$originalLocale = setlocale(LC_TIME, 0);

setlocale(LC_TIME, 'de_DE');
for ($m = 1; $m <= 12; ++$m) {
$search[] = strftime('%B', mktime(0, 0, 0, $m, 1));
$search[] = strftime('%b', mktime(0, 0, 0, $m, 1));
}

setlocale(LC_TIME, 'en_US');
for ($m = 1; $m <= 12; ++$m) {
$replace[] = strftime('%B', mktime(0, 0, 0, $m, 1));
$replace[] = strftime('%b', mktime(0, 0, 0, $m, 1));
}

setlocale(LC_TIME, $originalLocale);

foreach ($matches[0] as $date) {
$date = str_replace($search, $replace, $date);

if (false !== strtotime($date)) {
$extractions[] = $date;
}
}

return $extractions;
}
}
1 change: 1 addition & 0 deletions src/Date/En.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function handle()
{
$extractions = [];

// F d, Y
preg_match_all('/\b[A-Z][a-z]+ [0-3]?[0-9], [0-9]{4}\b/', $this->text, $matches);

foreach ($matches[0] as $date) {
Expand Down

0 comments on commit 7ddf00b

Please sign in to comment.