diff options
author | Loïc Dachary | 2021-11-09 08:35:27 +0100 |
---|---|---|
committer | Anthony Wang | 2022-03-18 17:34:09 -0500 |
commit | e8907c3c9edbb57dfdfe5f5078b01415f4d54fdc (patch) | |
tree | 589968838c6268b5f90fcad0314d14c496c6f587 | |
parent | 678a56fbf81f6568ba82b6236c534ce10ab18fea (diff) |
activitypub: go-fed conformant Clock instance
Signed-off-by: Loïc Dachary <loic@dachary.org>
-rw-r--r-- | modules/activitypub/clock.go | 28 | ||||
-rw-r--r-- | modules/activitypub/clock_test.go | 30 |
2 files changed, 58 insertions, 0 deletions
diff --git a/modules/activitypub/clock.go b/modules/activitypub/clock.go new file mode 100644 index 000000000..88bd888e2 --- /dev/null +++ b/modules/activitypub/clock.go @@ -0,0 +1,28 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package activitypub + +import ( + "time" + + "code.gitea.io/gitea/modules/setting" + "github.com/go-fed/activity/pub" +) + +var _ pub.Clock = &Clock{} + +// Clock struct +type Clock struct{} + +// NewClock function +func NewClock() (c *Clock, err error) { + c = &Clock{} + return +} + +// Now function +func (c *Clock) Now() time.Time { + return time.Now().In(setting.DefaultUILocation) +} diff --git a/modules/activitypub/clock_test.go b/modules/activitypub/clock_test.go new file mode 100644 index 000000000..55b0cbb29 --- /dev/null +++ b/modules/activitypub/clock_test.go @@ -0,0 +1,30 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package activitypub + +import ( + "regexp" + "testing" + "time" + + "code.gitea.io/gitea/modules/setting" + + "github.com/stretchr/testify/assert" +) + +func TestClock(t *testing.T) { + DefaultUILocation := setting.DefaultUILocation + defer func() { + setting.DefaultUILocation = DefaultUILocation + }() + c, err := NewClock() + assert.NoError(t, err) + setting.DefaultUILocation, err = time.LoadLocation("UTC") + assert.NoError(t, err) + assert.Regexp(t, regexp.MustCompile(`\+0000$`), c.Now().Format(time.Layout)) + setting.DefaultUILocation, err = time.LoadLocation("Europe/Paris") + assert.NoError(t, err) + assert.Regexp(t, regexp.MustCompile(`\+0[21]00$`), c.Now().Format(time.Layout)) +} |