49 lines
1.2 KiB
Swift
49 lines
1.2 KiB
Swift
//
|
|
// TestClock.swift
|
|
// SportsTimeTests
|
|
//
|
|
// Centralized time utilities for deterministic tests.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
enum TestClock {
|
|
static let timeZone = TimeZone.current
|
|
static let locale = Locale(identifier: "en_US_POSIX")
|
|
|
|
static let calendar: Calendar = {
|
|
var calendar = Calendar.current
|
|
calendar.timeZone = timeZone
|
|
calendar.locale = locale
|
|
return calendar
|
|
}()
|
|
|
|
static let baseDate: Date = {
|
|
let components = DateComponents(
|
|
calendar: calendar,
|
|
timeZone: timeZone,
|
|
year: 2026,
|
|
month: 1,
|
|
day: 15,
|
|
hour: 12,
|
|
minute: 0,
|
|
second: 0
|
|
)
|
|
return calendar.date(from: components) ?? Date(timeIntervalSince1970: 0)
|
|
}()
|
|
|
|
static var now: Date { baseDate }
|
|
|
|
static func startOfDay(for date: Date = baseDate) -> Date {
|
|
calendar.startOfDay(for: date)
|
|
}
|
|
|
|
static func addingDays(_ days: Int, to date: Date = baseDate) -> Date {
|
|
calendar.date(byAdding: .day, value: days, to: date) ?? date
|
|
}
|
|
|
|
static func addingHours(_ hours: Int, to date: Date = baseDate) -> Date {
|
|
calendar.date(byAdding: .hour, value: hours, to: date) ?? date
|
|
}
|
|
}
|