github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/sql/sem/tree/time.go (about)

     1  // Copyright 2019 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package tree
    12  
    13  import (
    14  	"time"
    15  
    16  	"github.com/cockroachdb/errors"
    17  )
    18  
    19  // TimeFamilyPrecisionToRoundDuration takes in a type's precision, and returns the
    20  // duration to use to pass into time.Truncate to truncate to that duration.
    21  // Panics if the precision is not supported.
    22  func TimeFamilyPrecisionToRoundDuration(precision int32) time.Duration {
    23  	switch precision {
    24  	case 0:
    25  		return time.Second
    26  	case 1:
    27  		return time.Millisecond * 100
    28  	case 2:
    29  		return time.Millisecond * 10
    30  	case 3:
    31  		return time.Millisecond
    32  	case 4:
    33  		return time.Microsecond * 100
    34  	case 5:
    35  		return time.Microsecond * 10
    36  	case 6:
    37  		return time.Microsecond
    38  	}
    39  	panic(errors.Newf("unsupported precision: %d", precision))
    40  }