mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-29 07:23:56 +01:00
activitypub: go-fed conformant Clock instance
Signed-off-by: Loïc Dachary <loic@dachary.org>
This commit is contained in:
parent
678a56fbf8
commit
e8907c3c9e
2 changed files with 58 additions and 0 deletions
28
modules/activitypub/clock.go
Normal file
28
modules/activitypub/clock.go
Normal file
|
@ -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)
|
||||
}
|
30
modules/activitypub/clock_test.go
Normal file
30
modules/activitypub/clock_test.go
Normal file
|
@ -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))
|
||||
}
|
Loading…
Reference in a new issue