forked from periph/conn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpower.go
68 lines (59 loc) · 1.72 KB
/
power.go
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
// Copyright 2018 The Periph Authors. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.
// Modifications 2024 Sam Anthony.
package unit
// Power is a measurement of power stored as a nano watts.
//
// The highest representable value is 9.2GW.
type Power int64
// String returns the power formatted as a string in watts.
func (p Power) String() string {
return nanoAsString(int64(p)) + "W"
}
// Set sets the Power to the value represented by s. Units are to
// be provided in "W" with an optional SI prefix: "p", "n", "u", "µ", "m", "k",
// "M", "G" or "T".
func (p *Power) Set(s string) error {
v, n, err := valueOfUnitString(s, nano)
if err != nil {
if e, ok := err.(*parseError); ok {
switch e.error {
case errNotANumber:
if found := hasSuffixes(s, "W", "w"); found != "" {
return err
}
return notNumberUnitErr("W")
case errOverflowsInt64:
return maxValueErr(maxPower.String())
case errOverflowsInt64Negative:
return minValueErr(minPower.String())
}
}
return err
}
switch s[n:] {
case "W", "w":
*p = (Power)(v)
case "":
return noUnitErr("W")
default:
if found := hasSuffixes(s[n:], "W", "w"); found != "" {
return unknownUnitPrefixErr(found, "p,n,u,µ,m,k,M,G or T")
}
return incorrectUnitErr("W")
}
return nil
}
const (
// Watt is unit of power J/s, kg⋅m²⋅s⁻³
NanoWatt Power = 1
MicroWatt Power = 1000 * NanoWatt
MilliWatt Power = 1000 * MicroWatt
Watt Power = 1000 * MilliWatt
KiloWatt Power = 1000 * Watt
MegaWatt Power = 1000 * KiloWatt
GigaWatt Power = 1000 * MegaWatt
maxPower = 9223372036854775807 * NanoWatt
minPower = -9223372036854775807 * NanoWatt
)