From 67071ea11feeca2704c358ab3bf7a059dc0b46d6 Mon Sep 17 00:00:00 2001
From: CrazyMax <1951866+crazy-max@users.noreply.github.com>
Date: Sun, 1 Nov 2020 00:22:39 +0100
Subject: [PATCH] Use zoneinfo from Go (#202)

Co-authored-by: CrazyMax <crazy-max@users.noreply.github.com>
---
 .github/workflows/build.yml   |  2 --
 Dockerfile                    |  1 -
 cmd/main.go                   | 11 ++---------
 docs/faq.md                   | 12 ++++++++++++
 docs/index.md                 |  1 -
 docs/install/linux-service.md |  1 +
 docs/usage/cli.md             |  2 --
 internal/app/diun.go          |  6 +++---
 internal/logging/logger.go    |  6 +-----
 internal/model/cli.go         |  1 -
 10 files changed, 19 insertions(+), 24 deletions(-)

diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 122500ea..36d1489e 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -4,7 +4,6 @@ on:
   push:
     branches:
       - '**'
-      - 'v*'
     tags:
       - 'v*'
     paths-ignore:
@@ -23,7 +22,6 @@ on:
       - 'mkdocs.yml'
 
 jobs:
-
   go:
     runs-on: ubuntu-latest
     steps:
diff --git a/Dockerfile b/Dockerfile
index b451f844..4d3eed1d 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -31,7 +31,6 @@ LABEL maintainer="CrazyMax"
 RUN apk --update --no-cache add \
     ca-certificates \
     libressl \
-    tzdata \
   && rm -rf /tmp/* /var/cache/apk/*
 
 COPY --from=builder /app/diun /usr/local/bin/diun
diff --git a/cmd/main.go b/cmd/main.go
index 69d8d146..98c749cf 100644
--- a/cmd/main.go
+++ b/cmd/main.go
@@ -7,7 +7,6 @@ import (
 	"runtime"
 	"strings"
 	"syscall"
-	"time"
 
 	"github.com/alecthomas/kong"
 	"github.com/crazy-max/diun/v4/internal/app"
@@ -54,14 +53,8 @@ func main() {
 			Summary: true,
 		}))
 
-	// Load timezone location
-	location, err := time.LoadLocation(cli.Timezone)
-	if err != nil {
-		log.Fatal().Err(err).Msgf("Cannot load timezone %s", cli.Timezone)
-	}
-
 	// Init
-	logging.Configure(&cli, location)
+	logging.Configure(&cli)
 	log.Info().Str("version", version).Msgf("Starting %s", meta.Name)
 
 	// Handle os signals
@@ -82,7 +75,7 @@ func main() {
 	log.Debug().Msg(cfg.String())
 
 	// Init
-	if diun, err = app.New(meta, cli, cfg, location); err != nil {
+	if diun, err = app.New(meta, cli, cfg); err != nil {
 		log.Fatal().Err(err).Msgf("Cannot initialize %s", meta.Name)
 	}
 
diff --git a/docs/faq.md b/docs/faq.md
index 2a8d1e4c..5c97cd6d 100644
--- a/docs/faq.md
+++ b/docs/faq.md
@@ -1,5 +1,17 @@
 # FAQ
 
+## Timezone
+
+By default, all interpretation and scheduling is done with your local timezone (`TZ` environment variable).
+
+Cron schedule may also override the timezone to be interpreted in by providing an additional space-separated field
+at the beginning of the cron spec, of the form `CRON_TZ=<timezone>`:
+
+```yaml
+watch:
+  schedule: "CRON_TZ=Asia/Tokyo 0 */6 * * *"
+```
+
 ## Test notifications
 
 Through the [command line](usage/cli.md) with:
diff --git a/docs/index.md b/docs/index.md
index 72ab1fb1..ce43bf31 100644
--- a/docs/index.md
+++ b/docs/index.md
@@ -36,7 +36,6 @@ and [File](providers/file.md) providers available
 * Get notified through Gotify, Mail, Slack, Telegram and [more](config/index.md#reference)
 * [Healthchecks support](config/watch.md#healthchecks) to monitor Diun watcher
 * Enhanced logging
-* Timezone can be changed
 * Official [Docker image available](install/docker.md)
 
 ## License
diff --git a/docs/install/linux-service.md b/docs/install/linux-service.md
index be4ed309..961cf591 100644
--- a/docs/install/linux-service.md
+++ b/docs/install/linux-service.md
@@ -21,6 +21,7 @@ User=diun
 Group=diun
 ExecStart=/usr/local/bin/diun --config /etc/diun/diun.yml --log-level info
 Restart=always
+#Environment=TZ=Europe/Paris
 Environment=DIUN_DB_PATH=/var/lib/diun/diun.db
 
 [Install]
diff --git a/docs/usage/cli.md b/docs/usage/cli.md
index 2b773680..83cddd45 100644
--- a/docs/usage/cli.md
+++ b/docs/usage/cli.md
@@ -18,7 +18,6 @@ Flags:
   --help                Show context-sensitive help.
   --version
   --config=STRING       Diun configuration file ($CONFIG).
-  --timezone="UTC"      Timezone assigned to Diun ($TZ).
   --log-level="info"    Set log level ($LOG_LEVEL).
   --log-json            Enable JSON logging output ($LOG_JSON).
   --log-caller          Add file:line of the caller to log output ($LOG_CALLER).
@@ -32,7 +31,6 @@ Following environment variables can be used in place:
 | Name               | Default       | Description   |
 |--------------------|---------------|---------------|
 | `CONFIG`           |               | Diun configuration file |
-| `TZ`               | `UTC`         | Timezone assigned |
 | `LOG_LEVEL`        | `info`        | Log level output |
 | `LOG_JSON`         | `false`       | Enable JSON logging output |
 | `LOG_CALLER`       | `false`       | Enable to add `file:line` of the caller |
diff --git a/internal/app/diun.go b/internal/app/diun.go
index c9aa33de..70193f11 100644
--- a/internal/app/diun.go
+++ b/internal/app/diun.go
@@ -39,14 +39,14 @@ type Diun struct {
 }
 
 // New creates new diun instance
-func New(meta model.Meta, cli model.Cli, cfg *config.Config, location *time.Location) (*Diun, error) {
+func New(meta model.Meta, cli model.Cli, cfg *config.Config) (*Diun, error) {
 	var err error
 
 	diun := &Diun{
 		meta: meta,
 		cfg:  cfg,
-		cron: cron.New(cron.WithLocation(location), cron.WithParser(cron.NewParser(
-			cron.SecondOptional|cron.Minute|cron.Hour|cron.Dom|cron.Month|cron.Dow|cron.Descriptor),
+		cron: cron.New(cron.WithParser(cron.NewParser(
+			cron.SecondOptional | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor),
 		)),
 	}
 
diff --git a/internal/logging/logger.go b/internal/logging/logger.go
index ec93e65e..64564d9a 100644
--- a/internal/logging/logger.go
+++ b/internal/logging/logger.go
@@ -13,14 +13,10 @@ import (
 )
 
 // Configure configures logger
-func Configure(cli *model.Cli, location *time.Location) {
+func Configure(cli *model.Cli) {
 	var err error
 	var w io.Writer
 
-	zerolog.TimestampFunc = func() time.Time {
-		return time.Now().In(location)
-	}
-
 	if !cli.LogJSON {
 		w = zerolog.ConsoleWriter{
 			Out:        os.Stdout,
diff --git a/internal/model/cli.go b/internal/model/cli.go
index 122b4dc4..f0a7ded3 100644
--- a/internal/model/cli.go
+++ b/internal/model/cli.go
@@ -6,7 +6,6 @@ import "github.com/alecthomas/kong"
 type Cli struct {
 	Version   kong.VersionFlag
 	Cfgfile   string `kong:"name='config',env='CONFIG',help='Diun configuration file.'"`
-	Timezone  string `kong:"name='timezone',env='TZ',default='UTC',help='Timezone assigned to Diun.'"`
 	LogLevel  string `kong:"name='log-level',env='LOG_LEVEL',default='info',help='Set log level.'"`
 	LogJSON   bool   `kong:"name='log-json',env='LOG_JSON',default='false',help='Enable JSON logging output.'"`
 	LogCaller bool   `kong:"name='log-caller',env='LOG_CALLER',default='false',help='Add file:line of the caller to log output.'"`