github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/unit/time.go (about)

     1  // Code generated by "go generate github.com/jingcheng-WU/gonum/unit”; DO NOT EDIT.
     2  
     3  // Copyright ©2014 The Gonum Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  package unit
     8  
     9  import (
    10  	"errors"
    11  	"fmt"
    12  	"math"
    13  	"unicode/utf8"
    14  )
    15  
    16  // Time represents a duration in seconds.
    17  type Time float64
    18  
    19  const (
    20  	Second Time = 1
    21  
    22  	Minute = 60 * Second
    23  	Hour   = 60 * Minute
    24  )
    25  
    26  // Unit converts the Time to a *Unit.
    27  func (t Time) Unit() *Unit {
    28  	return New(float64(t), Dimensions{
    29  		TimeDim: 1,
    30  	})
    31  }
    32  
    33  // Time allows Time to implement a Timer interface.
    34  func (t Time) Time() Time {
    35  	return t
    36  }
    37  
    38  // From converts the unit into the receiver. From returns an
    39  // error if there is a mismatch in dimension.
    40  func (t *Time) From(u Uniter) error {
    41  	if !DimensionsMatch(u, Second) {
    42  		*t = Time(math.NaN())
    43  		return errors.New("unit: dimension mismatch")
    44  	}
    45  	*t = Time(u.Unit().Value())
    46  	return nil
    47  }
    48  
    49  func (t Time) Format(fs fmt.State, c rune) {
    50  	switch c {
    51  	case 'v':
    52  		if fs.Flag('#') {
    53  			fmt.Fprintf(fs, "%T(%v)", t, float64(t))
    54  			return
    55  		}
    56  		fallthrough
    57  	case 'e', 'E', 'f', 'F', 'g', 'G':
    58  		p, pOk := fs.Precision()
    59  		w, wOk := fs.Width()
    60  		const unit = " s"
    61  		switch {
    62  		case pOk && wOk:
    63  			fmt.Fprintf(fs, "%*.*"+string(c), pos(w-utf8.RuneCount([]byte(unit))), p, float64(t))
    64  		case pOk:
    65  			fmt.Fprintf(fs, "%.*"+string(c), p, float64(t))
    66  		case wOk:
    67  			fmt.Fprintf(fs, "%*"+string(c), pos(w-utf8.RuneCount([]byte(unit))), float64(t))
    68  		default:
    69  			fmt.Fprintf(fs, "%"+string(c), float64(t))
    70  		}
    71  		fmt.Fprint(fs, unit)
    72  	default:
    73  		fmt.Fprintf(fs, "%%!%c(%T=%g s)", c, t, float64(t))
    74  	}
    75  }