From ee35cd0ac3ba4d8a75dbca8734309be9e24d6853 Mon Sep 17 00:00:00 2001 From: gbanyan Date: Sun, 25 Jan 2026 06:41:42 +0800 Subject: [PATCH] Fix ROC date parsing for roster import --- app/Console/Commands/ImportMembersCommand.php | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/app/Console/Commands/ImportMembersCommand.php b/app/Console/Commands/ImportMembersCommand.php index 051386d..159c4a8 100644 --- a/app/Console/Commands/ImportMembersCommand.php +++ b/app/Console/Commands/ImportMembersCommand.php @@ -348,7 +348,30 @@ class ImportMembersCommand extends Command if (is_numeric($value)) { $numeric = (float) $value; - if ($numeric > 10000) { + $digits = preg_replace('/\D/', '', (string) $numeric); + + if (strlen($digits) === 6 || strlen($digits) === 7) { + $yearLength = strlen($digits) - 4; + $rocYear = (int) substr($digits, 0, $yearLength); + $month = (int) substr($digits, $yearLength, 2); + $day = (int) substr($digits, $yearLength + 2, 2); + $year = $rocYear + 1911; + + if (checkdate($month, $day, $year)) { + return sprintf('%04d-%02d-%02d', $year, $month, $day); + } + } + + if (strlen($digits) === 8) { + $year = (int) substr($digits, 0, 4); + $month = (int) substr($digits, 4, 2); + $day = (int) substr($digits, 6, 2); + if (checkdate($month, $day, $year)) { + return sprintf('%04d-%02d-%02d', $year, $month, $day); + } + } + + if ($numeric > 0) { return ExcelDate::excelToDateTimeObject($numeric)->format('Y-m-d'); } } @@ -477,6 +500,10 @@ class ImportMembersCommand extends Command $update['birth_date'] = $birthDate; } + if ($birthDate && $member->birth_date && $this->isInvalidDate($member->birth_date)) { + $update['birth_date'] = $birthDate; + } + if ($gender && ! $member->gender) { $update['gender'] = $gender; } @@ -512,6 +539,20 @@ class ImportMembersCommand extends Command return $update; } + protected function isInvalidDate(mixed $value): bool + { + try { + $date = $value instanceof \DateTimeInterface ? $value : new \DateTime($value); + } catch (\Exception) { + return true; + } + + $year = (int) $date->format('Y'); + $currentYear = (int) date('Y'); + + return $year < 1900 || $year > ($currentYear + 1); + } + protected function toPinyin(string $name): string { // Simple romanization for email generation