From 31edce0276027f5a411323823d5c451fc47b7b64 Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Tue, 9 Dec 2025 00:03:39 +0100 Subject: [PATCH] Limit timestamps to valid range to prevent database errors when users enter pre-epoch dates. --- CHANGELOG.md | 2 +- lib/timestamps.rb | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 722447aa..a7fdb3e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Cities visited during a trip are now being calculated correctly. #547 #641 - Points on the map are now show time in user's timezone. #580 -- Date range inputs now handle pre-epoch dates gracefully by clamping to valid PostgreSQL integer range (1970-2038), preventing database errors when users enter dates like year 1000. +- Date range inputs now handle pre-epoch dates gracefully by clamping to valid PostgreSQL integer range. #685 # [0.36.2] - 2025-12-06 diff --git a/lib/timestamps.rb b/lib/timestamps.rb index 1912de46..59273d59 100644 --- a/lib/timestamps.rb +++ b/lib/timestamps.rb @@ -1,13 +1,13 @@ # frozen_string_literal: true module Timestamps - MIN_TIMESTAMP = Time.zone.parse('1970-01-01').to_i - MAX_TIMESTAMP = Time.zone.parse('2100-01-01').to_i - def self.parse_timestamp(timestamp) + min_timestamp = Time.zone.parse('1970-01-01').to_i + max_timestamp = Time.zone.parse('2100-01-01').to_i + parsed = DateTime.parse(timestamp).to_time.to_i - parsed.clamp(MIN_TIMESTAMP, MAX_TIMESTAMP) + parsed.clamp(min_timestamp, max_timestamp) rescue StandardError result = if timestamp.to_s.length > 10 @@ -16,6 +16,6 @@ module Timestamps timestamp.to_i end - result.clamp(MIN_TIMESTAMP, MAX_TIMESTAMP) + result.clamp(min_timestamp, max_timestamp) end end