-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathage_calculation.go
More file actions
263 lines (226 loc) · 6.5 KB
/
age_calculation.go
File metadata and controls
263 lines (226 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package gotime
import (
"fmt"
"time"
)
// Age calculates the precise age in years, months, and days between a birth date and a reference date.
// If no reference date is provided, it uses the current time.
//
// The calculation accounts for leap years and varying month lengths.
//
// Example:
//
// birthDate := time.Date(1990, 5, 15, 0, 0, 0, 0, time.UTC)
// asOf := time.Date(2025, 7, 7, 0, 0, 0, 0, time.UTC)
// years, months, days := gotime.Age(birthDate, asOf)
// // Returns: 35, 1, 23 (35 years, 1 month, 23 days)
func Age(birthDate time.Time, asOf ...time.Time) (years, months, days int) {
var ref time.Time
if len(asOf) > 0 {
ref = asOf[0]
} else {
ref = time.Now()
}
// Ensure birth date is before reference date
if birthDate.After(ref) {
return 0, 0, 0
}
// Use a more reliable approach: count years first, then months, then days
birth := time.Date(birthDate.Year(), birthDate.Month(), birthDate.Day(), 0, 0, 0, 0, birthDate.Location())
reference := time.Date(ref.Year(), ref.Month(), ref.Day(), 0, 0, 0, 0, ref.Location())
// Start with the birth year and increment until we can't add more years
currentDate := birth
for {
nextYear := currentDate.AddDate(1, 0, 0)
if nextYear.After(reference) {
break
}
years++
currentDate = nextYear
}
// Now add months
for {
nextMonth := currentDate.AddDate(0, 1, 0)
if nextMonth.After(reference) {
break
}
months++
currentDate = nextMonth
}
// Finally, count remaining days
for currentDate.Before(reference) {
days++
currentDate = currentDate.AddDate(0, 0, 1)
}
return years, months, days
}
// YearsBetween calculates the precise number of years between two dates as a float64.
// The result includes fractional years based on the exact time difference.
//
// Example:
//
// start := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
// end := time.Date(2025, 7, 1, 0, 0, 0, 0, time.UTC)
// years := gotime.YearsBetween(start, end)
// // Returns: approximately 5.5 years
func YearsBetween(start, end time.Time) float64 {
// Ensure start is before end
if start.After(end) {
start, end = end, start
}
duration := end.Sub(start)
// Average number of hours in a year (accounting for leap years)
// 365.2425 days per year * 24 hours per day
const hoursPerYear = 365.2425 * 24
return duration.Hours() / hoursPerYear
}
// MonthsBetween calculates the precise number of months between two dates as a float64.
// The result includes fractional months based on the exact time difference.
//
// Example:
//
// start := time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)
// end := time.Date(2025, 7, 15, 0, 0, 0, 0, time.UTC)
// months := gotime.MonthsBetween(start, end)
// // Returns: approximately 6.5 months
func MonthsBetween(start, end time.Time) float64 {
// Ensure start is before end
if start.After(end) {
start, end = end, start
}
duration := end.Sub(start)
// Average number of hours in a month
// 365.2425 days per year / 12 months * 24 hours per day
const hoursPerMonth = (365.2425 / 12) * 24
return duration.Hours() / hoursPerMonth
}
// DaysBetween calculates the number of days between two dates.
// This is a convenience function that returns the integer number of days.
//
// Example:
//
// start := time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)
// end := time.Date(2025, 1, 8, 0, 0, 0, 0, time.UTC)
// days := gotime.DaysBetween(start, end)
// // Returns: 7
func DaysBetween(start, end time.Time) int {
// Ensure start is before end
if start.After(end) {
start, end = end, start
}
duration := end.Sub(start)
return int(duration.Hours() / 24)
}
// WeeksBetween calculates the precise number of weeks between two dates as a float64.
// The result includes fractional weeks based on the exact time difference.
//
// Example:
//
// start := time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)
// end := time.Date(2025, 1, 11, 0, 0, 0, 0, time.UTC)
// weeks := gotime.WeeksBetween(start, end)
// // Returns: approximately 1.43 weeks
func WeeksBetween(start, end time.Time) float64 {
// Ensure start is before end
if start.After(end) {
start, end = end, start
}
duration := end.Sub(start)
const hoursPerWeek = 7 * 24
return duration.Hours() / hoursPerWeek
}
// DurationInWords returns a human-readable representation of a duration.
// It formats the duration in the most appropriate unit(s).
//
// Example:
//
// d := 2*time.Hour + 30*time.Minute
// result := gotime.DurationInWords(d)
// // Returns: "2 hours 30 minutes"
func DurationInWords(d time.Duration) string {
if d == 0 {
return "0 seconds"
}
// Handle negative durations
negative := d < 0
if negative {
d = -d
}
var parts []string
// Extract time components
days := int(d.Hours()) / 24
hours := int(d.Hours()) % 24
minutes := int(d.Minutes()) % 60
seconds := int(d.Seconds()) % 60
// Build the string with appropriate units
if days > 0 {
if days == 1 {
parts = append(parts, "1 day")
} else {
parts = append(parts, fmt.Sprintf("%d days", days))
}
}
if hours > 0 {
if hours == 1 {
parts = append(parts, "1 hour")
} else {
parts = append(parts, fmt.Sprintf("%d hours", hours))
}
}
if minutes > 0 {
if minutes == 1 {
parts = append(parts, "1 minute")
} else {
parts = append(parts, fmt.Sprintf("%d minutes", minutes))
}
}
if seconds > 0 && len(parts) < 2 { // Only show seconds if we don't have 2+ larger units
if seconds == 1 {
parts = append(parts, "1 second")
} else {
parts = append(parts, fmt.Sprintf("%d seconds", seconds))
}
}
// Handle very small durations
if len(parts) == 0 {
return "less than 1 second"
}
// Join parts appropriately
var result string
if len(parts) == 1 {
result = parts[0]
} else if len(parts) == 2 {
result = parts[0] + " " + parts[1]
} else {
// For 3+ parts, use only the first 2 most significant units
result = parts[0] + " " + parts[1]
}
if negative {
result = "-" + result
}
return result
}
// IsValidAge checks if the given birth date results in a valid age (not negative, not unreasonably old).
// This is useful for validating user input for birth dates.
//
// Example:
//
// birthDate := time.Date(1990, 5, 15, 0, 0, 0, 0, time.UTC)
// valid := gotime.IsValidAge(birthDate)
// // Returns: true
func IsValidAge(birthDate time.Time, asOf ...time.Time) bool {
var ref time.Time
if len(asOf) > 0 {
ref = asOf[0]
} else {
ref = time.Now()
}
// Birth date cannot be in the future
if birthDate.After(ref) {
return false
}
// Calculate age in years
years := YearsBetween(birthDate, ref)
// Reasonable age limits (0 to 150 years) - use <= for inclusive range
return years >= 0 && years <= 150
}