Ejemplo n.º 1
0
func TestToDuration(t *testing.T) {
	t.Parallel()

	d := iso8601.Duration{Years: 1}
	assert.Equal(t, d.ToDuration(), time.Hour*24*365)

	d = iso8601.Duration{Weeks: 1}
	assert.Equal(t, d.ToDuration(), time.Hour*24*7)

	d = iso8601.Duration{Days: 1}
	assert.Equal(t, d.ToDuration(), time.Hour*24)

	d = iso8601.Duration{Hours: 1}
	assert.Equal(t, d.ToDuration(), time.Hour)

	d = iso8601.Duration{Minutes: 1}
	assert.Equal(t, d.ToDuration(), time.Minute)

	d = iso8601.Duration{Seconds: 1}
	assert.Equal(t, d.ToDuration(), time.Second)

	d = iso8601.Duration{Months: 2}
	fmt.Println(d.ToDuration())
}
Ejemplo n.º 2
0
func TestString(t *testing.T) {
	t.Parallel()

	// test empty
	d := iso8601.Duration{}
	assert.Equal(t, d.String(), "P")

	// test only larger-than-day
	d = iso8601.Duration{Years: 1, Days: 2}
	assert.Equal(t, d.String(), "P1Y2D")

	// test only smaller-than-day
	d = iso8601.Duration{Hours: 1, Minutes: 2, Seconds: 3}
	assert.Equal(t, d.String(), "PT1H2M3S")

	// test full format
	d = iso8601.Duration{Years: 1, Months: 2, Days: 3, Hours: 4, Minutes: 5, Seconds: 6}
	assert.Equal(t, d.String(), "P1Y2M3DT4H5M6S")

	// test week format
	d = iso8601.Duration{Weeks: 1}
	assert.Equal(t, d.String(), "P1W")
}