Skip to content

Commit

Permalink
Merge pull request #483 from traPtitech/fix/tz-jst
Browse files Browse the repository at this point in the history
fix tz
  • Loading branch information
ras0q authored Oct 31, 2023
2 parents 028abeb + 2404c62 commit ecd0c1f
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 35 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.20

require (
github.com/arran4/golang-ical v0.1.0
github.com/carlescere/scheduler v0.0.0-20170109141437-ee74d2f83d82
github.com/go-gormigrate/gormigrate/v2 v2.1.1
github.com/go-sql-driver/mysql v1.7.1
github.com/gofrs/uuid v4.4.0+incompatible
Expand All @@ -16,6 +15,7 @@ require (
github.com/labstack/echo/v4 v4.11.1
github.com/ory/dockertest/v3 v3.10.0
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/robfig/cron/v3 v3.0.1
github.com/stretchr/testify v1.8.4
github.com/traPtitech/go-traq v0.0.0-20220822084224-d405d26bce99
go.uber.org/zap v1.26.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 h1:TngWCqHvy9oXAN6lEV
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5/go.mod h1:lmUJ/7eu/Q8D7ML55dXQrVaamCz2vxCfdQBasLZfHKk=
github.com/arran4/golang-ical v0.1.0 h1:Oz0Rd5fpeNoHNFF9B9H5uYZyt1ubuZSZ3LVdHD5KvZI=
github.com/arran4/golang-ical v0.1.0/go.mod h1:BSTTrYHuM12oAL8jDdcmPdw02SBThKYWNFHQlvEG6b0=
github.com/carlescere/scheduler v0.0.0-20170109141437-ee74d2f83d82 h1:9bAydALqAjBfPHd/eAiJBHnMZUYov8m2PkXVr+YGQeI=
github.com/carlescere/scheduler v0.0.0-20170109141437-ee74d2f83d82/go.mod h1:tyA14J0sA3Hph4dt+AfCjPrYR13+vVodshQSM7km9qw=
github.com/cenkalti/backoff/v4 v4.1.3 h1:cFAlzYUlVYDysBEH2T5hyJZMh3+5+WCBvSnK6Q8UtC4=
github.com/cenkalti/backoff/v4 v4.1.3/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down Expand Up @@ -160,6 +158,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down
27 changes: 19 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ import (

"github.com/traPtitech/knoQ/domain"
"github.com/traPtitech/knoQ/infra/db"
"github.com/traPtitech/knoQ/repository"
"github.com/traPtitech/knoQ/infra/traq"
"github.com/traPtitech/knoQ/repository"
"github.com/traPtitech/knoQ/utils"
"github.com/traPtitech/knoQ/utils/tz"
"golang.org/x/oauth2"

"github.com/traPtitech/knoQ/router"

"github.com/carlescere/scheduler"
"github.com/gorilla/sessions"
"github.com/robfig/cron/v3"
"go.uber.org/zap"
)

Expand All @@ -42,8 +43,6 @@ var (
webhookSecret = getenv("WEBHOOK_SECRET", "")
activityChannelID = getenv("ACTIVITY_CHANNEL_ID", "")
dailyChannelID = getenv("DAILY_CHANNEL_ID", "")

jst, _ = time.LoadLocation("Asia/Tokyo")
)

func main() {
Expand All @@ -53,7 +52,7 @@ func main() {
domain.DEVELOPMENT, _ = strconv.ParseBool(development)

gormRepo := db.GormRepository{}
err := gormRepo.Setup(mariadbHost, mariadbUser, mariadbPassword, mariadbDatabase, mariadbPort, tokenKey, gormLogLevel, jst)
err := gormRepo.Setup(mariadbHost, mariadbUser, mariadbPassword, mariadbDatabase, mariadbPort, tokenKey, gormLogLevel, tz.JST)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -94,9 +93,21 @@ func main() {
e := handler.SetupRoute()

// webhook
job := utils.InitPostEventToTraQ(&repo.GormRepo, handler.WebhookSecret,
handler.DailyChannelId, handler.WebhookID, handler.Origin)
_, _ = scheduler.Every().Day().At("08:00").Run(job)
c := cron.New(cron.WithLocation(tz.JST))
_, err = c.AddFunc(
"0 8 * * *",
utils.InitPostEventToTraQ(
&repo.GormRepo,
handler.WebhookSecret,
handler.DailyChannelId,
handler.WebhookID,
handler.Origin,
),
)
if err != nil {
panic(err)
}
c.Start()

// サーバースタート
go func() {
Expand Down
4 changes: 2 additions & 2 deletions router/presentation/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/traPtitech/knoQ/domain"
"github.com/traPtitech/knoQ/utils/tz"

ics "github.com/arran4/golang-ical"
"github.com/gofrs/uuid"
Expand Down Expand Up @@ -161,7 +162,6 @@ func ICalFormat(events []*domain.Event, host string, userMap map[uuid.UUID]*doma
}

func GenerateEventWebhookContent(method string, e *EventDetailRes, nofiticationTargets []string, origin string, isMention bool) string {
jst, _ := time.LoadLocation("Asia/Tokyo")
timeFormat := "01/02(Mon) 15:04"
var content string
switch method {
Expand All @@ -172,7 +172,7 @@ func GenerateEventWebhookContent(method string, e *EventDetailRes, nofiticationT
}
content += fmt.Sprintf("### [%s](%s/events/%s)", e.Name, origin, e.ID) + "\n"
content += fmt.Sprintf("- 主催: [%s](%s/groups/%s)", e.GroupName, origin, e.Group.ID) + "\n"
content += fmt.Sprintf("- 日時: %s ~ %s", e.TimeStart.In(jst).Format(timeFormat), e.TimeEnd.In(jst).Format(timeFormat)) + "\n"
content += fmt.Sprintf("- 日時: %s ~ %s", e.TimeStart.In(tz.JST).Format(timeFormat), e.TimeEnd.In(tz.JST).Format(timeFormat)) + "\n"
content += fmt.Sprintf("- 場所: %s", e.Room.Place) + "\n"
content += "\n"

Expand Down
6 changes: 3 additions & 3 deletions router/presentation/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/gofrs/uuid"
"github.com/traPtitech/knoQ/domain"
"github.com/traPtitech/knoQ/utils/tz"
)

//go:generate go run github.com/fuji8/gotypeconverter/cmd/gotypeconverter@latest -s RoomReq -d domain.WriteRoomParams -o converter.go .
Expand Down Expand Up @@ -75,17 +76,16 @@ func ConvdomainRoomToRoomRes(src domain.Room) (dst RoomRes) {
func ChangeRoomCSVReqTodomainWriteRoomParams(src RoomCSVReq, userID uuid.UUID) (*domain.WriteRoomParams, error) {

layout := "2006/01/02 15:04"
jst, _ := time.LoadLocation("Asia/Tokyo")
var params domain.WriteRoomParams
var err error

params.Place = src.Location
params.TimeStart, err = time.ParseInLocation(layout, src.StartDate+" "+src.StartTime, jst)
params.TimeStart, err = time.ParseInLocation(layout, src.StartDate+" "+src.StartTime, tz.JST)
if err != nil {
return nil, err
}

params.TimeEnd, err = time.ParseInLocation(layout, src.EndDate+" "+src.EndTime, jst)
params.TimeEnd, err = time.ParseInLocation(layout, src.EndDate+" "+src.EndTime, tz.JST)
if err != nil {
return nil, err
}
Expand Down
9 changes: 4 additions & 5 deletions utils/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import (
"github.com/traPtitech/knoQ/domain"
"github.com/traPtitech/knoQ/domain/filter"
"github.com/traPtitech/knoQ/infra/db"
"github.com/traPtitech/knoQ/utils/tz"
"golang.org/x/exp/slices"
)

var jst, _ = time.LoadLocation("Asia/Tokyo")

type timeTable struct {
name string
start time.Time
Expand Down Expand Up @@ -41,7 +40,7 @@ func InitPostEventToTraQ(repo *db.GormRepository, secret, channelID, webhookID,

func setTimeFromString(t time.Time, str string) time.Time {
s, _ := time.Parse(time.TimeOnly, str)
return time.Date(t.Year(), t.Month(), t.Day(), s.Hour(), s.Minute(), s.Second(), 0, jst)
return time.Date(t.Year(), t.Month(), t.Day(), s.Hour(), s.Minute(), s.Second(), 0, tz.JST)
}

// makeRoomAvailableByTimeTable timeTables の各時間帯を行、rooms の各部屋を列とする表を map 形式で作成する。 unVerified の部屋は無視する。
Expand Down Expand Up @@ -97,7 +96,7 @@ func makeRoomAvailableByTimeTable(rooms []*domain.Room, timeTables []timeTable,
}

func createMessage(t time.Time, rooms []*domain.Room, events []*db.Event, origin string) string {
date := t.In(jst).Format("01/02(Mon)")
date := t.In(tz.JST).Format("01/02(Mon)")
combined := map[bool]string{
true: "(併用可)",
false: "",
Expand Down Expand Up @@ -164,7 +163,7 @@ func createMessage(t time.Time, rooms []*domain.Room, events []*db.Event, origin
} else {
for _, event := range events {
eventMessage += fmt.Sprintf("- [%s](%s/events/%s) %s ~ %s @%s %s\n", event.Name, origin, event.ID,
event.TimeStart.In(jst).Format("15:04"), event.TimeEnd.In(jst).Format("15:04"),
event.TimeStart.In(tz.JST).Format("15:04"), event.TimeEnd.In(tz.JST).Format("15:04"),
event.Room.Place, combined[event.AllowTogether])
}

Expand Down
14 changes: 0 additions & 14 deletions utils/format.go

This file was deleted.

5 changes: 5 additions & 0 deletions utils/tz/jst.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package tz

import "time"

var JST = time.FixedZone("Asia/Tokyo", 9*60*60)

0 comments on commit ecd0c1f

Please sign in to comment.