add basic go program

This commit is contained in:
Nick Griffey 2024-02-21 21:35:55 -06:00
parent 4d42e6cae8
commit 990c9a66bd
4 changed files with 74 additions and 0 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
*.db *.db
xbit

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module dairydemon.net/xbit
go 1.19
require github.com/mattn/go-sqlite3 v1.14.22

2
go.sum Normal file
View File

@ -0,0 +1,2 @@
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=

66
main.go Normal file
View File

@ -0,0 +1,66 @@
package main
import (
"database/sql"
"flag"
"fmt"
_ "github.com/mattn/go-sqlite3"
"log"
"math"
"time"
)
type Event struct {
description string
timestamp int64
}
func formula(p float64) int64 {
yearsAgo := math.Exp(20.3444*math.Pow(p, 3)+3) - math.Exp(3)
return int64(yearsAgo * 31556926)
}
func closestEvent(t int64, db *sql.DB) (Event, error) {
var event Event
query := `
select description, timestamp
from events
where timestamp is not null
order by abs(? - timestamp) asc
limit 1
`
stmt, err := db.Prepare(query)
if err != nil {
return event, err
}
defer stmt.Close()
err = stmt.QueryRow(t).Scan(&event.description, &event.timestamp)
return event, err
}
func main() {
percent := flag.Float64("p", -1, "percentage")
flag.Parse()
if *percent < 0 || *percent > 1 {
log.Fatal("invalid percentage")
}
t := time.Now().Unix() - formula(*percent)
db, err := sql.Open("sqlite3", "./timeline.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
event, err := closestEvent(t, db)
if err != nil {
log.Fatal(err)
}
date := time.Unix(event.timestamp, 0)
fmt.Printf("%v\t%v\t%v\n", event.timestamp, date, event.description)
}