Skip to content

Commit c04e547

Browse files
authored
Add support for time zones in DateTag. (#131)
* Add support for time zones in `DateTag`. * Fix tests.
1 parent 547e48c commit c04e547

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

Sources/LeafKit/LeafSyntax/LeafTag.swift

+9
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ struct DateTag: LeafTag {
8787
throw "Unable to convert date format to string"
8888
}
8989
formatter.dateFormat = string
90+
case 3:
91+
guard let string = ctx.parameters[1].string else {
92+
throw "Unable to convert date format to string"
93+
}
94+
formatter.dateFormat = string
95+
guard let timeZone = ctx.parameters[2].string else {
96+
throw "Unable to convert time zone to string"
97+
}
98+
formatter.timeZone = TimeZone(identifier: timeZone)
9099
default:
91100
throw "invalid parameters provided for date"
92101
}

Tests/LeafKitTests/TagTests.swift

+24
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,30 @@ class TagTests: XCTestCase {
208208
try XCTAssertEqual(render(template, ["now": .int(now)]), expected)
209209
}
210210

211+
func testDateWithCustomFormatAndTimeZone() throws {
212+
let now = 1604932200 - Calendar.current.timeZone.secondsFromGMT()
213+
214+
let templateNewYork = """
215+
The date is #date(now, "yyyy-MM-dd'T'HH:mm", "America/New_York")
216+
"""
217+
218+
let expectedNewYork = """
219+
The date is 2020-11-09T09:30
220+
"""
221+
222+
try XCTAssertEqual(render(templateNewYork, ["now": .int(now)]), expectedNewYork)
223+
224+
let templateCalifornia = """
225+
The date is #date(now, "yyyy-MM-dd'T'HH:mm", "America/Los_Angeles")
226+
"""
227+
228+
let expectedCalifornia = """
229+
The date is 2020-11-09T06:30
230+
"""
231+
232+
try XCTAssertEqual(render(templateCalifornia, ["now": .int(now)]), expectedCalifornia)
233+
}
234+
211235
func testDumpContext() throws {
212236
let data: [String: LeafData] = ["value": 12345]
213237
let template = """

0 commit comments

Comments
 (0)