Removed local xsd:duration functionality in favour of external package
This commit is contained in:
parent
6e7676346e
commit
c928d2da7e
|
@ -3,6 +3,7 @@ package activitypub
|
|||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.sr.ht/~mariusor/go-xsd-duration"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -79,7 +80,7 @@ func writeTimeProp(b *[]byte, n string, t time.Time) (notEmpty bool) {
|
|||
}
|
||||
|
||||
func writeDurationProp(b *[]byte, n string, d time.Duration) (notEmpty bool) {
|
||||
if v, err := marshalXSD(d); err == nil {
|
||||
if v, err := xsd.Marshal(d); err == nil {
|
||||
return writeProp(b, n, v)
|
||||
}
|
||||
return false
|
||||
|
|
1
go.mod
1
go.mod
|
@ -3,6 +3,7 @@ module github.com/go-ap/activitypub
|
|||
go 1.13
|
||||
|
||||
require (
|
||||
git.sr.ht/~mariusor/go-xsd-duration v0.0.0-20200411073322-f0bcc40f0bf2
|
||||
github.com/buger/jsonparser v0.0.0-20200322175846-f7e751efca13
|
||||
github.com/go-ap/jsonld v0.0.0-20191123195936-1e43eac08b0c
|
||||
)
|
||||
|
|
|
@ -1,84 +0,0 @@
|
|||
package activitypub
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
const day = time.Hour * 24
|
||||
const week = day * 7
|
||||
const month = week * 4
|
||||
const year = month * 12
|
||||
|
||||
func Days(d time.Duration) float64 {
|
||||
dd := d / day
|
||||
h := d % day
|
||||
return float64(dd) + float64(h)/(24*60*60*1e9)
|
||||
}
|
||||
func Weeks(d time.Duration) float64 {
|
||||
w := d / week
|
||||
dd := d % week
|
||||
return float64(w) + float64(dd)/(7*24*60*60*1e9)
|
||||
}
|
||||
func Months(d time.Duration) float64 {
|
||||
m := d / month
|
||||
w := d % month
|
||||
return float64(m) + float64(w)/(4*7*24*60*60*1e9)
|
||||
}
|
||||
func Years(d time.Duration) float64 {
|
||||
y := d / year
|
||||
m := d % year
|
||||
return float64(y) + float64(m)/(12*4*7*24*60*60*1e9)
|
||||
}
|
||||
|
||||
func marshalXSD(d time.Duration) ([]byte, error) {
|
||||
if d == 0 {
|
||||
return []byte{'P','T','0','S'}, nil
|
||||
}
|
||||
|
||||
neg := d < 0
|
||||
if neg {
|
||||
d = -d
|
||||
}
|
||||
y := Years(d)
|
||||
d -= time.Duration(y) * year
|
||||
m := Months(d)
|
||||
d -= time.Duration(m) * month
|
||||
dd := Days(d)
|
||||
d -= time.Duration(dd) * day
|
||||
H := d.Hours()
|
||||
d -= time.Duration(H) * time.Hour
|
||||
M := d.Minutes()
|
||||
d -= time.Duration(M) * time.Minute
|
||||
s := d.Seconds()
|
||||
d -= time.Duration(s) * time.Second
|
||||
b := bytes.Buffer{}
|
||||
if neg {
|
||||
b.Write([]byte{'-'})
|
||||
}
|
||||
b.Write([]byte{'P'})
|
||||
if y > 0 {
|
||||
b.WriteString(fmt.Sprintf("%dY", int64(y)))
|
||||
}
|
||||
if m > 0 {
|
||||
b.WriteString(fmt.Sprintf("%dM", int64(m)))
|
||||
}
|
||||
if dd > 0 {
|
||||
b.WriteString(fmt.Sprintf("%dD", int64(dd)))
|
||||
}
|
||||
|
||||
if H + M + s > 0 {
|
||||
b.Write([]byte{'T'})
|
||||
if H > 0 {
|
||||
b.WriteString(fmt.Sprintf("%dH", int64(H)))
|
||||
}
|
||||
if M > 0 {
|
||||
b.WriteString(fmt.Sprintf("%dM", int64(M)))
|
||||
}
|
||||
if s > 0 {
|
||||
b.WriteString(fmt.Sprintf("%dS", int64(s)))
|
||||
}
|
||||
}
|
||||
return b.Bytes(), nil
|
||||
}
|
|
@ -1,48 +0,0 @@
|
|||
package activitypub
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Test_marshalXSD(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
d time.Duration
|
||||
want []byte
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "Zero duration",
|
||||
d: 0,
|
||||
want: []byte("PT0S"),
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "One year",
|
||||
d: year,
|
||||
want: []byte("P1Y"),
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "XSD:duration example 1st",
|
||||
d: 2*year+6*month+5*day+12*time.Hour+35*time.Minute+30*time.Second,
|
||||
want: []byte("P2Y6M5DT12H35M30S"),
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := marshalXSD(tt.d)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("marshalXSD() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("marshalXSD() got = %s, want %s", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in a new issue