Initial Commit -- I am a noob

This commit is contained in:
Sacha Ligthert 2022-12-15 20:12:52 +01:00
commit 4e60a3d5e5
3 changed files with 155 additions and 0 deletions

17
README.md Normal file
View File

@ -0,0 +1,17 @@
# TimePercentage
by Sacha Ligthert 2022, Amsterdam
## Goal
Print the current day and year in percentages along with a bar for visual reference.
For example. This was taken at 20:05 on 15th of December 2022:
```
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒░░░░░░░░ 83.7354816%
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒░░ 95.5718780%
```
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.
## Building
Checking out this repo and running `go build` should do this for you.

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module gitea.ligthert.net/sacha/timepercentage
go 1.19

135
tp.go Normal file
View File

@ -0,0 +1,135 @@
package main
import (
"fmt"
"time"
)
func renderYear() string {
// Get Current time
var ct time.Time
var offset int
ct, offset = getCurrentTime()
// So what the following does is
// Add one year to the current time (and call it next year)
// Get the start of this year
// Get the start of the next year
// The New Year
ny := ct.AddDate(1, 0, 0)
// Get the start of this and next year
t1 := startYear(ct.Year(), 1, 1)
t2 := startYear(ny.Year(), 1, 1)
// Subtract the current year in seconds from next year, in seconds.
// This will leave us with the total year in seconds
// I do this in case there is a leap-year,
// or some dodgy science reason to add 30 seconds to a day.
seconds := t2.Sub(t1).Seconds()
// From the current time, subtract the start of this year, in seconds.
// This should leave us with how far we are into the year, in seconds.
tdiff := ct.Sub(t1).Seconds()
// Calculate the percentages
percentage := calcPercentageFloat64(seconds, tdiff+float64(offset))
// Wrap things up
var render string
render = renderPercentage(percentage)
render = render + fmt.Sprintf(" %.7f%%", percentage)
return render
}
func renderDay() string {
// Get Current time
var ct time.Time
var offset int
ct, offset = getCurrentTime()
// Get the Start of the Day
sd := time.Date(ct.Year(), ct.Month(), ct.Day(), 0, 0, 0, 0, time.UTC)
// Get the End of the Day
ed := ct.AddDate(0, 0, 1)
ed = time.Date(ed.Year(), ed.Month(), ed.Day(), 0, 0, 0, 0, time.UTC)
// From the end of the day, subtract the end of the day to get the number of seconds.
// Normally this would be 86400, thanks to science this may change.
// Think Leap Seconds
//
// Get the current number of seconds into the day
seconds_total := ed.Sub(sd).Seconds()
seconds_tmp := ed.Sub(ct).Seconds()
seconds_current := seconds_total - seconds_tmp
// Calculate the percentages
percentage := calcPercentageFloat64(seconds_total, seconds_current+float64(offset))
// Wrap everything up
var render string
render = renderPercentage(percentage)
render = render + fmt.Sprintf(" %.7f%%", percentage)
return render
}
func getCurrentTime() (time.Time, int) {
// Current Time
ct := time.Now()
// Get the offset from whatever the system timezone is
_, offset := ct.Local().Zone()
// Make sure that the time is in UTC, so the offset makes sense
loc, _ := time.LoadLocation("UTC")
ct = ct.In(loc)
// Return things
return ct, offset
}
// displayPercentage Print a string of blocks simulating percentages
func renderPercentage(percentage float64) string {
var counter int
var balk string
for percentage > 2 {
balk = balk + "▓"
percentage = percentage - 2
counter++
}
if percentage < 2 {
balk = balk + "▒"
counter++
}
for counter < 50 {
balk = balk + "░"
counter++
}
return balk
}
// Get the percentages between two float64s
func calcPercentageFloat64(full float64, part float64) float64 {
return part / (full / 100)
}
// Get the start of the year.
func startYear(year, month, day int) time.Time {
return time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC)
}
func main() {
fmt.Println(renderDay())
fmt.Println(renderYear())
}