-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStations.go
More file actions
78 lines (64 loc) · 1.26 KB
/
Copy pathStations.go
File metadata and controls
78 lines (64 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"database/sql"
"fmt"
"log"
"time"
)
type StationsStruct struct {
SQLdb *sql.DB
statement *sql.Stmt
// ProviderStatement *sql.Stmt
}
type StationDataStruct struct {
Identifier string
Name string
Province string
Latitude string
Longitude string
Elevation string
TimeOffset int
URL string
}
func MakeStationsStruct() (o StationsStruct) {
o.SQLdb = Cache.GetSQLConnection()
RefreshCache, err := o.CacheIsStale()
if err != nil {
log.Panic(err)
}
if RefreshCache == false {
if Verbose {
fmt.Println("Used cached stations list.")
}
} else {
err = o.Import()
if err != nil {
log.Panic(err)
}
}
return
}
func (o *StationsStruct) CacheIsStale() (bool, error) {
var err error
if !RefreshStationCache {
SQL := `CREATE TABLE IF NOT EXISTS KeyValueStore (
"Key" TEXT PRIMARY KEY,
"Value" TEXT
) WITHOUT ROWID;`
_, err := o.SQLdb.Exec(SQL)
if err != nil {
log.Panic(err)
}
Now := time.Now().Unix()
SQL = `SELECT Value FROM KeyValueStore WHERE Key = ?`
var LastUpdated int64
err = o.SQLdb.QueryRow(SQL, "StationsLastUpdated").Scan(&LastUpdated)
if err == nil {
const Day = 24 * 60 * 60
if (Now - LastUpdated) <= (Day) {
return false, err
}
}
}
return true, err
}