Skip to content

add support for two-digit year in yeear selection (63095) #258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions app/models/util/period.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,32 @@ def with(start_date = Time.zone.today, end_date = Time.zone.today, label = nil)
def parse_date(date)
return nil if date.blank?

if date.is_a? String
if date.is_a?(String)
if date.match?(/\A\d{1,2}\.\d{1,2}\.\d{2}\z/)
day, month, short_year = date.split('.').map(&:to_i)

# Get current full year and its century
current_year = Time.zone.today.year
cutoff = (current_year + 5) % 100
current_century = (current_year / 100) * 100

full_year = if short_year <= cutoff
current_century + short_year
else
(current_century - 100) + short_year
end

return Date.new(full_year, month, day)
end

begin
date = Date.strptime(date, I18n.t('date.formats.default'))
rescue StandardError
rescue ArgumentError
date = Date.parse(date)
end
end
date = date.to_date if date.is_a? Time

date = date.to_date if date.is_a?(Time)
date
end

Expand Down