2024-08-26 12:37:54 +02:00
|
|
|
package utils
|
|
|
|
|
2024-11-21 09:21:16 +01:00
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
2024-08-26 12:37:54 +02:00
|
|
|
|
2024-08-27 09:15:11 +02:00
|
|
|
// 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.
|
2024-08-26 12:37:54 +02:00
|
|
|
func CalculateAge(birthdate, today time.Time) int {
|
|
|
|
today = today.In(birthdate.Location())
|
|
|
|
ty, tm, td := today.Date()
|
|
|
|
today = time.Date(ty, tm, td, 0, 0, 0, 0, time.UTC)
|
|
|
|
by, bm, bd := birthdate.Date()
|
|
|
|
birthdate = time.Date(by, bm, bd, 0, 0, 0, 0, time.UTC)
|
|
|
|
if today.Before(birthdate) {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
age := ty - by
|
|
|
|
anniversary := birthdate.AddDate(age, 0, 0)
|
|
|
|
if anniversary.After(today) {
|
|
|
|
age--
|
|
|
|
}
|
|
|
|
return age
|
2024-08-27 13:59:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CalculateAgeWithYOB calculates the age based on the given year of birth (YOB).
|
|
|
|
// It subtracts the YOB from the current year to determine the age.
|
|
|
|
//
|
|
|
|
// Parameters:
|
2024-11-21 09:21:16 +01:00
|
|
|
//
|
|
|
|
// yob: The year of birth as an integer.
|
2024-08-27 13:59:17 +02:00
|
|
|
//
|
|
|
|
// Returns:
|
2024-11-21 09:21:16 +01:00
|
|
|
//
|
|
|
|
// The calculated age as an integer.
|
2024-08-27 13:59:17 +02:00
|
|
|
func CalculateAgeWithYOB(yob int) int {
|
2024-11-21 09:21:16 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|