Browse Source

Added timing basics

master
grumbulon 6 months ago
parent
commit
1b034ce21c
  1. 2
      db.go
  2. 1
      main.go
  3. 45
      timer.go

2
db.go

@ -32,7 +32,7 @@ func InitDb(Dbname string) {
defer db.Close()
sqlStmt := `
create table posts (id integer not null primary key AUTOINCREMENT, title text, url text, created timestamp);
create table posts (id integer not null primary key AUTOINCREMENT, title text, url text, created int);
delete from posts;
`
_, err = db.Exec(sqlStmt)

1
main.go

@ -46,6 +46,7 @@ func run(subreddit string) (err error) {
for i := 0; i < 5; i++ {
insert(db, posts[i])
}
getNewest()
getFiles()
return
}

45
timer.go

@ -0,0 +1,45 @@
package main
import (
"database/sql"
"fmt"
"log"
"time"
"github.com/joho/godotenv"
)
func getNewest() {
godotenv.Load()
var timestamps []int
db, err := sql.Open("sqlite3", opt.DB+".db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
row, err := db.Query("SELECT created FROM posts ORDER BY created DESC LIMIT 1") //get newest timestamp
if err != nil {
log.Fatalln(err)
}
defer row.Close()
for row.Next() {
var createdAt int
row.Scan(&createdAt)
timestamps = append(timestamps, createdAt)
}
calcDiff(timestamps)
}
func calcDiff(timestamp []int) {
fmt.Println(timestamp[0])
now := time.Now()
thirtyMinutes := time.Minute * time.Duration(30)
futureTime := now.Add(thirtyMinutes)
fmt.Println("30 minutes from now will be : ", futureTime.Unix())
}
func timing() {
}