aboutsummaryrefslogtreecommitdiffstats
path: root/temperature/temperature.go
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2023-01-15 22:40:41 -0330
committerSam Anthony <sam@samanthony.xyz>2023-01-15 22:40:41 -0330
commitb6247a8372eb18a19c35a924316941d6c1e585d2 (patch)
treeeb7c1db8ff7f4524f3ac093d6f445ee52c747302 /temperature/temperature.go
parent585fbf852c1e76470df42ebe99ede62440ce19d9 (diff)
downloadvolute-b6247a8372eb18a19c35a924316941d6c1e585d2.zip
volume
Diffstat (limited to 'temperature/temperature.go')
-rw-r--r--temperature/temperature.go71
1 files changed, 0 insertions, 71 deletions
diff --git a/temperature/temperature.go b/temperature/temperature.go
deleted file mode 100644
index 2a135df..0000000
--- a/temperature/temperature.go
+++ /dev/null
@@ -1,71 +0,0 @@
-package temperature
-
-import (
- "errors"
- "fmt"
-)
-
-type unit int
-
-const (
- Celcius unit = iota
- Kelvin
- Fahrenheit
-)
-
-// UnitStrings returns a slice of strings, each representing a
-// unit.
-// This is necessary because giu.Combo only works with strings.
-func UnitStrings() []string {
- return []string{"°C", "°K", "°F"}
-}
-
-const (
- DefaultUnit unit = Celcius
- // DefaultUnitIndex is used to index UnitStrings().
- DefaultUnitIndex int32 = 0 // celcius
-)
-
-func UnitFromString(s string) (unit, error) {
- // Each case corresponds to a value in UnitStrings().
- switch s {
- case "°C":
- return Celcius, nil
- case "°K":
- return Kelvin, nil
- case "°F":
- return Fahrenheit, nil
- default:
- return *new(unit), errors.New(fmt.Sprintf("invalid unit: '%s'", s))
- }
-}
-
-type Temperature struct {
- Val float32
- Unit unit
-}
-
-func (t Temperature) AsUnit(u unit) (float32, error) {
- // Convert to celcius
- var c float32
- switch t.Unit {
- case Celcius:
- c = t.Val
- case Kelvin:
- c = t.Val - 272.15
- case Fahrenheit:
- c = (t.Val - 32.0) * (5.0 / 9.0)
- }
-
- // Convert to desired unit
- switch u {
- case Celcius:
- return c, nil
- case Kelvin:
- return c + 272.15, nil
- case Fahrenheit:
- return c*(9.0/5.0) + 32.0, nil
- default:
- return 0, errors.New(fmt.Sprintf("invalid unit: '%v'", u))
- }
-}