Skip to content

Commit

Permalink
feat: allow specifying timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Dec 12, 2023
1 parent 854b100 commit 8ea40b8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
5 changes: 5 additions & 0 deletions config.example.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Path to where aliases in .toml will be stored. If omitted, no aliases setting/displaying would work.
aliases = "/home/monitoring/config/cosmos-transactions-bot-aliases.toml"
# Timezone in which time (like undelegation finish time) will be displayed. Defaults to "Etc/GMT", so UTC+0
timezone = "Europe/Moscow"

# Logging configuration
[log]
# Log level. Set to "debug" or "trace" to make it more verbose, or to "warn"/"error" to make it less verbose.
Expand Down
8 changes: 7 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"bytes"
"os"
"time"

"gopkg.in/guregu/null.v4"

Expand Down Expand Up @@ -34,6 +35,7 @@ type AppConfig struct {
LogConfig LogConfig
Chains Chains
Metrics MetricsConfig
Timezone *time.Location
}

type TelegramConfig struct {
Expand Down Expand Up @@ -77,6 +79,8 @@ func GetConfig(path string) (*AppConfig, error) {
}

func FromTomlConfig(c *tomlConfig.TomlConfig, path string) *AppConfig {
timezone, _ := time.LoadLocation(c.Timezone)

return &AppConfig{
Path: path,
AliasesPath: c.AliasesPath,
Expand All @@ -96,6 +100,7 @@ func FromTomlConfig(c *tomlConfig.TomlConfig, path string) *AppConfig {
Chains: utils.Map(c.Chains, func(c *tomlConfig.Chain) *types.Chain {
return c.ToAppConfigChain()
}),
Timezone: timezone,
}
}

Expand All @@ -115,7 +120,8 @@ func (c *AppConfig) ToTomlConfig() *tomlConfig.TomlConfig {
ListenAddr: c.Metrics.ListenAddr,
Enabled: null.BoolFrom(c.Metrics.Enabled),
},
Chains: utils.Map(c.Chains, tomlConfig.FromAppConfigChain),
Chains: utils.Map(c.Chains, tomlConfig.FromAppConfigChain),
Timezone: c.Timezone.String(),
}
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/config/toml_config/toml_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package toml_config

import (
"fmt"
"time"

"gopkg.in/guregu/null.v4"
)
Expand All @@ -12,6 +13,7 @@ type TomlConfig struct {
LogConfig LogConfig `toml:"log"`
MetricsConfig MetricsConfig `toml:"metrics"`
Chains Chains `toml:"chains"`
Timezone string `default:"Etc/GMT" toml:"timezone"`
}

type TelegramConfig struct {
Expand All @@ -30,6 +32,10 @@ func (c *TomlConfig) Validate() error {
return fmt.Errorf("no chains provided")
}

if _, err := time.LoadLocation(c.Timezone); err != nil {
return fmt.Errorf("error parsing timezone: %s", err)
}

for index, chain := range c.Chains {
if err := chain.Validate(); err != nil {
return fmt.Errorf("error in chain %d: %s", index, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/reporters/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,5 +252,5 @@ func (reporter *TelegramReporter) SerializeAmount(amount amount.Amount) template
}

func (reporter *TelegramReporter) SerializeDate(date time.Time) template.HTML {
return template.HTML(date.Format(time.RFC822))
return template.HTML(date.In(reporter.Config.Timezone).Format(time.RFC822))
}

0 comments on commit 8ea40b8

Please sign in to comment.