From 8ea40b88fe3258bc89936463de9fb5142c822a01 Mon Sep 17 00:00:00 2001 From: Techno Freak Date: Tue, 12 Dec 2023 23:24:17 +0300 Subject: [PATCH] feat: allow specifying timezone --- config.example.toml | 5 +++++ pkg/config/config.go | 8 +++++++- pkg/config/toml_config/toml_config.go | 6 ++++++ pkg/reporters/telegram/telegram.go | 2 +- 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/config.example.toml b/config.example.toml index b720053..cbb7d41 100644 --- a/config.example.toml +++ b/config.example.toml @@ -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. diff --git a/pkg/config/config.go b/pkg/config/config.go index 01815b1..8d475a8 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -3,6 +3,7 @@ package config import ( "bytes" "os" + "time" "gopkg.in/guregu/null.v4" @@ -34,6 +35,7 @@ type AppConfig struct { LogConfig LogConfig Chains Chains Metrics MetricsConfig + Timezone *time.Location } type TelegramConfig struct { @@ -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, @@ -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, } } @@ -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(), } } diff --git a/pkg/config/toml_config/toml_config.go b/pkg/config/toml_config/toml_config.go index 89afbec..ebafbc6 100644 --- a/pkg/config/toml_config/toml_config.go +++ b/pkg/config/toml_config/toml_config.go @@ -2,6 +2,7 @@ package toml_config import ( "fmt" + "time" "gopkg.in/guregu/null.v4" ) @@ -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 { @@ -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) diff --git a/pkg/reporters/telegram/telegram.go b/pkg/reporters/telegram/telegram.go index 2e255f4..2f24d9d 100644 --- a/pkg/reporters/telegram/telegram.go +++ b/pkg/reporters/telegram/telegram.go @@ -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)) }