Skip to content

Commit

Permalink
Added DateStrCheckAge, DateStrCheckRange and DateStrCheckErrMessage
Browse files Browse the repository at this point in the history
Signed-off-by: miguel <miguelpragier@gmail.com>
  • Loading branch information
miguelpragier committed May 21, 2020
1 parent 0503cfb commit a4feb74
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
27 changes: 27 additions & 0 deletions handydatetime-translations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package handy

func DateStrCheckErrMessage(idiom string, errCode DateStrCheck) string {
if idiom == "bra" {
switch errCode {
case DateStrCheckErrInvalid:
return "data ou formato inválido"
case DateStrCheckErrOutOfRange:
return "data fora do intervalo permitido"
case DateStrCheckErrEmpty:
return "data não definida"
default:
return ""
}
}

switch errCode {
case DateStrCheckErrInvalid:
return "date or format invalid"
case DateStrCheckErrOutOfRange:
return "date out of range"
case DateStrCheckErrEmpty:
return "date undefined"
default:
return ""
}
}
67 changes: 67 additions & 0 deletions handydatetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,70 @@ func DateReformat(dt1 string, currentFormat, newFormat string) string {

return ""
}

type DateStrCheck uint8

const (
DateStrCheckOk DateStrCheck = 0
DateStrCheckErrInvalid DateStrCheck = 1
DateStrCheckErrOutOfRange DateStrCheck = 2
DateStrCheckErrEmpty DateStrCheck = 3
)

// DateStrCheckAge checks a date string considering minimum and maximum age
// The resulting code can be translated to text, according prefered idiom, with DateStrCheckErrMessage
func DateStrCheckAge(date, format string, yearsAgeMin, yearsAgeMax int, acceptEmpty bool) DateStrCheck {
if date == "" {
if !acceptEmpty {
return DateStrCheckErrEmpty
}

return DateStrCheckOk
}

dt := StringAsDateTime(date, format)

if dt.IsZero() {
return DateStrCheckErrInvalid
}

yearsAge := YearsAge(dt)

if !Between(yearsAge, yearsAgeMin, yearsAgeMax) {
return DateStrCheckErrOutOfRange
}

return DateStrCheckOk
}

// DateStrCheckRange checks a date string considering minimum and maximum date range
// The resulting code can be translated to text, according prefered idiom, with DateStrCheckErrMessage
func DateStrCheckRange(date, format string, dateMin, dateMax time.Time, acceptEmpty bool) DateStrCheck {
if date == "" {
if !acceptEmpty {
return DateStrCheckErrEmpty
}

return DateStrCheckOk
}

dt := StringAsDateTime(date, format)

if dt.IsZero() {
return DateStrCheckErrInvalid
}

if !dateMin.IsZero() {
if dt.Before(dateMin) {
return DateStrCheckErrOutOfRange
}
}

if !dateMax.IsZero() {
if dt.After(dateMax) {
return DateStrCheckErrOutOfRange
}
}

return DateStrCheckOk
}

0 comments on commit a4feb74

Please sign in to comment.