-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
143 lines (126 loc) · 2.95 KB
/
main.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
package main
import (
"errors"
"github.com/gofiber/fiber/v2"
"github.com/golang-module/carbon/v2"
"github.com/vjeantet/eastertime"
"log"
"strconv"
)
func main() {
app := fiber.New(fiber.Config{
GETOnly: true,
ErrorHandler: func(c *fiber.Ctx, err error) error {
code := fiber.StatusInternalServerError
var e *fiber.Error
if errors.As(err, &e) {
code = e.Code
}
return c.Status(code).JSON(map[string]string{
"message": err.Error(),
})
},
})
app.Get("/", func(c *fiber.Ctx) error {
queryYear := c.Query("year", carbon.Now().Format("Y"))
year, err := strconv.Atoi(queryYear)
if err != nil {
return fiber.NewError(fiber.StatusUnprocessableEntity, "the year must be an integer")
}
holidays, err := getHolidays(year)
if err != nil {
return fiber.NewError(fiber.StatusUnprocessableEntity, err.Error())
}
return c.JSON(holidays)
})
err := app.Listen(":8080")
if err != nil {
log.Fatal(err)
}
}
func getHolidays(year int) ([]Holiday, error) {
orthodoxEaster, err := eastertime.OrthodoxByYear(year)
if err != nil {
return []Holiday{}, errors.New("the year must be greater than 325")
}
easter := carbon.CreateFromStdTime(orthodoxEaster)
secondDayOfEaster := easter.AddDay()
goodFriday := easter.SubDays(2)
whitMonday := easter.AddDays(50)
whitSunday := whitMonday.SubDay()
holidays := []Holiday{
{
Name: "Anul nou",
Date: carbon.CreateFromDate(year, 1, 1).ToDateString(),
},
{
Name: "Anul nou",
Date: carbon.CreateFromDate(year, 1, 2).ToDateString(),
},
{
Name: "Bobotează",
Date: carbon.CreateFromDate(year, 1, 6).ToDateString(),
},
{
Name: "Soborul Sfântului Ioan Botezătorul",
Date: carbon.CreateFromDate(year, 1, 7).ToDateString(),
},
{
Name: "Ziua Unirii",
Date: carbon.CreateFromDate(year, 1, 24).ToDateString(),
},
{
Name: "Vinerea Mare",
Date: goodFriday.ToDateString(),
},
{
Name: "Paștele",
Date: easter.ToDateString(),
},
{
Name: "A doua zi de Paște",
Date: secondDayOfEaster.ToDateString(),
},
{
Name: "Ziua Muncii",
Date: carbon.CreateFromDate(year, 5, 1).ToDateString(),
},
{
Name: "Ziua Copilului",
Date: carbon.CreateFromDate(year, 6, 1).ToDateString(),
},
{
Name: "Rusalii",
Date: whitSunday.ToDateString(),
},
{
Name: "A doua zi de Rusalii",
Date: whitMonday.ToDateString(),
},
{
Name: "Adormirea Maicii Domnului",
Date: carbon.CreateFromDate(year, 8, 15).ToDateString(),
},
{
Name: "Ziua Sfântului Andrei",
Date: carbon.CreateFromDate(year, 11, 30).ToDateString(),
},
{
Name: "Ziua naţională",
Date: carbon.CreateFromDate(year, 12, 1).ToDateString(),
},
{
Name: "Crăciunul",
Date: carbon.CreateFromDate(year, 12, 25).ToDateString(),
},
{
Name: "A doua zi de Crăciun",
Date: carbon.CreateFromDate(year, 12, 26).ToDateString(),
},
}
return holidays, nil
}
type Holiday struct {
Name string `json:"name"`
Date string `json:"date"`
}