Skip to content

Commit 7364d8a

Browse files
author
Shuo
authored
Merge pull request #753 from openset/develop
Update: dayOfYear
2 parents f9e7b74 + 2c31b2a commit 7364d8a

File tree

1 file changed

+3
-28
lines changed

1 file changed

+3
-28
lines changed
Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,8 @@
11
package problem1154
22

3-
import "strconv"
4-
5-
var daysBefore = [...]int{
6-
0,
7-
31,
8-
31 + 28,
9-
31 + 28 + 31,
10-
31 + 28 + 31 + 30,
11-
31 + 28 + 31 + 30 + 31,
12-
31 + 28 + 31 + 30 + 31 + 30,
13-
31 + 28 + 31 + 30 + 31 + 30 + 31,
14-
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
15-
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
16-
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
17-
31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
18-
}
3+
import "time"
194

205
func dayOfYear(date string) int {
21-
year, _ := strconv.Atoi(date[:4])
22-
month, _ := strconv.Atoi(date[5:7])
23-
day, _ := strconv.Atoi(date[8:])
24-
day += daysBefore[month-1]
25-
if month > 2 && isLeap(year) {
26-
day++
27-
}
28-
return day
29-
}
30-
31-
func isLeap(year int) bool {
32-
return year%4 == 0 && (year%100 != 0 || year%400 == 0)
6+
t, _ := time.Parse("2006-01-02", date)
7+
return t.YearDay()
338
}

0 commit comments

Comments
 (0)