6 Commits

3 changed files with 57 additions and 3 deletions

4
build.sh Executable file
View File

@ -0,0 +1,4 @@
#!/bin/bash
env GOOS=linux GOARCH=amd64 go build -o timepercentage.linux-amd64
env GOOS=windows GOARCH=amd64 go build -o timepercentage.windows-amd64.exe

20
tp.go
View File

@ -2,6 +2,7 @@ package main
import (
"fmt"
"log"
"time"
)
@ -80,14 +81,22 @@ func renderDay() string {
}
func getCurrentTime() (time.Time, int) {
// Declarations
var offset int
var loc *time.Location
var err error
// Current Time
ct := time.Now()
// Get the offset from whatever the system timezone is
_, offset := ct.Local().Zone()
_, offset = ct.Local().Zone()
// Make sure that the time is in UTC, so the offset makes sense
loc, _ := time.LoadLocation("UTC")
loc, err = time.LoadLocation("UTC")
if err != nil {
log.Fatal(err)
}
ct = ct.In(loc)
// Return things
@ -121,7 +130,12 @@ func renderPercentage(percentage float64) string {
// Get the percentages between two float64s
func calcPercentageFloat64(full float64, part float64) float64 {
return part / (full / 100)
var percentage float64
percentage = part / (full / 100)
if percentage >= 100 {
percentage = percentage - 100
}
return percentage
}
// Get the start of the year.

36
tp_test.go Normal file
View File

@ -0,0 +1,36 @@
package main
import "testing"
func TestRenderPercentage(t *testing.T) {
var percentage float64
percentage = 91.8948715
if renderPercentage(percentage) != "▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒░░░░" {
t.Errorf("renderPercentage(%f)", percentage)
}
percentage = 15.8682051
if renderPercentage(percentage) != "▓▓▓▓▓▓▓▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░" {
t.Errorf("renderPercentage(%f)", percentage)
}
}
func TestCalcPercentageFloat64(t *testing.T) {
var full float64
var part float64
full = 100
part = 50
if calcPercentageFloat64(full, part) != 50 {
t.Errorf("calcPercentageFloat64(%f,%f) test failed", full, part)
}
full = 8000
part = 2000
if calcPercentageFloat64(full, part) != 25 {
t.Errorf("calcPercentageFloat64(%f,%f) test failed", full, part)
}
}