Web Variante in Go V0.1

This commit is contained in:
concierge committed 2026-11-12 20:00:33 +01:00
1 parent f9e3e94629
commit 692b9b6ef8
60 files changed
+13140

No files matched your search

+290
View File
@@ -0,0 +1,290 @@
// Package domain contains the planning rules shared by the web application.
package domain
import (
"fmt"
"sort"
"strings"
"time"
)
// All app dates are calendar days anchored at noon in the Europe/Berlin time
// zone. Noon avoids daylight-saving surprises while the ISO key stays stable.
var Berlin = loadBerlin()
func loadBerlin() *time.Location {
if loc, err := time.LoadLocation("Europe/Berlin"); err == nil {
return loc
}
return time.UTC
}
func Normalize(t time.Time) time.Time {
y, m, d := t.In(Berlin).Date()
return time.Date(y, m, d, 12, 0, 0, 0, Berlin)
}
func Today() time.Time {
return Normalize(time.Now())
}
func DateFromKey(key string) (time.Time, bool) {
t, err := time.ParseInLocation("2006-01-02", key, Berlin)
if err != nil {
return time.Time{}, false
}
return Normalize(t), true
}
func MustDate(key string) time.Time {
t, ok := DateFromKey(key)
if !ok {
return Today()
}
return t
}
func Key(t time.Time) string {
return t.In(Berlin).Format("2006-01-02")
}
func KeyYMD(year, month, day int) string {
return fmt.Sprintf("%04d-%02d-%02d", year, month, day)
}
func AddingDays(days int, t time.Time) time.Time {
return Normalize(Normalize(t).AddDate(0, 0, days))
}
func AddingMonths(months int, t time.Time) time.Time {
t = Normalize(t)
y, m, d := t.Date()
first := time.Date(y, m, 1, 12, 0, 0, 0, Berlin).AddDate(0, months, 0)
last := first.AddDate(0, 1, -1)
if d > last.Day() {
d = last.Day()
}
return time.Date(first.Year(), first.Month(), d, 12, 0, 0, 0, Berlin)
}
func AddingYears(years int, t time.Time) time.Time {
t = Normalize(t)
return time.Date(t.Year()+years, t.Month(), t.Day(), 12, 0, 0, 0, Berlin)
}
func ordinal(t time.Time) int64 {
y, m, d := Normalize(t).Date()
return time.Date(y, m, d, 0, 0, 0, 0, time.UTC).Unix() / 86400
}
func DaysBetween(start, end time.Time) int {
return int(ordinal(end) - ordinal(start))
}
func DaysInRange(r DayRange) []time.Time {
if r.Start.After(r.End) {
return nil
}
var result []time.Time
for cursor := Normalize(r.Start); !cursor.After(r.End); cursor = AddingDays(1, cursor) {
result = append(result, cursor)
}
return result
}
// Weekday follows the Swift app's convention: 1 = Sunday … 7 = Saturday.
func Weekday(t time.Time) int {
return int(Normalize(t).Weekday()) + 1
}
func IsSunday(t time.Time) bool { return Weekday(t) == 1 }
func IsSaturday(t time.Time) bool { return Weekday(t) == 7 }
var weekdayNames = []string{"", "Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"}
var shortWeekdayNames = []string{"", "So", "Mo", "Di", "Mi", "Do", "Fr", "Sa"}
var monthNames = []string{"", "Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"}
var shortMonthNames = []string{"", "Jan.", "Feb.", "März", "Apr.", "Mai", "Juni", "Juli", "Aug.", "Sept.", "Okt.", "Nov.", "Dez."}
func WeekdayName(t time.Time) string { return weekdayNames[Weekday(t)] }
func ShortWeekdayName(t time.Time) string { return shortWeekdayNames[Weekday(t)] }
func MonthName(t time.Time) string { return monthNames[int(Normalize(t).Month())] }
func ShortMonthName(t time.Time) string { return shortMonthNames[int(Normalize(t).Month())] }
func LongString(t time.Time) string {
return fmt.Sprintf("%s, %d. %s %d", WeekdayName(t), DayNumber(t), MonthName(t), Year(t))
}
func MediumString(t time.Time) string {
return fmt.Sprintf("%d. %s %d", DayNumber(t), ShortMonthName(t), Year(t))
}
func DayNumber(t time.Time) int { return Normalize(t).Day() }
func Month(t time.Time) int { return int(Normalize(t).Month()) }
func Year(t time.Time) int { return Normalize(t).Year() }
func StartOfYear(t time.Time) time.Time {
return time.Date(Year(t), 1, 1, 12, 0, 0, 0, Berlin)
}
func EndOfYear(t time.Time) time.Time {
return time.Date(Year(t), 12, 31, 12, 0, 0, 0, Berlin)
}
func StartOfMonth(t time.Time) time.Time {
y, m, _ := Normalize(t).Date()
return time.Date(y, m, 1, 12, 0, 0, 0, Berlin)
}
func EndOfMonth(t time.Time) time.Time {
return AddingDays(-1, AddingMonths(1, StartOfMonth(t)))
}
func StartOfQuarter(t time.Time) time.Time {
m := Month(t)
quarterStart := ((m-1)/3)*3 + 1
return time.Date(Year(t), time.Month(quarterStart), 1, 12, 0, 0, 0, Berlin)
}
func EndOfQuarter(t time.Time) time.Time {
return AddingDays(-1, AddingMonths(3, StartOfQuarter(t)))
}
func Quarter(t time.Time) int { return (Month(t)-1)/3 + 1 }
func MonthKey(t time.Time) string {
return fmt.Sprintf("%04d-%02d", Year(t), Month(t))
}
func TimeString(minutes int) string {
if minutes < 0 {
minutes = 0
}
h := minutes / 60
m := minutes % 60
if h > 23 {
h = 23
}
if m > 59 {
m = 59
}
return fmt.Sprintf("%02d:%02d", h, m)
}
func Minutes(hour, minute int) int { return hour*60 + minute }
func ParseTimeString(s string) (int, bool) {
var h, m int
if _, err := fmt.Sscanf(strings.TrimSpace(s), "%d:%d", &h, &m); err != nil {
return 0, false
}
if h < 0 || h > 23 || m < 0 || m > 59 {
return 0, false
}
return h*60 + m, true
}
// DayRange is an inclusive calendar-day range.
type DayRange struct {
Start time.Time
End time.Time
}
func NewDayRange(start, end time.Time) DayRange {
a, b := Normalize(start), Normalize(end)
if a.After(b) {
a, b = b, a
}
return DayRange{Start: a, End: b}
}
func RangeFromKeys(startKey, endKey string) DayRange {
start := MustDate(startKey)
end, ok := DateFromKey(endKey)
if !ok {
end = start
}
return NewDayRange(start, end)
}
func QuarterRange(date time.Time) DayRange {
return NewDayRange(StartOfQuarter(date), EndOfQuarter(date))
}
func YearRange(year int) DayRange {
return NewDayRange(MustDate(KeyYMD(year, 1, 1)), MustDate(KeyYMD(year, 12, 31)))
}
func MonthRange(date time.Time) DayRange {
return NewDayRange(StartOfMonth(date), EndOfMonth(date))
}
func DaysRange(count int, from time.Time) DayRange {
if count < 1 {
count = 1
}
return NewDayRange(from, AddingDays(count-1, from))
}
func (r DayRange) Days() []time.Time { return DaysInRange(r) }
func (r DayRange) DayCount() int { return DaysBetween(r.Start, r.End) + 1 }
func (r DayRange) Contains(t time.Time) bool {
d := Normalize(t)
return !d.Before(r.Start) && !d.After(r.End)
}
func (r DayRange) MonthCount() int {
return (Year(r.End)-Year(r.Start))*12 + Month(r.End) - Month(r.Start) + 1
}
func (r DayRange) Title() string {
return MediumString(r.Start) + " – " + MediumString(r.End)
}
func (r DayRange) ShortTitle() string {
startLabel := fmt.Sprintf("%d. %s", DayNumber(r.Start), ShortMonthName(r.Start))
endLabel := fmt.Sprintf("%d. %s", DayNumber(r.End), ShortMonthName(r.End))
if Year(r.Start) == Year(r.End) {
return fmt.Sprintf("%s – %s %d", startLabel, endLabel, Year(r.End))
}
return fmt.Sprintf("%s %d – %s %d", startLabel, Year(r.Start), endLabel, Year(r.End))
}
func (r DayRange) LongTitle() string {
return fmt.Sprintf("%s bis %s · %d Tage", LongString(r.Start), LongString(r.End), r.DayCount())
}
func (r DayRange) Clamped(bounds DayRange) DayRange {
start, end := r.Start, r.End
if start.Before(bounds.Start) {
start = bounds.Start
}
if end.After(bounds.End) {
end = bounds.End
}
if end.Before(start) {
end = start
}
return NewDayRange(start, end)
}
func (r DayRange) Extend(monthsBefore, monthsAfter int) DayRange {
return NewDayRange(AddingMonths(-monthsBefore, r.Start), AddingMonths(monthsAfter, r.End))
}
// CurrentChurchYear returns the liturgical year containing the date.
func CurrentChurchYear(date time.Time) DayRange {
y := Year(date)
thisAdvent := AdventStart(y)
if date.Before(thisAdvent) {
return NewDayRange(AdventStart(y-1), ChristKing(y))
}
return NewDayRange(thisAdvent, ChristKing(y+1))
}
func SortedKeys(m map[string]int) []string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}