-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calendar.go
307 lines (241 loc) · 5 KB
/
Calendar.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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package main
import (
"errors"
"fmt"
"math"
"time"
)
const layoutRo = "02-01-2006"
type Date struct {
year int
month int
day int
}
//SetYear
func (d *Date) SetYear(year int) error {
if year < 1 {
return errors.New("invalid year")
}
d.year = year
return nil
}
func (d *Date) SetMonth(month int) error {
if month < 1 || month > 12 {
return errors.New("invalid month")
}
d.month = month
return nil
}
func (d *Date) SetDay(day int) error {
if day < 1 || day > 31 {
return errors.New("invalid day")
}
d.day = day
return nil
}
func (d *Date) Year() int {
return d.year
}
func (d *Date) Month() int {
return d.month
}
func (d *Date) Day() int {
return d.day
}
func (d *Date) ReadDate() {
var year, month, day int
fmt.Print("Year:\t\t")
readValue(&year)
_ = d.SetYear(year)
fmt.Print("Month:\t\t")
readValue(&month)
_ = d.SetMonth(month)
fmt.Print("Day:\t\t")
readValue(&day)
_ = d.SetDay(day)
fmt.Println("Date: ", d.Day(), "-", d.Month(), "-", d.Year())
}
func ToDate(year, month, day int) time.Time { // convert input to time.Date format
return time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC)
}
func IsLeap(y int) bool {
if y%100 != 0 && y%4 == 0 || y%400 == 0 {
return true
}
return false
}
func CountLeapYears(date time.Time) (leaps int) {
y, m, _ := date.Date()
if m <= 2 {
y--
}
leaps = y/4 + y/400 - y/100
return leaps
}
func GetDifference(a, b time.Time) (days int) {
monthDays := [12]int{31, 28, 31, 30, 31,
30, 31, 31, 30, 31, 30, 31}
y1, m1, d1 := a.Date()
y2, m2, d2 := b.Date()
totalDays1 := y1*365 + d1
for i := 0; i < (int)(m1)-1; i++ {
totalDays1 += monthDays[i]
}
totalDays1 += CountLeapYears(a)
totalDays2 := y2*365 + d2
for i := 0; i < (int)(m2)-1; i++ {
totalDays2 += monthDays[i]
}
totalDays2 += CountLeapYears(b)
days = totalDays2 - totalDays1
return days
}
func offsetDays(a time.Time) int {
y, m, d := a.Date()
offset := d
fmt.Println(offset)
if m-1 == 11 {
offset += 335
}
if m-1 == 10 {
offset += 304
}
if m-1 == 9 {
offset += 273
}
if m-1 == 8 {
offset += 243
}
if m-1 == 7 {
offset += 212
}
if m-1 == 6 {
offset += 181
}
if m-1 == 5 {
offset += 151
}
if m-1 == 4 {
offset += 120
}
if m-1 == 3 {
offset += 90
}
if m-1 == 2 {
offset += 59
}
if m-1 == 1 {
offset += 31
}
fmt.Println(offset)
if IsLeap(y) && m > 2 {
offset += 1
}
fmt.Println(offset)
return offset
}
func RevOffSetDays(offset int, y int) (d int, m int) {
month := [13]int{0, 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31}
if IsLeap(y) {
month[2] = 29
}
var i int
for i = 1; i <= 12; i++ {
if offset <= month[i] {
break
}
offset = offset - month[i]
}
d = offset
m = i
return d, m
}
func AddDays(a time.Time, days int) {
y, _, _ := a.Date()
offset1 := offsetDays(a)
fmt.Println(offset1)
remDays := 0
if IsLeap(y) {
remDays = 366 - offset1
} else {
remDays = 365 - offset1
}
var y1 int
var offset2 int
if days <= remDays {
y1 = y
offset2 = offset1 + days
} else {
days -= remDays
y1 = y + 1
var y1days int
if IsLeap(y1) {
y1days = 366
} else {
y1days = 365
}
for days >= y1days {
days -= y1days
y1++
if IsLeap(y1) {
y1days = 366
} else {
y1days = 365
}
}
offset2 = days
}
day, month := RevOffSetDays(offset2, y1)
fmt.Println(day, "-", month, "-", y1)
}
func TimeSubYearsMonthsWeeksDays(days float64) {
years := math.Floor(days / 365) // approximates down
months := math.Floor((days - years*365) / 30)
weeks := math.Floor((days - years*365 - months*30) / 7)
day := days - years*365 - months*30 - weeks*7 //calculates the number of days left in the calculation
fmt.Printf("The diffence between of these 2 dates: %v years, %v months, %v weeks, %v days. \n", -years, months, weeks, day)
fmt.Println()
}
func SubDays(t2 time.Time) {
layout := layoutRo
var numberOfDays int
fmt.Print("Number of days:\t\t")
readValue(&numberOfDays)
date := t2.AddDate(0, 0, numberOfDays*-1)
fmt.Printf("%v \n", date.Format(layout))
fmt.Println()
}
func readValue(value *int) {
_, err := fmt.Scanf("%d", value)
if err != nil {
fmt.Println(err)
}
}
func main() {
date1 := Date{day: 15, month: 10, year: 1999}
date2 := Date{day: 15, month: 10, year: 1998}
fmt.Println(date1.Year(), "-", date1.Month(), "-", date1.Day())
fmt.Println(date2.Year(), "-", date2.Month(), "-", date2.Day())
t1 := ToDate(date1.Year(), date1.Month(), date1.Day())
t2 := ToDate(date2.Year(), date2.Month(), date2.Day())
fmt.Println(" 1. Number of days between two dates \n 2. Difference of two dates in years, months, weeks and day \n 3. Add number of days on a date \n 4. Subtract number of days on a date \n ")
var caseNumber int
fmt.Print("Case Number:\t\t")
var _, _ = fmt.Scanln(&caseNumber)
switch caseNumber {
case 1:
if t1.After(t2) {
t1, t2 = t2, t1
}
days := GetDifference(t1, t2)
fmt.Println(days)
case 2:
days := GetDifference(t1, t2)
TimeSubYearsMonthsWeeksDays(float64(days))
case 3:
AddDays(t1, 366)
case 4:
SubDays(t2)
}
}