From 9b89462797be98f0477f1d7ea316a092cdd4791b Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Thu, 21 Nov 2024 11:21:16 +0300 Subject: [PATCH] add function to check validity of provided yob --- internal/utils/age.go | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/internal/utils/age.go b/internal/utils/age.go index 6b040e7..59056a8 100644 --- a/internal/utils/age.go +++ b/internal/utils/age.go @@ -1,6 +1,9 @@ package utils -import "time" +import ( + "strconv" + "time" +) // CalculateAge calculates the age based on a given birthdate and the current date in the format dd/mm/yy // It adjusts for cases where the current date is before the birthday in the current year. @@ -25,11 +28,29 @@ func CalculateAge(birthdate, today time.Time) int { // It subtracts the YOB from the current year to determine the age. // // Parameters: -// yob: The year of birth as an integer. +// +// yob: The year of birth as an integer. // // Returns: -// The calculated age as an integer. +// +// The calculated age as an integer. func CalculateAgeWithYOB(yob int) int { - currentYear := time.Now().Year() - return currentYear - yob -} \ No newline at end of file + currentYear := time.Now().Year() + return currentYear - yob +} + + +//IsValidYob checks if the provided yob can be considered valid +func IsValidYOb(yob string) bool { + currentYear := time.Now().Year() + yearOfBirth, err := strconv.ParseInt(yob, 10, 64) + if err != nil { + return false + } + if yearOfBirth >= 1900 && int(yearOfBirth) <= currentYear { + return true + } else { + return false + } + +}