aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/influxdata/influxdb/models/time.go
blob: e98f2cb3363505728645893c0d7b8a0d2d23b7ef (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
package models

// Helper time methods since parsing time can easily overflow and we only support a
// specific time range.

import (
    "fmt"
    "math"
    "time"
)

const (
    // MinNanoTime is the minumum time that can be represented.
    //
    // 1677-09-21 00:12:43.145224194 +0000 UTC
    //
    // The two lowest minimum integers are used as sentinel values.  The
    // minimum value needs to be used as a value lower than any other value for
    // comparisons and another separate value is needed to act as a sentinel
    // default value that is unusable by the user, but usable internally.
    // Because these two values need to be used for a special purpose, we do
    // not allow users to write points at these two times.
    MinNanoTime = int64(math.MinInt64) + 2

    // MaxNanoTime is the maximum time that can be represented.
    //
    // 2262-04-11 23:47:16.854775806 +0000 UTC
    //
    // The highest time represented by a nanosecond needs to be used for an
    // exclusive range in the shard group, so the maximum time needs to be one
    // less than the possible maximum number of nanoseconds representable by an
    // int64 so that we don't lose a point at that one time.
    MaxNanoTime = int64(math.MaxInt64) - 1
)

var (
    minNanoTime = time.Unix(0, MinNanoTime).UTC()
    maxNanoTime = time.Unix(0, MaxNanoTime).UTC()

    // ErrTimeOutOfRange gets returned when time is out of the representable range using int64 nanoseconds since the epoch.
    ErrTimeOutOfRange = fmt.Errorf("time outside range %d - %d", MinNanoTime, MaxNanoTime)
)

// SafeCalcTime safely calculates the time given. Will return error if the time is outside the
// supported range.
func SafeCalcTime(timestamp int64, precision string) (time.Time, error) {
    mult := GetPrecisionMultiplier(precision)
    if t, ok := safeSignedMult(timestamp, mult); ok {
        tme := time.Unix(0, t).UTC()
        return tme, CheckTime(tme)
    }

    return time.Time{}, ErrTimeOutOfRange
}

// CheckTime checks that a time is within the safe range.
func CheckTime(t time.Time) error {
    if t.Before(minNanoTime) || t.After(maxNanoTime) {
        return ErrTimeOutOfRange
    }
    return nil
}

// Perform the multiplication and check to make sure it didn't overflow.
func safeSignedMult(a, b int64) (int64, bool) {
    if a == 0 || b == 0 || a == 1 || b == 1 {
        return a * b, true
    }
    if a == MinNanoTime || b == MaxNanoTime {
        return 0, false
    }
    c := a * b
    return c, c/b == a
}