Skip to content

Commit 5caa944

Browse files
anna-devKagemaru
authored andcommitted
Fix date parse for short_year format (63095)
1 parent a02eaa2 commit 5caa944

File tree

2 files changed

+33
-49
lines changed

2 files changed

+33
-49
lines changed

app/models/util/period.rb

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -130,37 +130,22 @@ def with(start_date = Time.zone.today, end_date = Time.zone.today, label = nil)
130130
def parse_date(date)
131131
return nil if date.blank?
132132

133-
if date.is_a?(String)
134-
if date.match?(/\A\d{1,2}\.\d{1,2}\.\d{2}\z/)
135-
day, month, short_year = date.split('.').map(&:to_i)
136-
137-
# Get current full year and its century
138-
current_year = Time.zone.today.year
139-
cutoff = (current_year + 5) % 100
140-
current_century = (current_year / 100) * 100
141-
142-
full_year = if short_year <= cutoff
143-
current_century + short_year
144-
else
145-
(current_century - 100) + short_year
146-
end
147-
148-
return Date.new(full_year, month, day)
149-
end
150-
151-
begin
152-
date = Date.strptime(date, I18n.t('date.formats.default'))
153-
rescue ArgumentError
154-
date = Date.parse(date)
155-
end
133+
case date
134+
when Time then date.to_date
135+
when /\A\d{1,2}\.\d{1,2}\.\d{2}\z/ then parse_date_string(date, :short_year)
136+
when String then parse_date_string(date)
137+
else date
156138
end
157-
158-
date = date.to_date if date.is_a?(Time)
159-
date
160139
end
161140

162141
private
163142

143+
def parse_date_string(date, type = :default)
144+
Date.strptime(date, I18n.t(type, scope: 'date.formats'))
145+
rescue ArgumentError
146+
Date.parse(date)
147+
end
148+
164149
def day_label(date)
165150
case date
166151
when Time.zone.today then 'Heute'

config/locales/crud.de-CH.yml

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33
# or later. See the COPYING file at the top-level directory or at
44
# https://github.com/puzzle/puzzletime.
55

6-
76
# Translations of all crud strings.
87
# See also I18nHelper#translate_inheritable and #translate_association.
98
---
109
de-CH:
1110
# global scope
1211
global:
13-
"yes": "ja"
14-
"no": "nein"
15-
"true": "ja"
16-
"false": "nein"
12+
'yes': 'ja'
13+
'no': 'nein'
14+
'true': 'ja'
15+
'false': 'nein'
1716
no_list_entries: Keine Einträge gefunden.
1817
confirm_delete: Wollen Sie diesen Eintrag wirklich löschen?
1918

@@ -40,43 +39,43 @@ de-CH:
4039

4140
errors:
4241
header:
43-
one: "Ein Fehler verhinderte das Speichern dieses Eintrages:"
44-
other: "%{count} Fehler verhinderten das Speichern dieses Eintrages:"
42+
one: 'Ein Fehler verhinderte das Speichern dieses Eintrages:'
43+
other: '%{count} Fehler verhinderten das Speichern dieses Eintrages:'
4544

4645
# formats
4746
date:
4847
formats:
49-
long: "%a, %d.%m.%Y"
50-
week: "%Y %V"
51-
month: "%B %Y"
52-
month_only: "%B"
48+
long: '%a, %d.%m.%Y'
49+
week: '%Y %V'
50+
month: '%B %Y'
51+
month_only: '%B'
52+
short_year: '%d.%m.%y'
5353
time:
5454
formats:
55-
time: "%H:%M"
55+
time: '%H:%M'
5656

5757
# list controller
5858
list:
5959
index:
60-
title: "%{models}"
60+
title: '%{models}'
6161

6262
# crud controller
6363
crud:
6464
show:
65-
title: "%{model}"
65+
title: '%{model}'
6666
new:
67-
title: "%{model} erstellen"
67+
title: '%{model} erstellen'
6868
edit:
69-
title: "%{model} bearbeiten"
69+
title: '%{model} bearbeiten'
7070
create:
71-
title: "%{model} erstellen"
71+
title: '%{model} erstellen'
7272
flash:
73-
success: "%{model} wurde erfolgreich erstellt."
73+
success: '%{model} wurde erfolgreich erstellt.'
7474
update:
75-
title: "%{model} bearbeiten"
75+
title: '%{model} bearbeiten'
7676
flash:
77-
success: "%{model} wurde erfolgreich aktualisiert."
77+
success: '%{model} wurde erfolgreich aktualisiert.'
7878
destroy:
7979
flash:
80-
success: "%{model} wurde erfolgreich gelöscht."
81-
failure: "%{model} konnte nicht gelöscht werden."
82-
...
80+
success: '%{model} wurde erfolgreich gelöscht.'
81+
failure: '%{model} konnte nicht gelöscht werden.'

0 commit comments

Comments
 (0)