-
Notifications
You must be signed in to change notification settings - Fork 0
/
dates.go
156 lines (136 loc) · 3.12 KB
/
dates.go
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
// Simple date handling
package main
import (
"fmt"
"strconv"
"strings"
"time"
)
// simple struct and functions for date only: time is irrelevant
// days/months start from 1, not 0
type Date struct {
year int
month int
day int
}
// display a date in ISO whateveritis format
func show(d Date) string {
return fmt.Sprintf("%d-%02d-%02d", d.year, d.month, d.day)
}
type Period Date
var monthLengths = [12]int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
func monthLength(month, year int) int {
if month == 2 && isLeap(year) {
return 29
}
return monthLengths[month-1]
}
func isLeap(year int) bool {
switch {
case year%400 == 0:
return true
case year%100 == 0:
return false
case year%4 == 0:
return true
default:
return false
}
}
func dateRange(start, end Date, interval Period) []Date {
dates := make([]Date, 0)
for d := start; isNotAfter(d, end); d = postpone(d, interval) {
dates = append(dates, d)
}
return dates
}
// check if date a hasn't passed b (i.e. a <= b)
func isNotAfter(a, b Date) bool {
// switch b.year - a.year {
switch {
case b.year > a.year:
return true
case b.year < a.year:
return false
}
switch {
case b.month > a.month:
return true
case b.month < a.month:
return false
}
return b.day >= a.day
}
// advance a date by a certain interval of days/months/years
// Behaviour is arbitrary if days and months/years given to advance:
// this function advances years, then months, then days
// e.g. advancing 1mo31day from 15th Feb -> 15th Mar -> 15th Apr
func postpone(date Date, interval Period) Date {
date.year = date.year + interval.year
date.month = date.month + interval.month
date.day = date.day + interval.day
// if month or date number is higher than it should be
for {
// overflow months
if date.month > 12 {
date.year += date.month / 12
date.month = ((date.month - 1) % 12) + 1
}
// overflow days -- month by month in the for loop
currentMonthDays := monthLength(date.month, date.year)
if date.day <= currentMonthDays {
break
}
date.day -= currentMonthDays
date.month += 1
}
return date
}
// Read dates and periods from strings
// Including 'today', 'yesterday'
func readDate(date string) Date {
// first check for special dates
switch date {
case "today":
y, m, d := (time.Now()).Date()
return Date{y, int(m), d}
case "yesterday":
// <time.Time>.addDate(0,0,-1)
y, m, d := ((time.Now()).AddDate(0, 0, -1)).Date()
return Date{y, int(m), d}
}
ds := strings.Split(date, "-")
var y, m, d int
var err error
if len(ds) != 3 {
panic("so, is that a date?")
}
if y, err = strconv.Atoi(ds[0]); err != nil {
panic(err)
}
if m, err = strconv.Atoi(ds[1]); err != nil {
panic(err)
}
if d, err = strconv.Atoi(ds[2]); err != nil {
panic(err)
}
return Date{y, m, d}
}
func readPeriod(period string) Period {
ds := strings.Split(period, "-")
var y, m, d int
var err error
if len(ds) != 3 {
panic("so, is that a date?")
}
if y, err = strconv.Atoi(ds[0]); err != nil {
panic(err)
}
if m, err = strconv.Atoi(ds[1]); err != nil {
panic(err)
}
if d, err = strconv.Atoi(ds[2]); err != nil {
panic(err)
}
return Period{y, m, d}
}