feat: add --time mode for using 24h time format
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"time"
|
||||
"tz-snipe/internal/geodata"
|
||||
)
|
||||
|
||||
func GetTZsMatchingTime(db geodata.TimezoneMap, targetTime string) ([]string, error) {
|
||||
// another go quirK: for layout it uses a fixed point in time
|
||||
// as reference date Mon, 1/2 03:04:05 PM 2006 -0700
|
||||
// 1 2 3 4 5 6 7
|
||||
if _, err := time.Parse("15:04", targetTime); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
now := time.Now().UTC()
|
||||
var matches []string
|
||||
|
||||
for tz := range db {
|
||||
// Parse offset to location
|
||||
zoneTime, err := time.Parse("-0700", tz)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
// Check if the formatted string matches exactly
|
||||
// same minute match
|
||||
if now.In(zoneTime.Location()).Format("15:04") == targetTime {
|
||||
matches = append(matches, tz)
|
||||
}
|
||||
}
|
||||
return matches, nil
|
||||
}
|
||||
+18
-1
@@ -29,7 +29,7 @@ var _ tea.Model = Model{}
|
||||
// REMINDER REMINDER REMINDER
|
||||
// Model, NewModel, Init, Commands, Update, View is all you need
|
||||
|
||||
func NewModel(db geodata.TimezoneMap, ghUser, manualTz string) Model {
|
||||
func NewModel(db geodata.TimezoneMap, ghUser, manualTz, targetTime string) Model {
|
||||
m := Model{
|
||||
db: db,
|
||||
username: ghUser,
|
||||
@@ -46,6 +46,23 @@ func NewModel(db geodata.TimezoneMap, ghUser, manualTz string) Model {
|
||||
m.table = newTable(m.preds)
|
||||
m.displayTz = m.manualTz
|
||||
}
|
||||
case targetTime != "":
|
||||
// Logic for time matching
|
||||
tzs, err := core.GetTZsMatchingTime(db, targetTime)
|
||||
if err != nil {
|
||||
m.err = err
|
||||
} else if len(tzs) == 0 {
|
||||
m.err = fmt.Errorf("no timezones found matching %s", targetTime)
|
||||
} else {
|
||||
preds, err := core.GetStatsForTZs(tzs)
|
||||
if err != nil {
|
||||
m.err = err
|
||||
} else {
|
||||
m.preds = preds
|
||||
m.table = newTable(m.preds)
|
||||
m.displayTz = targetTime
|
||||
}
|
||||
}
|
||||
case ghUser != "":
|
||||
m.loadingGithubTz = true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user