summaryrefslogtreecommitdiffstats
path: root/time/time_test.go
blob: 3f0bb84ce567371345ab7cc8c84b13f926d3ad20 (plain) (blame)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright © 2022 siddharth ravikumar <s@ricketyspace.net>
// SPDX-License-Identifier: ISC

package time

import (
	"fmt"
	"testing"
	"time"
)

func TestDurationToSeconds(t *testing.T) {
	d, err := Duration("PT3H4M60S")
	if err != nil {
		t.Errorf("failed: %v", err)
		return
	}
	if d.Seconds() != 11100 {
		t.Errorf("duration in seconds incorrect: %v", d)
		return
	}

	d, err = Duration("PT4M60S")
	if err != nil {
		t.Errorf("failed: %v", err)
		return
	}
	if d.Seconds() != 300 {
		t.Errorf("duration in seconds incorrect: %v", d)
		return
	}

	d, err = Duration("PT12H")
	if err != nil {
		t.Errorf("failed: %v", err)
		return
	}
	if d.Seconds() != 43200 {
		t.Errorf("duration in seconds incorrect: %v", d)
		return
	}

	d, err = Duration("PT1H")
	if err != nil {
		t.Errorf("failed: %v", err)
		return
	}
	if d.Seconds() != 3600 {
		t.Errorf("duration in seconds incorrect: %v", d)
		return
	}

	d, err = Duration("PT2H")
	if err != nil {
		t.Errorf("failed: %v", err)
		return
	}
	if d.Seconds() != 7200 {
		t.Errorf("duration in seconds incorrect: %v", d)
		return
	}

	d, err = Duration("PT45M")
	if err != nil {
		t.Errorf("failed: %v", err)
		return
	}
	if d.Seconds() != 2700 {
		t.Errorf("duration in seconds incorrect: %v", d)
		return
	}

	d, err = Duration("P3DT45M")
	if err != nil {
		t.Errorf("failed: %v", err)
		return
	}
	if d.Seconds() != 261900 {
		t.Errorf("duration in seconds incorrect: %v", d)
		return
	}

	d, err = Duration("P7DT18H")
	if err != nil {
		t.Errorf("failed: %v", err)
		return
	}
	if d.Seconds() != 669600 {
		t.Errorf("duration in seconds incorrect: %v", d)
		return
	}
}

func TestIsCurrent(t *testing.T) {
	yes, err := IsCurrent("2022-08-07T01:00:00+00:00/PT1H")
	if yes || err != nil {
		t.Errorf("iscurrent failed: %v: %v", yes, err)
		return
	}

	h, err := time.ParseDuration("-3600s")
	if err != nil {
		t.Errorf("-3600s parsing duration: %v", err)
		return
	}
	ts := time.Now().Add(h).UTC().Format(time.RFC3339)
	yes, err = IsCurrent(fmt.Sprintf("%s+00:00/PT2H", ts[:len(ts)-1]))
	if !yes || err != nil {
		t.Errorf("iscurrent failed: %v: %v", yes, err)
		return
	}

}