Files
sure/lib/simplefin/date_utils.rb
LPW 101b9dac95 Refactor transaction merging logic to prefer accurate posted dates and improve deduplication (#449)
- Updated `date_utils.rb` to return `nil` for timestamps equal to 0.
- Enhanced `SimplefinItem::Importer` to merge transactions by prioritizing non-pending records with valid posted timestamps.
- Introduced a comparator and key-builder for resolving transaction conflicts systematically.

Co-authored-by: Josh Waldrep <joshua.waldrep5+github@gmail.com>
2025-12-13 09:13:02 +01:00

30 lines
632 B
Ruby

# frozen_string_literal: true
module Simplefin
module DateUtils
module_function
# Parses provider-supplied dates that may be String (ISO), Numeric (epoch seconds),
# Time/DateTime, or Date. Returns a Date or nil when unparseable.
def parse_provider_date(val)
return nil if val.nil?
case val
when Date
val
when Time, DateTime
val.to_date
when Integer, Float
return nil if val.to_i == 0
Time.at(val).utc.to_date
when String
Date.parse(val)
else
nil
end
rescue ArgumentError, TypeError
nil
end
end
end