-
Notifications
You must be signed in to change notification settings - Fork 0
/
datetime_tstzspanset.go
159 lines (136 loc) · 4.12 KB
/
datetime_tstzspanset.go
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package gomeos
/*
#include "meos.h"
#include <stdio.h>
#include <stdlib.h>
#include "cast.h"
*/
import "C"
import (
"fmt"
"time"
"unsafe"
"github.com/leekchan/timeutil"
)
type TsTzSpanSet struct {
_inner *C.SpanSet
}
func NewTsTzSpanSet(g_tts_in string) *TsTzSpanSet {
c_tts_in := C.CString(g_tts_in)
defer C.free(unsafe.Pointer(c_tts_in))
c_tts := C.tstzspanset_in(c_tts_in)
g_tts := &TsTzSpanSet{_inner: c_tts}
return g_tts
}
func (g_tts *TsTzSpanSet) TsTzSpanSetOut() string {
c_tts_out := C.tstzspanset_out(g_tts._inner)
defer C.free(unsafe.Pointer(c_tts_out))
g_tts_out := C.GoString(c_tts_out)
return g_tts_out
}
func (g_tts *TsTzSpanSet) ToSpan() *TsTzSpan {
c_ds := C.spanset_span(g_tts._inner)
return &TsTzSpan{_inner: c_ds}
}
func (g_tts *TsTzSpanSet) ToTsTzSpan() *TsTzSpan {
return g_tts.ToSpan()
}
func (g_tts *TsTzSpanSet) Duration(ignore_gap bool) timeutil.Timedelta {
interval := C.tstzspanset_duration(g_tts._inner, C.bool(ignore_gap))
delta := IntervalToTimeDelta(*interval)
return delta
}
func (g_tts *TsTzSpanSet) NumTimestamps() int {
return int(C.tstzspanset_num_timestamps(g_tts._inner))
}
func (g_tts *TsTzSpanSet) StartTimestamp() time.Time {
start_ts := C.tstzspanset_start_timestamptz(g_tts._inner)
return TimestamptzToDatetime(start_ts)
}
func (g_tts *TsTzSpanSet) EndTimestamp() time.Time {
start_ts := C.tstzspanset_end_timestamptz(g_tts._inner)
return TimestamptzToDatetime(start_ts)
}
func (g_tts *TsTzSpanSet) TimestampN(n int) time.Time {
res := C.malloc(C.sizeof_int)
defer C.free(unsafe.Pointer(res)) // Ensure memory is freed.
success := C.tstzspanset_timestamptz_n(g_tts._inner, C.int(n+1), (*C.TimestampTz)(res))
if success {
result := *(*C.TimestampTz)(res)
return TimestamptzToDatetime(result)
} else {
return time.Time{}
}
}
func (g_tts *TsTzSpanSet) Timestamps() []time.Time {
nums := g_tts.NumTimestamps()
timestamps := make([]time.Time, nums)
for i := 0; i < nums; i++ {
timestamps[i] = g_tts.TimestampN(i)
}
return timestamps
}
func (g_tts *TsTzSpanSet) NumSpans() int {
return int(C.spanset_num_spans(g_tts._inner))
}
func (g_tts *TsTzSpanSet) StartSpan() TsTzSpan {
return TsTzSpan{_inner: C.spanset_start_span(g_tts._inner)}
}
func (g_tts *TsTzSpanSet) EndSpan() TsTzSpan {
return TsTzSpan{_inner: C.spanset_end_span(g_tts._inner)}
}
func (g_tts *TsTzSpanSet) SpanN(n int) TsTzSpan {
return TsTzSpan{_inner: C.spanset_span_n(g_tts._inner, C.int(n+1))}
}
func (g_tts *TsTzSpanSet) Spans() []TsTzSpan {
nums := g_tts.NumSpans()
spans := make([]TsTzSpan, nums)
for i := 0; i < nums; i++ {
spans[i] = g_tts.SpanN(i)
}
return spans
}
func (g_tts *TsTzSpanSet) ShiftScale(shift interface{}, duration interface{}) (*TsTzSpanSet, error) {
if shift == nil && duration == nil {
return nil, fmt.Errorf("shift and duration must not be both nil")
}
var shift_delta, duration_delta int
var shift_in, duration_in timeutil.Timedelta
var shift_interval, duration_interval C.Interval
switch s := shift.(type) {
case timeutil.Timedelta:
shift_delta = 1
shift_in = s
shift_interval = TimeDeltaToInterval(shift_in)
case nil:
shift_delta = 0
default:
return &TsTzSpanSet{}, fmt.Errorf("operation not supported with type %T", shift)
}
switch d := duration.(type) {
case timeutil.Timedelta:
duration_delta = 1
duration_in = d
duration_interval = TimeDeltaToInterval(duration_in)
case nil:
duration_delta = 0
default:
return &TsTzSpanSet{}, fmt.Errorf("operation not supported with type %T", duration)
}
if (shift_delta == 1) && (duration_delta == 1) {
tss := C.tstzspanset_shift_scale(g_tts._inner, &shift_interval, &duration_interval)
return &TsTzSpanSet{_inner: tss}, nil
} else if shift_delta == 0 {
tss := C.tstzspanset_shift_scale(g_tts._inner, nil, &duration_interval)
return &TsTzSpanSet{_inner: tss}, nil
} else {
tss := C.tstzspanset_shift_scale(g_tts._inner, &shift_interval, nil)
return &TsTzSpanSet{_inner: tss}, nil
}
}
func (g_tts *TsTzSpanSet) Shift(delta interface{}) (*TsTzSpanSet, error) {
return g_tts.ShiftScale(delta, nil)
}
func (g_tts *TsTzSpanSet) Scale(duration interface{}) (*TsTzSpanSet, error) {
return g_tts.ShiftScale(nil, duration)
}