diff --git a/api/handlers.go b/api/handlers.go index 29bd9c5..aa4d4c2 100644 --- a/api/handlers.go +++ b/api/handlers.go @@ -10,7 +10,6 @@ import ( "log" "net/http" "strconv" - "time" ) type Handler struct { @@ -54,7 +53,7 @@ func writeError(w http.ResponseWriter, status int, msg string) { } func (h *Handler) handleGetToday(w http.ResponseWriter, r *http.Request) { - h.handleGetDayData(w, time.Now().Format("2006-01-02")) + h.handleGetDayData(w, h.cfg.Now().Format("2006-01-02")) } func (h *Handler) handleGetDay(w http.ResponseWriter, r *http.Request) { @@ -124,6 +123,16 @@ func (h *Handler) handleAddEntry(w http.ResponseWriter, r *http.Request) { return } + now := h.cfg.Now() + entryDate := now.Format("2006-01-02") + entryTime := now.Format("15:04") + if req.Date != "" { + entryDate = req.Date + if req.Date != now.Format("2006-01-02") { + entryTime = "12:00" + } + } + var entries []types.Entry for _, item := range estimation.Items { desc := item.Name @@ -132,6 +141,8 @@ func (h *Handler) handleAddEntry(w http.ResponseWriter, r *http.Request) { } entry := types.Entry{ + Date: entryDate, + Time: entryTime, Type: req.Type, Description: desc, Kcal: item.Kcal, @@ -193,7 +204,7 @@ func (h *Handler) handleWeighIn(w http.ResponseWriter, r *http.Request) { return } - weighIn, err := h.db.AddWeighIn("", req.WeightKg) + weighIn, err := h.db.AddWeighIn(h.cfg.Now().Format("2006-01-02"), req.WeightKg) if err != nil { writeError(w, http.StatusInternalServerError, err.Error()) return diff --git a/config/config.go b/config/config.go index b9bcffc..72aa4c9 100644 --- a/config/config.go +++ b/config/config.go @@ -18,6 +18,7 @@ func DefaultConfig() types.Config { ActivityLevel: "moderate", WeeklyLossKg: 0.5, ListenAddr: ":8080", + Timezone: "America/Halifax", } } diff --git a/types/types.go b/types/types.go index 1fd7da7..0c1bee8 100644 --- a/types/types.go +++ b/types/types.go @@ -56,9 +56,20 @@ type Config struct { ActivityLevel string `toml:"activity_level" json:"activity_level"` WeeklyLossKg float64 `toml:"weekly_loss_kg" json:"weekly_loss_kg"` ListenAddr string `toml:"listen_addr" json:"listen_addr"` + Timezone string `toml:"timezone" json:"timezone"` HasAPIKey bool `toml:"-" json:"has_api_key"` } +// Now returns the current time in the configured timezone. +func (c *Config) Now() time.Time { + if c.Timezone != "" { + if loc, err := time.LoadLocation(c.Timezone); err == nil { + return time.Now().In(loc) + } + } + return time.Now() +} + type TodayResponse struct { Date string `json:"date"` Entries []Entry `json:"entries"` @@ -72,6 +83,7 @@ type TodayResponse struct { type AddEntryRequest struct { Type EntryType `json:"type"` Description string `json:"description"` + Date string `json:"date,omitempty"` } type AddEntryResponse struct { diff --git a/web/favicon.svg b/web/favicon.svg new file mode 100644 index 0000000..cf7bcd7 --- /dev/null +++ b/web/favicon.svg @@ -0,0 +1,5 @@ + + + + CT + diff --git a/web/index.html b/web/index.html index f0a4231..4ec1fcf 100644 --- a/web/index.html +++ b/web/index.html @@ -4,6 +4,7 @@ CalTrack +