From 690365f56bf27a4e7d0c0d717fec78577e9490c3 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Fri, 27 Aug 2021 23:19:44 +0200 Subject: [PATCH] Support dynamic date specification Signed-off-by: Knut Ahlers --- go.mod | 1 + go.sum | 2 ++ mod_streamSchedule.go | 27 ++++++++++++--------------- strftime.go | 35 +++++++++++++++++++++++++++++++++++ wiki/Home.md | 3 +++ 5 files changed, 53 insertions(+), 15 deletions(-) create mode 100644 strftime.go diff --git a/go.mod b/go.mod index 4e183cd..11d692f 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/Luzifer/korvike/functions v0.6.1 github.com/Luzifer/rconfig/v2 v2.2.1 github.com/bwmarrin/discordgo v0.23.2 + github.com/goodsign/monday v1.0.1-0.20210726100240-24c0b92f25dc github.com/pkg/errors v0.9.1 github.com/robfig/cron/v3 v3.0.1 github.com/sirupsen/logrus v1.8.1 diff --git a/go.sum b/go.sum index 5826bfd..d734b50 100644 --- a/go.sum +++ b/go.sum @@ -39,6 +39,8 @@ github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/goodsign/monday v1.0.1-0.20210726100240-24c0b92f25dc h1:OJv+Qvp14bbk2EVQ8dt4qYvTwv3V+wXFX7DVCyYm2Ys= +github.com/goodsign/monday v1.0.1-0.20210726100240-24c0b92f25dc/go.mod h1:r4T4breXpoFwspQNM+u2sLxJb2zyTaxVGqUfTBjWOu8= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= diff --git a/mod_streamSchedule.go b/mod_streamSchedule.go index 0f71264..988d9be 100644 --- a/mod_streamSchedule.go +++ b/mod_streamSchedule.go @@ -122,7 +122,7 @@ func (m modStreamSchedule) cronUpdateSchedule() { } msgEmbed.Fields = append(msgEmbed.Fields, &discordgo.MessageEmbedField{ - Name: m.formatGermanShort(*seg.StartTime), + Name: m.formatTime(*seg.StartTime), Value: title, Inline: false, }) @@ -172,21 +172,18 @@ func (m modStreamSchedule) cronUpdateSchedule() { log.Info("Updated Stream Schedule") } -func (m modStreamSchedule) formatGermanShort(t time.Time) string { - wd := map[time.Weekday]string{ - time.Monday: "Mo.", - time.Tuesday: "Di.", - time.Wednesday: "Mi.", - time.Thursday: "Do.", - time.Friday: "Fr.", - time.Saturday: "Sa.", - time.Sunday: "So.", - }[t.Weekday()] - - tz, err := time.LoadLocation("Europe/Berlin") +func (m modStreamSchedule) formatTime(t time.Time) string { + // @attr timezone optional string "UTC" Timezone to display the times in (e.g. "Europe/Berlin") + tz, err := time.LoadLocation(m.attrs.MustString("timezone", ptrString("UTC"))) if err != nil { - log.WithError(err).Fatal("Unable to load timezone Europe/Berlin") + log.WithError(err).Fatal("Unable to load timezone") } - return strings.Join([]string{wd, t.In(tz).Format("02.01. 15:04"), "Uhr"}, " ") + return localeStrftime( + t.In(tz), + // @attr time_format optional string "%b %d, %Y %I:%M %p" Time format in [limited strftime format](https://github.com/Luzifer/discord-community/blob/master/strftime.go) to use (e.g. "%a. %d.%m. %H:%M Uhr") + m.attrs.MustString("time_format", ptrString("%b %d, %Y %I:%M %p")), + // @attr locale optional string "en_US" Locale to translate the date to ([supported locales](https://github.com/goodsign/monday/blob/24c0b92f25dca51152defe82cefc7f7fc1c92009/locale.go#L9-L49)) + m.attrs.MustString("locale", ptrString("en_US")), + ) } diff --git a/strftime.go b/strftime.go new file mode 100644 index 0000000..a7c49a5 --- /dev/null +++ b/strftime.go @@ -0,0 +1,35 @@ +package main + +import ( + "strings" + "time" + + "github.com/goodsign/monday" +) + +var strftimeReplaces = []string{ + "%a", "Mon", // Weekday as locale’s abbreviated name. + "%A", "Monday", // Weekday as locale’s full name. + "%d", "02", // Day of the month as a zero-padded decimal number. + "%b", "Jan", // Month as locale’s abbreviated name. + "%B", "January", // Month as locale’s full name. + "%m", "01", // Month as a zero-padded decimal number. + "%y", "06", // Year without century as a zero-padded decimal number. + "%Y", "2006", // Year with century as a decimal number. + "%H", "15", // Hour (24-hour clock) as a zero-padded decimal number. + "%I", "03", // Hour (12-hour clock) as a zero-padded decimal number. + "%p", "PM", // Locale’s equivalent of either AM or PM. + "%M", "04", // Minute as a zero-padded decimal number. + "%S", "05", // Second as a zero-padded decimal number. + "%f", "000000", // Microsecond as a decimal number, zero-padded on the left. + "%z", "-0700", // UTC offset in the form ±HHMM[SS[.ffffff]] (empty string if the object is naive). + "%Z", "MST", // Time zone name (empty string if the object is naive). +} + +func localeStrftime(t time.Time, format, locale string) string { + return monday.Format( + t, + strings.NewReplacer(strftimeReplaces...).Replace(format), + monday.Locale(locale), + ) +} diff --git a/wiki/Home.md b/wiki/Home.md index 9fce536..4263eb9 100644 --- a/wiki/Home.md +++ b/wiki/Home.md @@ -144,8 +144,11 @@ Posts stream schedule derived from Twitch schedule as embed in Discord channel | `embed_thumbnail_height` | | int64 | | Height of the thumbnail | | `embed_thumbnail_url` | | string | | Publically hosted image URL to use as thumbnail | | `embed_thumbnail_width` | | int64 | | Width of the thumbnail | +| `locale` | | string | `en_US` | Locale to translate the date to ([supported locales](https://github.com/goodsign/monday/blob/24c0b92f25dca51152defe82cefc7f7fc1c92009/locale.go#L9-L49)) | | `schedule_entries` | | int64 | `5` | How many schedule entries to add to the embed as fields | | `schedule_past_time` | | duration | `15m` | How long in the past should the schedule contain an entry | +| `time_format` | | string | `%b %d, %Y %I:%M %p` | Time format in [limited strftime format](https://github.com/Luzifer/discord-community/blob/master/strftime.go) to use (e.g. "%a. %d.%m. %H:%M Uhr") | +| `timezone` | | string | `UTC` | Timezone to display the times in (e.g. "Europe/Berlin") |