Skip to content

Commit a4feb74

Browse files
committed
Added DateStrCheckAge, DateStrCheckRange and DateStrCheckErrMessage
Signed-off-by: miguel <[email protected]>
1 parent 0503cfb commit a4feb74

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

handydatetime-translations.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package handy
2+
3+
func DateStrCheckErrMessage(idiom string, errCode DateStrCheck) string {
4+
if idiom == "bra" {
5+
switch errCode {
6+
case DateStrCheckErrInvalid:
7+
return "data ou formato inválido"
8+
case DateStrCheckErrOutOfRange:
9+
return "data fora do intervalo permitido"
10+
case DateStrCheckErrEmpty:
11+
return "data não definida"
12+
default:
13+
return ""
14+
}
15+
}
16+
17+
switch errCode {
18+
case DateStrCheckErrInvalid:
19+
return "date or format invalid"
20+
case DateStrCheckErrOutOfRange:
21+
return "date out of range"
22+
case DateStrCheckErrEmpty:
23+
return "date undefined"
24+
default:
25+
return ""
26+
}
27+
}

handydatetime.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,70 @@ func DateReformat(dt1 string, currentFormat, newFormat string) string {
261261

262262
return ""
263263
}
264+
265+
type DateStrCheck uint8
266+
267+
const (
268+
DateStrCheckOk DateStrCheck = 0
269+
DateStrCheckErrInvalid DateStrCheck = 1
270+
DateStrCheckErrOutOfRange DateStrCheck = 2
271+
DateStrCheckErrEmpty DateStrCheck = 3
272+
)
273+
274+
// DateStrCheckAge checks a date string considering minimum and maximum age
275+
// The resulting code can be translated to text, according prefered idiom, with DateStrCheckErrMessage
276+
func DateStrCheckAge(date, format string, yearsAgeMin, yearsAgeMax int, acceptEmpty bool) DateStrCheck {
277+
if date == "" {
278+
if !acceptEmpty {
279+
return DateStrCheckErrEmpty
280+
}
281+
282+
return DateStrCheckOk
283+
}
284+
285+
dt := StringAsDateTime(date, format)
286+
287+
if dt.IsZero() {
288+
return DateStrCheckErrInvalid
289+
}
290+
291+
yearsAge := YearsAge(dt)
292+
293+
if !Between(yearsAge, yearsAgeMin, yearsAgeMax) {
294+
return DateStrCheckErrOutOfRange
295+
}
296+
297+
return DateStrCheckOk
298+
}
299+
300+
// DateStrCheckRange checks a date string considering minimum and maximum date range
301+
// The resulting code can be translated to text, according prefered idiom, with DateStrCheckErrMessage
302+
func DateStrCheckRange(date, format string, dateMin, dateMax time.Time, acceptEmpty bool) DateStrCheck {
303+
if date == "" {
304+
if !acceptEmpty {
305+
return DateStrCheckErrEmpty
306+
}
307+
308+
return DateStrCheckOk
309+
}
310+
311+
dt := StringAsDateTime(date, format)
312+
313+
if dt.IsZero() {
314+
return DateStrCheckErrInvalid
315+
}
316+
317+
if !dateMin.IsZero() {
318+
if dt.Before(dateMin) {
319+
return DateStrCheckErrOutOfRange
320+
}
321+
}
322+
323+
if !dateMax.IsZero() {
324+
if dt.After(dateMax) {
325+
return DateStrCheckErrOutOfRange
326+
}
327+
}
328+
329+
return DateStrCheckOk
330+
}

0 commit comments

Comments
 (0)