Skip to content

Commit bd0c04b

Browse files
committed
Fixed bug in reldate where Month and Monday were confused.
1 parent 6dc311e commit bd0c04b

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

codemeta.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"codeRepository": "https://github.com/caltechlibrary/datatools",
77
"issueTracker": "https://github.com/caltechlibrary/datatools/issues",
88
"license": "https://data.caltech.edu/license",
9-
"version": "0.0.25",
9+
"version": "0.0.26",
1010
"author": [
1111
{
1212
"@type": "Person",
@@ -31,4 +31,4 @@
3131
"Go",
3232
"Python"
3333
]
34-
}
34+
}

reldate/reldate.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,14 @@ func relativeWeekday(t time.Time, weekday time.Weekday) (time.Time, error) {
9393
// computes the relative time in days from time returning a new
9494
// time and error.
9595
func RelativeTime(t time.Time, i int, u string) (time.Time, error) {
96+
//NOTE: Month needs to come before Monday.
9697
switch {
98+
case strings.HasPrefix(u, "year"):
99+
return t.AddDate(i, 0, 0), nil
100+
case strings.HasPrefix(u, "month"):
101+
return t.AddDate(0, i, 0), nil
102+
case strings.HasPrefix(u, "week"):
103+
return t.AddDate(0, 0, 7*i), nil
97104
case strings.HasPrefix(u, "sun"):
98105
return relativeWeekday(t, time.Sunday)
99106
case strings.HasPrefix(u, "mon"):
@@ -110,12 +117,6 @@ func RelativeTime(t time.Time, i int, u string) (time.Time, error) {
110117
return relativeWeekday(t, time.Saturday)
111118
case strings.HasPrefix(u, "day"):
112119
return t.AddDate(0, 0, i), nil
113-
case strings.HasPrefix(u, "week"):
114-
return t.AddDate(0, 0, 7*i), nil
115-
case strings.HasPrefix(u, "month"):
116-
return t.AddDate(0, i, 0), nil
117-
case strings.HasPrefix(u, "year"):
118-
return t.AddDate(i, 0, 0), nil
119120
}
120121
return t, errors.New("Time unit must be day(s), week(s), month(s) or year(s) or weekday name.")
121122
}

0 commit comments

Comments
 (0)