Skip to content

Commit

Permalink
Merge pull request #351 from a-thousand-channels/ut/improve-fuzzy-dat…
Browse files Browse the repository at this point in the history
…es-display

Fuzzy dates (with qualifier): Dates now with four digit years
  • Loading branch information
ut authored Nov 22, 2024
2 parents eedafda + 75d06a0 commit 2e45679
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 35 deletions.
46 changes: 29 additions & 17 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,32 @@ def smart_date_display(startdate, enddate)
if startdate.strftime('%d.%m') == '01.01'
startdate.strftime('%Y').to_s
else
startdate.strftime('%d.%m.%y').to_s
startdate.strftime('%d.%m.%Y').to_s
end
else
startdate.strftime('%d.%m.%y, %H:%M').to_s
startdate.strftime('%d.%m.%Y, %H:%M').to_s
end
elsif startdate > enddate
startdate.strftime('%d.%m.%y, %H:%M').to_s
startdate.strftime('%d.%m.%Y, %H:%M').to_s
elsif startdate == enddate
if startdate.strftime('%H:%M') == '00:00'
startdate.strftime('%d.%m.%y').to_s
startdate.strftime('%d.%m.%Y').to_s
else
startdate.strftime('%d.%m.%y, %H:%M').to_s
startdate.strftime('%d.%m.%Y, %H:%M').to_s
end
elsif startdate.to_date == enddate.to_date
"#{startdate.strftime('%d.%m.%y, %H:%M')}#{enddate.strftime('%H:%M')}"
"#{startdate.strftime('%d.%m.%Y, %H:%M')}#{enddate.strftime('%H:%M')}"
else
if startdate.strftime('%H:%M') == '00:00'
if startdate.strftime('%d.%m') == '01.01' && enddate.strftime('%d.%m') == '01.01'
"#{startdate.strftime('%Y')}#{enddate.strftime('%Y')}"
elsif (startdate.strftime('%d.%m') == '01.01' && enddate.strftime('%d.%m') == '31.12') && (startdate.strftime('%Y') == enddate.strftime('%Y'))
startdate.strftime('%Y').to_s
else
"#{startdate.strftime('%d.%m.%y')}#{enddate.strftime('%d.%m.%y')}"
"#{startdate.strftime('%d.%m.%Y')}#{enddate.strftime('%d.%m.%Y')}"
end
else
"#{startdate.strftime('%d.%m.%y, %H:%M')}#{enddate.strftime('%d.%m.%y, %H:%M')}"
"#{startdate.strftime('%d.%m.%Y, %H:%M')}#{enddate.strftime('%d.%m.%Y, %H:%M')}"
end
end
end
Expand All @@ -64,22 +64,22 @@ def smart_date_display_with_qualifier(startdate, enddate, startdate_qualifier =
if startdate.strftime('%d.%m') == '01.01'
startdate.strftime('%Y').to_s
else
startdate.strftime('%-d.%-m.%y').to_s
startdate.strftime('%-d.%-m.%Y').to_s
end
else
startdate.strftime('%-d.%-m.%y, %H:%M').to_s
startdate.strftime('%-d.%-m.%Y, %H:%M').to_s
end
end
elsif startdate > enddate
startdate.strftime('%-d.%-m.%y, %H:%M').to_s
startdate.strftime('%-d.%-m.%Y, %H:%M').to_s
elsif startdate == enddate
if startdate.strftime('%H:%M') == '00:00'
startdate.strftime('%-d.%-m.%y').to_s
startdate.strftime('%-d.%-m.%Y').to_s
else
startdate.strftime('%-d.%-m.%y, %H:%M').to_s
startdate.strftime('%-d.%-m.%Y, %H:%M').to_s
end
elsif startdate.to_date == enddate.to_date
"#{startdate.strftime('%-d.%-m.%y, %H:%M')}#{enddate.strftime('%H:%M')}"
"#{startdate.strftime('%-d.%-m.%Y, %H:%M')}#{enddate.strftime('%H:%M')}"
else
if startdate_qualifier == 'circa'
if enddate_qualifier == 'circa'
Expand All @@ -95,8 +95,19 @@ def smart_date_display_with_qualifier(startdate, enddate, startdate_qualifier =
# ca. 1980s
"ca. #{startdate.strftime('%Y')}s ‒ #{enddate.strftime('%Y')}s"
end
elsif startdate.strftime('%Y').to_i == enddate.strftime('%Y')
if startdate.strftime('%d.%m') == '01.01' && enddate.strftime('%d.%m') == '31.12'
# ca. 1981
"ca. #{startdate.strftime('%Y')}"
elsif startdate.strftime('%d.%m') == '01.01' && enddate.strftime('%d.%m') == '01.01'
# ca. 1981
"ca. #{startdate.strftime('%Y')}"
else
# ca. 15.11. ‒ 30.11.2020
"ca. #{startdate.strftime('%-d.%-m')}#{enddate.strftime('%-d.%-m.%Y')}"
end
else
# ca. 19802020
# ca. 19812025
"ca. #{startdate.strftime('%Y')}#{enddate.strftime('%Y')}"
end
elsif enddate_qualifier == 'exact'
Expand Down Expand Up @@ -136,12 +147,13 @@ def smart_date_display_with_qualifier(startdate, enddate, startdate_qualifier =
if startdate.strftime('%d.%m') == '01.01' && enddate.strftime('%d.%m') == '01.01'
"#{startdate.strftime('%Y')}#{enddate.strftime('%Y')}"
elsif (startdate.strftime('%d.%m') == '01.01' && enddate.strftime('%d.%m') == '31.12') && (startdate.strftime('%Y') == enddate.strftime('%Y'))
# 01.01.2020 ‒ 31.12.2020
startdate.strftime('%Y').to_s
else
"#{startdate.strftime('%-d.%-m.%y')}#{enddate.strftime('%-d.%-m.%y')}"
"#{startdate.strftime('%-d.%-m.%Y')}#{enddate.strftime('%-d.%-m.%Y')}"
end
else
"#{startdate.strftime('%-d.%-m.%y, %H:%M')}#{enddate.strftime('%-d.%-m.%y, %H:%M')}"
"#{startdate.strftime('%-d.%-m.%Y, %H:%M')}#{enddate.strftime('%-d.%-m.%Y, %H:%M')}"
end
end
end
Expand Down
28 changes: 14 additions & 14 deletions spec/helpers/application_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
it 'returns %d.%m.%Y, %H:%M (if enddate is exact the same)' do
startdate = '2015-03-16 15:13:04'.to_time
enddate = '2015-03-16 15:13:04'.to_time
expect(helper.smart_date_display(startdate, enddate)).to eq('16.03.15, 15:13')
expect(helper.smart_date_display(startdate, enddate)).to eq('16.03.2015, 15:13')
end
it 'returns %d.%m.%Y, %H:%M (if enddate is older)' do
startdate = '2015-03-16 15:13:04'.to_time
enddate = '2015-03-16 14:13:04'.to_time
expect(helper.smart_date_display(startdate, enddate)).to eq('16.03.15, 15:13')
expect(helper.smart_date_display(startdate, enddate)).to eq('16.03.2015, 15:13')
end
it 'returns %Y' do
startdate = '2015-01-01 00:00:00'.to_time
Expand All @@ -42,27 +42,27 @@
it 'returns %d.%m.%Y' do
startdate = '2015-03-16 00:00:00'.to_time
enddate = nil
expect(helper.smart_date_display(startdate, enddate)).to eq('16.03.15')
expect(helper.smart_date_display(startdate, enddate)).to eq('16.03.2015')
end
it 'returns %d.%m.%Y, %H:%M' do
startdate = '2015-03-16 20:00:00'.to_time
enddate = nil
expect(helper.smart_date_display(startdate, enddate)).to eq('16.03.15, 20:00')
expect(helper.smart_date_display(startdate, enddate)).to eq('16.03.2015, 20:00')
end
it 'returns %d.%m.%Y, %H:%M ‒ %H:%M' do
startdate = '2015-03-16 15:13:04'.to_time
enddate = '2015-03-16 20:13:04'.to_time
expect(helper.smart_date_display(startdate, enddate)).to eq('16.03.15, 15:13 ‒ 20:13')
expect(helper.smart_date_display(startdate, enddate)).to eq('16.03.2015, 15:13 ‒ 20:13')
end
it 'returns %d.%m.%Y, %H:%M ‒ %d.%m.%Y, %H:%M ' do
startdate = '2015-03-16 15:13:04'.to_time
enddate = '2015-03-17 15:13:04'.to_time
expect(helper.smart_date_display(startdate, enddate)).to eq('16.03.15, 15:13 ‒ 17.03.15, 15:13')
expect(helper.smart_date_display(startdate, enddate)).to eq('16.03.2015, 15:13 ‒ 17.03.2015, 15:13')
end
it 'returns %d.%m.%Y ‒ %d.%m.%Y ' do
startdate = '2015-03-16 00:00:00'.to_time
enddate = '2015-03-17 00:00:00'.to_time
expect(helper.smart_date_display(startdate, enddate)).to eq('16.03.15 ‒ 17.03.15')
expect(helper.smart_date_display(startdate, enddate)).to eq('16.03.2015 ‒ 17.03.2015')
end
it 'returns %Y ‒ %Y ' do
startdate = '2015-01-01 00:00:00'.to_time
Expand All @@ -80,12 +80,12 @@
it 'returns %d.%m.%Y, %H:%M (if enddate is exact the same)' do
startdate = '2015-03-16 15:13:04'.to_time
enddate = '2015-03-16 15:13:04'.to_time
expect(helper.smart_date_display_with_qualifier(startdate, enddate)).to eq('16.3.15, 15:13')
expect(helper.smart_date_display_with_qualifier(startdate, enddate)).to eq('16.3.2015, 15:13')
end
it 'returns %d.%m.%Y, %H:%M (if enddate is older)' do
startdate = '2015-03-16 15:13:04'.to_time
enddate = '2015-03-16 14:13:04'.to_time
expect(helper.smart_date_display_with_qualifier(startdate, enddate)).to eq('16.3.15, 15:13')
expect(helper.smart_date_display_with_qualifier(startdate, enddate)).to eq('16.3.2015, 15:13')
end
it 'returns %Y with qualifier' do
startdate = '2015-01-01 00:00:00'.to_time
Expand All @@ -95,27 +95,27 @@
it 'returns %d.%m.%Y' do
startdate = '2015-03-16 00:00:00'.to_time
enddate = nil
expect(helper.smart_date_display_with_qualifier(startdate, enddate)).to eq('16.3.15')
expect(helper.smart_date_display_with_qualifier(startdate, enddate)).to eq('16.3.2015')
end
it 'returns %d.%m.%Y, %H:%M' do
startdate = '2015-03-16 20:00:00'.to_time
enddate = nil
expect(helper.smart_date_display_with_qualifier(startdate, enddate)).to eq('16.3.15, 20:00')
expect(helper.smart_date_display_with_qualifier(startdate, enddate)).to eq('16.3.2015, 20:00')
end
it 'returns %d.%m.%Y, %H:%M ‒ %H:%M' do
startdate = '2015-03-16 15:13:04'.to_time
enddate = '2015-03-16 20:13:04'.to_time
expect(helper.smart_date_display_with_qualifier(startdate, enddate)).to eq('16.3.15, 15:13 ‒ 20:13')
expect(helper.smart_date_display_with_qualifier(startdate, enddate)).to eq('16.3.2015, 15:13 ‒ 20:13')
end
it 'returns %d.%m.%Y, %H:%M ‒ %d.%m.%Y, %H:%M ' do
startdate = '2015-03-16 15:13:04'.to_time
enddate = '2015-03-17 15:13:04'.to_time
expect(helper.smart_date_display_with_qualifier(startdate, enddate)).to eq('16.3.15, 15:13 ‒ 17.3.15, 15:13')
expect(helper.smart_date_display_with_qualifier(startdate, enddate)).to eq('16.3.2015, 15:13 ‒ 17.3.2015, 15:13')
end
it 'returns %d.%m.%Y ‒ %d.%m.%Y ' do
startdate = '2015-03-16 00:00:00'.to_time
enddate = '2015-03-17 00:00:00'.to_time
expect(helper.smart_date_display_with_qualifier(startdate, enddate)).to eq('16.3.15 ‒ 17.3.15')
expect(helper.smart_date_display_with_qualifier(startdate, enddate)).to eq('16.3.2015 ‒ 17.3.2015')
end
it 'returns ca. %Y ‒ %Y with qualifier' do
startdate = '2015-01-01 00:00:00'.to_time
Expand Down
8 changes: 4 additions & 4 deletions spec/models/place_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@
describe 'Dates for UI' do
it 'returns date range' do
p = FactoryBot.create(:place, startdate_date: '2018-01-01', enddate_date: '2018-01-02')
expect(p.date).to eq('01.01.18 ‒ 02.01.18')
expect(p.date).to eq('01.01.2018 ‒ 02.01.2018')
end

it 'returns full startdate' do
p = FactoryBot.create(:place, startdate_date: '2018-01-01', startdate_time: '20:30', enddate: '')
expect(p.date).to eq('01.01.18, 20:30')
expect(p.date).to eq('01.01.2018, 20:30')
end

it 'returns year (with only startdate given)' do
Expand All @@ -78,12 +78,12 @@
end
it 'returns full date range with time' do
p = FactoryBot.create(:place, startdate_date: '2018-01-01', startdate_time: '12:00', enddate_date: '2018-01-01', enddate_time: '18:00')
expect(p.date).to eq('01.01.18, 12:00 ‒ 18:00')
expect(p.date).to eq('01.01.2018, 12:00 ‒ 18:00')
end

it 'returns full date range without time' do
p = FactoryBot.create(:place, startdate_date: '2018-01-01', startdate_time: '', enddate_date: '2018-10-15')
expect(p.date).to eq('01.01.18 ‒ 15.10.18')
expect(p.date).to eq('01.01.2018 ‒ 15.10.2018')
end

it 'returns year range' do
Expand Down

0 comments on commit 2e45679

Please sign in to comment.