/
time.go
123 lines (104 loc) · 3.72 KB
/
time.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
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package mtime contains a millisecond representation of time. The purpose
// of this representation is alignment with the Beam specification, where we
// need extreme values outside the range of time.Time for windowing.
package mtime
import (
"fmt"
"math"
"time"
)
const (
// MinTimestamp is the minimum value for any Beam timestamp. Often referred to
// as "-infinity". This value and MaxTimestamp are chosen so that their
// microseconds-since-epoch can be safely represented with an int64 and boundary
// values can be represented correctly with millisecond precision.
MinTimestamp Time = math.MinInt64 / 1000
// MaxTimestamp is the maximum value for any Beam timestamp. Often referred to
// as "+infinity".
MaxTimestamp Time = math.MaxInt64 / 1000
// EndOfGlobalWindowTime is the timestamp at the end of the global window. It
// is a day before the max timestamp.
// TODO Use GLOBAL_WINDOW_MAX_TIMESTAMP_MILLIS from the Runner API constants
EndOfGlobalWindowTime = MaxTimestamp - 24*60*60*1000
// ZeroTimestamp is the default zero value time. It corresponds to the unix epoch.
ZeroTimestamp Time = 0
)
// Time is the number of milli-seconds since the Unix epoch. The valid range of
// times is bounded by what can be represented a _micro_-seconds-since-epoch.
type Time int64
// Now returns the current time.
func Now() Time {
return FromTime(time.Now())
}
// FromMilliseconds returns a timestamp from a raw milliseconds-since-epoch value.
func FromMilliseconds(unixMilliseconds int64) Time {
return Normalize(Time(unixMilliseconds))
}
// FromDuration returns a timestamp from a time.Duration-since-epoch value.
func FromDuration(d time.Duration) Time {
return ZeroTimestamp.Add(d)
}
// FromTime returns a milli-second precision timestamp from a time.Time.
func FromTime(t time.Time) Time {
return Normalize(Time(n2m(t.UnixNano())))
}
// Milliseconds returns the number of milli-seconds since the Unix epoch.
func (t Time) Milliseconds() int64 {
return int64(t)
}
// Add returns the time plus the duration.
func (t Time) Add(d time.Duration) Time {
return Normalize(Time(int64(t) + n2m(d.Nanoseconds())))
}
// Subtract returns the time minus the duration.
func (t Time) Subtract(d time.Duration) Time {
return Normalize(Time(int64(t) - n2m(d.Nanoseconds())))
}
func (t Time) String() string {
switch t {
case MinTimestamp:
return "-inf"
case MaxTimestamp:
return "+inf"
case EndOfGlobalWindowTime:
return "glo"
default:
return fmt.Sprintf("%v", t.Milliseconds())
}
}
// Min returns the smallest (earliest) time.
func Min(a, b Time) Time {
if int64(a) < int64(b) {
return a
}
return b
}
// Max returns the largest (latest) time.
func Max(a, b Time) Time {
if int64(a) < int64(b) {
return b
}
return a
}
// Normalize ensures a Time is within [MinTimestamp,MaxTimestamp].
func Normalize(t Time) Time {
return Min(Max(t, MinTimestamp), MaxTimestamp)
}
// n2m converts nanoseconds to milliseconds.
func n2m(v int64) int64 {
return v / 1e6
}