Skip to content

Commit 2c31b2a

Browse files
author
Shuo
committed
Update: dayOfYear
1 parent 6676eab commit 2c31b2a

File tree

1 file changed

+3
-28
lines changed

1 file changed

+3
-28
lines changed
+3-28
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)