From 0d00441ec65f9e6619dd7c434828184e268440bb Mon Sep 17 00:00:00 2001 From: Sunny Ripert Date: Tue, 24 Mar 2026 10:08:53 +0100 Subject: [PATCH] =?UTF-8?q?Fix=20Sweden=20TINs=20versions=20with=204-digit?= =?UTF-8?q?=20years=20=F0=9F=87=B8=F0=9F=87=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + lib/tin_valid/sweden_tin.rb | 8 ++++++-- spec/tin_valid/sweden_tin_spec.rb | 2 ++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 708b9c9..89d073f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ Fixes: - Fix Belgium to ignore `-` and `.` πŸ‡§πŸ‡ͺ - Fix Czechia to accept `CZ` prefix πŸ‡¨πŸ‡Ώ +- Fix Sweden TINs versions with 4-digit years πŸ‡ΈπŸ‡ͺ ## [1.3.0] - 2025-12-12 diff --git a/lib/tin_valid/sweden_tin.rb b/lib/tin_valid/sweden_tin.rb index ce77c9d..c8b951d 100644 --- a/lib/tin_valid/sweden_tin.rb +++ b/lib/tin_valid/sweden_tin.rb @@ -21,8 +21,7 @@ def valid? day = match[:day] next false unless date_valid?(year, month, day) - id = match[:id] - match[:check].to_i == checksum("#{year[..2]}#{month}#{day}#{id}") + check_match?(year:, month:, day:, id: match[:id], check: match[:check]) end end @@ -70,6 +69,11 @@ def valid? ].freeze private_constant :VERSIONS + def check_match?(year:, month:, day:, id:, check:) + checkyear = year.size == 4 ? year[2..] : year + check.to_i == checksum("#{checkyear}#{month}#{day}#{id}") + end + def checksum(numbers) # 1. Multiply the values of each position by the corresponding weight: # - C1: 2 diff --git a/spec/tin_valid/sweden_tin_spec.rb b/spec/tin_valid/sweden_tin_spec.rb index 91512ee..348f385 100644 --- a/spec/tin_valid/sweden_tin_spec.rb +++ b/spec/tin_valid/sweden_tin_spec.rb @@ -4,6 +4,8 @@ describe "#valid?" do valid_values = [ ["640883-3231", nil], + ["199109141086", nil], + ["040731-1372", nil], ["640823-3234", nil], ["640823-3234", Date.new(1964, 8, 23)], ]