vitess.io/vitess@v0.16.2/go/vt/vttablet/tabletserver/tabletenv/seconds.go (about)

     1  /*
     2  Copyright 2020 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package tabletenv
    18  
    19  import (
    20  	"time"
    21  
    22  	"github.com/spf13/pflag"
    23  )
    24  
    25  // Seconds provides convenience functions for extracting
    26  // duration from flaot64 seconds values.
    27  type Seconds float64
    28  
    29  // SecondsVar is like a flag.Float64Var, but it works for Seconds.
    30  func SecondsVar(fs *pflag.FlagSet, p *Seconds, name string, value Seconds, usage string) {
    31  	fs.Float64Var((*float64)(p), name, float64(value), usage)
    32  }
    33  
    34  // Get converts Seconds to time.Duration
    35  func (s Seconds) Get() time.Duration {
    36  	return time.Duration(s * Seconds(1*time.Second))
    37  }
    38  
    39  // Set sets the value from time.Duration
    40  func (s *Seconds) Set(d time.Duration) {
    41  	*s = Seconds(d) / Seconds(1*time.Second)
    42  }