Support dynamic date specification

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2021-08-27 23:19:44 +02:00
parent 777dc47692
commit 690365f56b
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D
5 changed files with 53 additions and 15 deletions

1
go.mod
View file

@ -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

2
go.sum
View file

@ -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=

View file

@ -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")),
)
}

35
strftime.go Normal file
View file

@ -0,0 +1,35 @@
package main
import (
"strings"
"time"
"github.com/goodsign/monday"
)
var strftimeReplaces = []string{
"%a", "Mon", // Weekday as locales abbreviated name.
"%A", "Monday", // Weekday as locales full name.
"%d", "02", // Day of the month as a zero-padded decimal number.
"%b", "Jan", // Month as locales abbreviated name.
"%B", "January", // Month as locales 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", // Locales 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),
)
}

View file

@ -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") |