github.com/m3db/m3@v1.5.0/src/query/util/timing.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package util
    22  
    23  import (
    24  	"fmt"
    25  	"math"
    26  	"strconv"
    27  	"time"
    28  
    29  	xerrors "github.com/m3db/m3/src/x/errors"
    30  	"github.com/prometheus/common/model"
    31  )
    32  
    33  var (
    34  	minTime = time.Unix(math.MinInt64/1000+62135596801, 0).UTC()
    35  	maxTime = time.Unix(math.MaxInt64/1000-62135596801, 999999999).UTC()
    36  
    37  	minTimeFormatted = minTime.Format(time.RFC3339Nano)
    38  	maxTimeFormatted = maxTime.Format(time.RFC3339Nano)
    39  )
    40  
    41  // ParseTimeString parses a time string into time.Time.
    42  func ParseTimeString(s string) (time.Time, error) {
    43  	if t, err := strconv.ParseFloat(s, 64); err == nil {
    44  		s, ns := math.Modf(t)
    45  		ns = math.Round(ns*1000) / 1000
    46  		return time.Unix(int64(s), int64(ns*float64(time.Second))), nil
    47  	}
    48  
    49  	if t, err := time.Parse(time.RFC3339Nano, s); err == nil {
    50  		return t, nil
    51  	}
    52  
    53  	// Stdlib's time parser can only handle 4 digit years. As a workaround until
    54  	// that is fixed we want to at least support our own boundary times.
    55  	// Context: https://github.com/prometheus/client_golang/issues/614
    56  	// Upstream issue: https://github.com/golang/go/issues/20555
    57  	switch s {
    58  	case minTimeFormatted:
    59  		return time.Unix(0, 0), nil
    60  	case maxTimeFormatted:
    61  		return time.Now(), nil
    62  	}
    63  
    64  	return time.Time{}, xerrors.NewInvalidParamsError(
    65  		fmt.Errorf("invalid timestamp for %s", s))
    66  }
    67  
    68  // ParseTimeStringWithDefault parses a time string into time.Time.
    69  func ParseTimeStringWithDefault(
    70  	s string,
    71  	defaultTime time.Time,
    72  ) (time.Time, error) {
    73  	if s != "" {
    74  		return ParseTimeString(s)
    75  	}
    76  	return defaultTime, nil
    77  }
    78  
    79  // ParseDurationString parses a string duration allows for
    80  // float seconds and also time strings such as 7d5h, etc.
    81  func ParseDurationString(s string) (time.Duration, error) {
    82  	if d, err := strconv.ParseFloat(s, 64); err == nil {
    83  		ts := d * float64(time.Second)
    84  		if ts > float64(math.MaxInt64) || ts < float64(math.MinInt64) {
    85  			return 0, xerrors.NewInvalidParamsError(
    86  				fmt.Errorf("cannot parse %q to a valid duration. It overflows int64", s))
    87  		}
    88  		return time.Duration(ts), nil
    89  	}
    90  	if d, err := model.ParseDuration(s); err == nil {
    91  		return time.Duration(d), nil
    92  	}
    93  	return 0, xerrors.NewInvalidParamsError(
    94  		fmt.Errorf("cannot parse %q to a valid duration", s))
    95  }
    96  
    97  // DurationToMS converts a duration into milliseconds
    98  func DurationToMS(duration time.Duration) int64 {
    99  	return duration.Nanoseconds() / int64(time.Millisecond)
   100  }