summaryrefslogtreecommitdiffci
path: root/internal/router/handlers/api-v1-tz-get.go
blob: 8b081130be76086486c68db95f362b49940e611e (plain)
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
package handlers

import (
	"fmt"
	"log/slog"
	"time"

	"github.com/SayaAndy/saya-today-web/internal/router"
	"github.com/gofiber/fiber/v2"
)

type GetTimezoneHandler struct {
	router.BasicHandler
}

func init() {
	router.Routes = append(router.Routes, &GetTimezoneHandler{})
}

func (r *GetTimezoneHandler) Filter() (method string, path string) {
	return "GET", "/api/v1/tz"
}

func (r *GetTimezoneHandler) IsTemplated() bool {
	return false
}

func (r *GetTimezoneHandler) TemplatesToInject() []string {
	return []string{}
}

func (r *GetTimezoneHandler) ToCache() router.CacheSetting {
	return router.Disabled
}

func (r *GetTimezoneHandler) ToValidateLang() router.LangSetting {
	return router.NotRequired
}

func (r *GetTimezoneHandler) Render(c *fiber.Ctx, supplements *router.Supplements, lang string, templateMap fiber.Map) (statusCode int, err error) {
	timestampString := c.Query("timestamp")
	tz := c.Query("tz")

	loc, err := time.LoadLocation(tz)
	if err != nil {
		loc = time.UTC
		slog.Warn("unable to parse a client timezone, defaulting to UTC", slog.String("error", err.Error()), slog.String("tz", tz))
	}

	var format string
	var timestamp time.Time
	for _, format = range []string{"2006-01-02 15:04:05 -07:00", time.RFC3339} {
		if timestamp, err = time.Parse(format, timestampString); err == nil {
			break
		}
	}

	if err != nil {
		return fiber.StatusBadRequest, fmt.Errorf("invalid timestamp format for '%s'", timestampString)
	}

	templateMap["Output"] = []byte(timestamp.In(loc).Format("2006-01-02 15:04:05 -07:00"))

	return fiber.StatusOK, nil
}