diff options
| author | sam-anthony <samanthony6@protonmail.com> | 2022-03-31 20:27:39 -0230 |
|---|---|---|
| committer | sam-anthony <samanthony6@protonmail.com> | 2022-03-31 20:27:39 -0230 |
| commit | 6634b3ff6bcdffbab38a049460ae6ea3cd68944f (patch) | |
| tree | 4cbcb7829060883afe074854a2ffc82b4ab2e31b /temperature/temperature.go | |
| parent | cc0d171c5cd1057693960b7f63aee29e7f70ee8e (diff) | |
| download | volute-6634b3ff6bcdffbab38a049460ae6ea3cd68944f.zip | |
refactor and compressor map image widget
Diffstat (limited to 'temperature/temperature.go')
| -rw-r--r-- | temperature/temperature.go | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/temperature/temperature.go b/temperature/temperature.go new file mode 100644 index 0000000..2a135df --- /dev/null +++ b/temperature/temperature.go @@ -0,0 +1,71 @@ +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)) + } +} |