diff --git a/Sources/HTMLKit/CompiledTemplate/HTML+CompiledTemplate/ForEach+CompiledTemplate.swift b/Sources/HTMLKit/CompiledTemplate/HTML+CompiledTemplate/ForEach+CompiledTemplate.swift index 92b675ad..70a2dfff 100644 --- a/Sources/HTMLKit/CompiledTemplate/HTML+CompiledTemplate/ForEach+CompiledTemplate.swift +++ b/Sources/HTMLKit/CompiledTemplate/HTML+CompiledTemplate/ForEach+CompiledTemplate.swift @@ -17,6 +17,8 @@ extension TemplateForEach: CompiledTemplate { // View `BrewableFormula` documentation public func brew(_ formula: HTMLRenderer.Formula) throws { + localFormula.calendar = formula.calendar + localFormula.timeZone = formula.timeZone formula.add(mappable: self) try view.build().brew(localFormula) } diff --git a/Sources/HTMLKit/CompiledTemplate/HTML+CompiledTemplate/IF+CompiledTemplate.swift b/Sources/HTMLKit/CompiledTemplate/HTML+CompiledTemplate/IF+CompiledTemplate.swift index cd42aa3a..88e0cddd 100644 --- a/Sources/HTMLKit/CompiledTemplate/HTML+CompiledTemplate/IF+CompiledTemplate.swift +++ b/Sources/HTMLKit/CompiledTemplate/HTML+CompiledTemplate/IF+CompiledTemplate.swift @@ -33,6 +33,8 @@ extension TemplateIF: CompiledTemplate { public func brew(_ formula: HTMLRenderer.Formula) throws { formula.add(mappable: self) for condition in conditions { + condition.localFormula.calendar = formula.calendar + condition.localFormula.timeZone = formula.timeZone try condition.brew(formula) } } diff --git a/Sources/HTMLKit/DateVariable.swift b/Sources/HTMLKit/DateVariable.swift index 83300241..a4f6351d 100644 --- a/Sources/HTMLKit/DateVariable.swift +++ b/Sources/HTMLKit/DateVariable.swift @@ -59,6 +59,8 @@ struct DateVariable: CompiledTemplate { // View `CompiledTemplate` func brew(_ formula: HTMLRenderer.Formula) throws where T : ContextualTemplate { + formatter.calendar = formula.calendar + formatter.timeZone = formula.timeZone formula.add(mappable: self) } } @@ -76,7 +78,6 @@ extension ContextualTemplate { let formatter = DateFormatter() formatter.dateStyle = dateStyle formatter.timeStyle = timeStyle - formatter.timeZone = TimeZone(identifier: "UTC") return DateVariable(formatter: formatter, dateReferance: .solid(datePath)) } @@ -99,7 +100,6 @@ extension ContextualTemplate { public func date(_ datePath: KeyPath, format: String) -> CompiledTemplate { let formatter = DateFormatter() formatter.dateFormat = format - formatter.timeZone = TimeZone(identifier: "UTC") return DateVariable(formatter: formatter, dateReferance: .solid(datePath)) } @@ -114,7 +114,6 @@ extension ContextualTemplate { let formatter = DateFormatter() formatter.dateStyle = dateStyle formatter.timeStyle = timeStyle - formatter.timeZone = TimeZone(identifier: "UTC") return DateVariable(formatter: formatter, dateReferance: .optional(datePath)) } @@ -127,7 +126,6 @@ extension ContextualTemplate { public func date(_ datePath: KeyPath, format: String) -> CompiledTemplate { let formatter = DateFormatter() formatter.dateFormat = format - formatter.timeZone = TimeZone(identifier: "UTC") return DateVariable(formatter: formatter, dateReferance: .optional(datePath)) } diff --git a/Sources/HTMLKit/HTMLRenderer.swift b/Sources/HTMLKit/HTMLRenderer.swift index a60ed588..14df6b5a 100644 --- a/Sources/HTMLKit/HTMLRenderer.swift +++ b/Sources/HTMLKit/HTMLRenderer.swift @@ -40,6 +40,12 @@ public struct HTMLRenderer { /// The localization to use when rendering var lingo: Lingo? + /// The calendar to use when rendering dates + var calendar: Calendar = Calendar(identifier: .gregorian) + + /// The time zone to use when rendering dates + var timeZone: TimeZone = TimeZone(secondsFromGMT: 0) ?? TimeZone.current + public init() { formulaCache = [:] } @@ -105,7 +111,7 @@ public struct HTMLRenderer { /// - Parameter type: The view type to brew /// - Throws: If the brewing process fails for some reason public mutating func add(template view: T) throws { - let formula = Formula(view: T.self) + let formula = Formula(view: T.self, calendar: calendar, timeZone: timeZone) try view.build().brew(formula) formulaCache[String(reflecting: T.self)] = formula } @@ -117,7 +123,7 @@ public struct HTMLRenderer { /// - Parameter type: The view type to brew /// - Throws: If the brewing process fails for some reason public mutating func add(template view: T) throws { - let formula = Formula(view: T.self) + let formula = Formula(view: T.self, calendar: calendar, timeZone: timeZone) formula.localePath = T.localePath guard formula.localePath != nil else { throw Localize.Errors.missingLocalePath @@ -197,14 +203,22 @@ public struct HTMLRenderer { /// The path to the selected locale to use in localization var localePath: KeyPath? + /// The calendar to use when rendering dates + var calendar: Calendar + + /// The time zone to use when rendering dates + var timeZone: TimeZone + /// Init's a view /// /// - Parameters: /// - view: The view type /// - contextPaths: The contextPaths. *Is empty by default* - init(view: T.Type, contextPaths: [String : AnyKeyPath] = [:]) { + init(view: T.Type, calendar: Calendar, timeZone: TimeZone, contextPaths: [String : AnyKeyPath] = [:]) { self.contextPaths = contextPaths ingredient = [] + self.calendar = calendar + self.timeZone = timeZone } /// Registers a key-path for later referancing diff --git a/Sources/HTMLKit/TemplateForEach.swift b/Sources/HTMLKit/TemplateForEach.swift index 5cc96d4b..59bedd84 100644 --- a/Sources/HTMLKit/TemplateForEach.swift +++ b/Sources/HTMLKit/TemplateForEach.swift @@ -22,6 +22,6 @@ public class TemplateForEach) { self.view = view self.referance = referance - self.localFormula = HTMLRenderer.Formula(view: Value.self) + self.localFormula = HTMLRenderer.Formula(view: Value.self, calendar: .current, timeZone: .current) } } diff --git a/Sources/HTMLKit/TemplateIF.swift b/Sources/HTMLKit/TemplateIF.swift index 759508bf..9435309b 100644 --- a/Sources/HTMLKit/TemplateIF.swift +++ b/Sources/HTMLKit/TemplateIF.swift @@ -23,7 +23,7 @@ public class TemplateIF where Root: ContextualTemplate { /// - Parameter condition: The condition to evaluate init(condition: Conditionable) { self.condition = condition - localFormula = HTMLRenderer.Formula(view: Root.self) + localFormula = HTMLRenderer.Formula(view: Root.self, calendar: .current, timeZone: .current) } } diff --git a/Tests/HTMLKitTests/HTMLKitTests.swift b/Tests/HTMLKitTests/HTMLKitTests.swift index 3d795ea2..c2708775 100644 --- a/Tests/HTMLKitTests/HTMLKitTests.swift +++ b/Tests/HTMLKitTests/HTMLKitTests.swift @@ -21,10 +21,17 @@ final class HTMLKitTests: XCTestCase { let testDate = Date() let shortDateFormatter = DateFormatter() let customDateFormatter = DateFormatter() + shortDateFormatter.calendar = .current + shortDateFormatter.timeZone = .current shortDateFormatter.dateStyle = .short shortDateFormatter.timeStyle = .short + customDateFormatter.calendar = .current + customDateFormatter.timeZone = .current customDateFormatter.dateFormat = "MM/dd/yyyy" + renderer.calendar = .current + renderer.timeZone = .current + try renderer.add(template: StaticEmbedView()) try renderer.add(template: StaticEmbedView()) try renderer.add(template: SomeView())