1. Prototyp

This commit is contained in:
concierge committed 2026-11-07 11:32:18 +01:00
1 parent e723eee440
commit c24bc91768
37 files changed
+11610

No files matched your search

+319
View File
@@ -0,0 +1,319 @@
import Foundation
/// All app dates are "calendar days" anchored at noon in the Europe/Berlin time zone.
/// Using noon avoids surprises around daylight-saving transitions while the ISO key
/// remains stable for persistence.
public enum DayMath {
public static var calendar: Calendar {
var cal = Calendar(identifier: .gregorian)
cal.locale = Locale(identifier: "de_DE")
cal.timeZone = TimeZone(identifier: "Europe/Berlin") ?? .current
cal.firstWeekday = 2
cal.minimumDaysInFirstWeek = 4
return cal
}
public static let germanLocale = Locale(identifier: "de_DE")
private static let keyFormatter: DateFormatter = {
let f = DateFormatter()
f.locale = Locale(identifier: "en_US_POSIX")
f.timeZone = TimeZone(identifier: "Europe/Berlin")
f.dateFormat = "yyyy-MM-dd"
return f
}()
private static let weekdayFormatter: DateFormatter = {
let f = DateFormatter()
f.locale = germanLocale
f.timeZone = TimeZone(identifier: "Europe/Berlin")
f.dateFormat = "EEEE"
return f
}()
private static let shortWeekdayFormatter: DateFormatter = {
let f = DateFormatter()
f.locale = germanLocale
f.timeZone = TimeZone(identifier: "Europe/Berlin")
f.dateFormat = "EEE"
return f
}()
private static let monthFormatter: DateFormatter = {
let f = DateFormatter()
f.locale = germanLocale
f.timeZone = TimeZone(identifier: "Europe/Berlin")
f.dateFormat = "MMMM"
return f
}()
private static let shortMonthFormatter: DateFormatter = {
let f = DateFormatter()
f.locale = germanLocale
f.timeZone = TimeZone(identifier: "Europe/Berlin")
f.dateFormat = "MMM"
return f
}()
private static let longFormatter: DateFormatter = {
let f = DateFormatter()
f.locale = germanLocale
f.timeZone = TimeZone(identifier: "Europe/Berlin")
f.dateFormat = "EEEE, d. MMMM yyyy"
return f
}()
private static let mediumFormatter: DateFormatter = {
let f = DateFormatter()
f.locale = germanLocale
f.timeZone = TimeZone(identifier: "Europe/Berlin")
f.dateFormat = "d. MMM yyyy"
return f
}()
public static func normalize(_ date: Date) -> Date {
let comps = calendar.dateComponents([.year, .month, .day], from: date)
return calendar.date(from: DateComponents(
timeZone: calendar.timeZone,
year: comps.year,
month: comps.month,
day: comps.day,
hour: 12
)) ?? date
}
public static func today() -> Date {
normalize(Date())
}
public static func date(fromKey key: String) -> Date? {
guard let date = keyFormatter.date(from: key) else { return nil }
return normalize(date)
}
public static func key(_ date: Date) -> String {
keyFormatter.string(from: date)
}
public static func key(year: Int, month: Int, day: Int) -> String {
String(format: "%04d-%02d-%02d", year, month, day)
}
public static func adding(days: Int, to date: Date) -> Date {
calendar.date(byAdding: .day, value: days, to: normalize(date)) ?? date
}
public static func adding(months: Int, to date: Date) -> Date {
calendar.date(byAdding: .month, value: months, to: normalize(date)) ?? date
}
public static func adding(years: Int, to date: Date) -> Date {
calendar.date(byAdding: .year, value: years, to: normalize(date)) ?? date
}
public static func daysBetween(_ start: Date, _ end: Date) -> Int {
calendar.dateComponents([.day], from: normalize(start), to: normalize(end)).day ?? 0
}
public static func days(in range: DayRange) -> [Date] {
guard range.start <= range.end else { return [] }
var result: [Date] = []
var cursor = normalize(range.start)
let end = normalize(range.end)
while cursor <= end {
result.append(cursor)
cursor = adding(days: 1, to: cursor)
}
return result
}
public static func weekday(_ date: Date) -> Int {
calendar.component(.weekday, from: normalize(date)) // 1 = Sunday … 7 = Saturday
}
public static func isSunday(_ date: Date) -> Bool {
weekday(date) == 1
}
public static func isSaturday(_ date: Date) -> Bool {
weekday(date) == 7
}
public static func weekdayName(_ date: Date) -> String {
weekdayFormatter.string(from: normalize(date))
}
public static func shortWeekdayName(_ date: Date) -> String {
shortWeekdayFormatter.string(from: normalize(date))
}
public static func monthName(_ date: Date) -> String {
monthFormatter.string(from: normalize(date))
}
public static func shortMonthName(_ date: Date) -> String {
shortMonthFormatter.string(from: normalize(date))
}
public static func longString(_ date: Date) -> String {
longFormatter.string(from: normalize(date))
}
public static func mediumString(_ date: Date) -> String {
mediumFormatter.string(from: normalize(date))
}
public static func dayNumber(_ date: Date) -> Int {
calendar.component(.day, from: normalize(date))
}
public static func month(_ date: Date) -> Int {
calendar.component(.month, from: normalize(date))
}
public static func year(_ date: Date) -> Int {
calendar.component(.year, from: normalize(date))
}
public static func startOfYear(_ date: Date) -> Date {
let y = year(date)
return calendar.date(from: DateComponents(timeZone: calendar.timeZone, year: y, month: 1, day: 1, hour: 12)) ?? date
}
public static func endOfYear(_ date: Date) -> Date {
let y = year(date)
return calendar.date(from: DateComponents(timeZone: calendar.timeZone, year: y, month: 12, day: 31, hour: 12)) ?? date
}
public static func startOfMonth(_ date: Date) -> Date {
let comps = calendar.dateComponents([.year, .month], from: date)
return calendar.date(from: DateComponents(
timeZone: calendar.timeZone, year: comps.year, month: comps.month, day: 1, hour: 12
)) ?? date
}
public static func endOfMonth(_ date: Date) -> Date {
let start = startOfMonth(date)
let next = adding(months: 1, to: start)
return adding(days: -1, to: next)
}
public static func startOfQuarter(_ date: Date) -> Date {
let m = month(date)
let quarterStartMonth = ((m - 1) / 3) * 3 + 1
return calendar.date(from: DateComponents(
timeZone: calendar.timeZone, year: year(date), month: quarterStartMonth, day: 1, hour: 12
)) ?? date
}
public static func endOfQuarter(_ date: Date) -> Date {
adding(days: -1, to: adding(months: 3, to: startOfQuarter(date)))
}
public static func quarter(_ date: Date) -> Int {
(month(date) - 1) / 3 + 1
}
public static func monthKey(_ date: Date) -> String {
String(format: "%04d-%02d", year(date), month(date))
}
public static func timeString(minutes: Int) -> String {
let h = max(0, min(23, minutes / 60))
let m = max(0, min(59, minutes % 60))
return String(format: "%02d:%02d", h, m)
}
public static func minutes(hour: Int, minute: Int) -> Int {
hour * 60 + minute
}
public static func ageString(from start: Date, to end: Date) -> String {
let days = abs(daysBetween(start, end)) + 1
return "\(days) Tage"
}
}
public struct DayRange: Hashable, Codable, Sendable, Identifiable {
public var start: Date
public var end: Date
public var id: String { "\(DayMath.key(start))_\(DayMath.key(end))" }
public init(start: Date, end: Date) {
let a = DayMath.normalize(start)
let b = DayMath.normalize(end)
if a <= b {
self.start = a
self.end = b
} else {
self.start = b
self.end = a
}
}
public init(startKey: String, endKey: String) {
let start = DayMath.date(fromKey: startKey) ?? DayMath.today()
let end = DayMath.date(fromKey: endKey) ?? start
self.init(start: start, end: end)
}
public static func quarter(containing date: Date) -> DayRange {
DayRange(start: DayMath.startOfQuarter(date), end: DayMath.endOfQuarter(date))
}
public static func year(_ year: Int) -> DayRange {
let start = DayMath.date(fromKey: DayMath.key(year: year, month: 1, day: 1)) ?? DayMath.today()
let end = DayMath.date(fromKey: DayMath.key(year: year, month: 12, day: 31)) ?? start
return DayRange(start: start, end: end)
}
public static func month(containing date: Date) -> DayRange {
DayRange(start: DayMath.startOfMonth(date), end: DayMath.endOfMonth(date))
}
public static func days(_ count: Int, from date: Date = DayMath.today()) -> DayRange {
DayRange(start: date, end: DayMath.adding(days: max(0, count - 1), to: date))
}
public var days: [Date] { DayMath.days(in: self) }
public var dayCount: Int { max(0, DayMath.daysBetween(start, end) + 1) }
public var monthCount: Int { max(1, (DayMath.year(end) - DayMath.year(start)) * 12 + DayMath.month(end) - DayMath.month(start) + 1) }
public var title: String {
if DayMath.year(start) == DayMath.year(end) {
return "\(DayMath.mediumString(start)) – \(DayMath.mediumString(end))"
}
return "\(DayMath.mediumString(start)) – \(DayMath.mediumString(end))"
}
public var shortTitle: String {
let startLabel = "\(DayMath.dayNumber(start)). \(DayMath.shortMonthName(start))"
let endLabel = "\(DayMath.dayNumber(end)). \(DayMath.shortMonthName(end))"
if DayMath.year(start) == DayMath.year(end) {
return "\(startLabel) – \(endLabel) \(DayMath.year(end))"
}
return "\(startLabel) \(DayMath.year(start)) – \(endLabel) \(DayMath.year(end))"
}
public func contains(_ date: Date) -> Bool {
let d = DayMath.normalize(date)
return d >= start && d <= end
}
public func clamped(to bounds: DayRange) -> DayRange {
DayRange(
start: max(start, bounds.start),
end: min(end, bounds.end)
)
}
public func extend(monthsBefore: Int, monthsAfter: Int) -> DayRange {
DayRange(
start: DayMath.adding(months: -monthsBefore, to: start),
end: DayMath.adding(months: monthsAfter, to: end)
)
}
}