File size: 1,296 Bytes
fea99b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package datetime

import (
	"testing"
	"time"

	"github.com/rickb777/period"
	"github.com/stretchr/testify/assert"
)

// MustLoadLocation is a helper function that panics if the location cannot be loaded.
func MustLoadLocation(t *testing.T, name string) *time.Location {
	t.Helper()
	loc, err := time.LoadLocation(name)
	if err != nil {
		t.Fatalf("failed to load timezone location %q: %v", name, err)
	}
	return loc
}

// MustParseDateTime is a helper function to parse time with error checking
func MustParseDateTime(t *testing.T, timeStr string) DateTime {
	t.Helper()
	dt, err := Parse(timeStr)
	assert.NoError(t, err, "failed to parse time string %q", timeStr)
	return dt
}

// MustParseTimeInLocation is a helper function to parse time in specific location
func MustParseTimeInLocation(t *testing.T, timeStr string, loc *time.Location) DateTime {
	t.Helper()
	parsedTime, err := time.Parse(time.RFC3339, timeStr)
	assert.NoError(t, err, "failed to parse time string %q", timeStr)
	return DateTime{Time: parsedTime.In(loc)}
}

// MustParseDuration is a helper function to parse duration with error checking
func MustParseDuration(t *testing.T, s string) ISODuration {
	res, err := period.Parse(s)
	if err != nil {
		t.Fatalf("failed to parse period: %v", err)
	}

	return ISODuration{res}
}