Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
4faefb742b | |||
d29d658d72 | |||
d7b94b978c | |||
2a3225c169 | |||
70fe89228d | |||
1cd285635f | |||
baf22b802d | |||
41fe193d1e | |||
961d4563e7 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
builds
|
24
README.md
24
README.md
@ -12,6 +12,28 @@ For example. This was taken at 20:05 on 15th of December 2022:
|
||||
|
||||
This is goes great with Conky and it allows you to display this on your desktop. As the past grows larger and times keeps ebbing away, this tool allows you to track this.
|
||||
|
||||
## Usage
|
||||
```
|
||||
$ ./timepercentage.linux-amd64 --help
|
||||
Usage of ./timepercentage.linux-amd64:
|
||||
-custom
|
||||
Display a custom range
|
||||
-custom-start string
|
||||
Start of the custom range
|
||||
-custom-stop string
|
||||
Start of the custom range
|
||||
-day
|
||||
Display progress of the day. (default true)
|
||||
-year
|
||||
Display progress of this year. (default true)
|
||||
```
|
||||
|
||||
By default the tool prints the progress of the day and the year. A custom and optional date range can be specified by using the `custom`, `custom-start`, and `custom-stop` program flags. For example:
|
||||
```
|
||||
$ ./timepercentage.linux-amd64 --day=false --year=false --custom=true --custom-start="1990-01-01T12:00:00+01:00" --custom-stop="2030-01-01T12:00:00+01:00"
|
||||
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒░░░░░░░ 84.8765436%
|
||||
```
|
||||
The start and stop times have to be specified in the [RFC3339](https://www.rfc-editor.org/rfc/rfc3339) format.
|
||||
|
||||
## Building
|
||||
Checking out this repo and running `go build` should do this for you.
|
||||
Checking out this repo and running `go build` should do this for you. Or run `build.sh` to build for other platforms.
|
10
build.sh
Executable file
10
build.sh
Executable file
@ -0,0 +1,10 @@
|
||||
#!/bin/sh
|
||||
|
||||
mkdir -p builds
|
||||
|
||||
rm builds/*
|
||||
|
||||
go tool dist list | grep -v android | grep -v ios | grep -v wasip1 | awk -F '/' '{printf "echo Compiling %s/%s; env CGO_ENABLED=1 GOOS=%s GOARCH=%s go build -o builds/timepercentage.%s-%s\n",$1,$2,$1,$2,$1,$2 }' | sh
|
||||
|
||||
cd builds
|
||||
for i in `ls *windows*`; do mv -v $i $i.exe; done
|
76
tp.go
76
tp.go
@ -1,7 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -79,15 +81,42 @@ func renderDay() string {
|
||||
return render
|
||||
}
|
||||
|
||||
func renderCustom(t1 time.Time, t2 time.Time) string {
|
||||
|
||||
var ct time.Time
|
||||
var offset int
|
||||
ct, offset = getCurrentTime()
|
||||
|
||||
seconds := t2.Sub(t1).Seconds()
|
||||
tdiff := ct.Sub(t1).Seconds()
|
||||
|
||||
percentage := calcPercentageFloat64(seconds, tdiff+float64(offset))
|
||||
|
||||
var render string
|
||||
render = renderPercentage(percentage)
|
||||
render = render + fmt.Sprintf(" %.7f%%", percentage)
|
||||
|
||||
return render
|
||||
|
||||
}
|
||||
|
||||
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 +150,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.
|
||||
@ -130,6 +164,38 @@ func startYear(year, month, day int) time.Time {
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(renderDay())
|
||||
fmt.Println(renderYear())
|
||||
|
||||
var dayFlag = flag.Bool("day", true, "Display progress of the day.")
|
||||
var yearFlag = flag.Bool("year", true, "Display progress of this year.")
|
||||
|
||||
var customFlag = flag.Bool("custom", false, "Display a custom range")
|
||||
var customStart = flag.String("custom-start", "", "Start of the custom range")
|
||||
var customStop = flag.String("custom-stop", "", "Start of the custom range")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
if *dayFlag {
|
||||
fmt.Println(renderDay())
|
||||
}
|
||||
|
||||
if *yearFlag {
|
||||
fmt.Println(renderYear())
|
||||
}
|
||||
|
||||
if *customFlag {
|
||||
|
||||
bt, err := time.Parse(time.RFC3339, *customStart)
|
||||
if err != nil {
|
||||
log.Fatalln("Error parsing start time: ", err)
|
||||
}
|
||||
|
||||
pa, err := time.Parse(time.RFC3339, *customStop)
|
||||
if err != nil {
|
||||
log.Fatalln("Error parsing stop time: ", err)
|
||||
}
|
||||
|
||||
fmt.Println(renderCustom(bt, pa))
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
36
tp_test.go
Normal file
36
tp_test.go
Normal 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)
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user