diff --git a/Sources/LeafKit/LeafSyntax/LeafTag.swift b/Sources/LeafKit/LeafSyntax/LeafTag.swift index c8fa105..ecdbb91 100644 --- a/Sources/LeafKit/LeafSyntax/LeafTag.swift +++ b/Sources/LeafKit/LeafSyntax/LeafTag.swift @@ -87,6 +87,15 @@ struct DateTag: LeafTag { throw "Unable to convert date format to string" } formatter.dateFormat = string + case 3: + guard let string = ctx.parameters[1].string else { + throw "Unable to convert date format to string" + } + formatter.dateFormat = string + guard let timeZone = ctx.parameters[2].string else { + throw "Unable to convert time zone to string" + } + formatter.timeZone = TimeZone(identifier: timeZone) default: throw "invalid parameters provided for date" } diff --git a/Tests/LeafKitTests/TagTests.swift b/Tests/LeafKitTests/TagTests.swift index 101fe1f..424e875 100644 --- a/Tests/LeafKitTests/TagTests.swift +++ b/Tests/LeafKitTests/TagTests.swift @@ -208,6 +208,30 @@ class TagTests: XCTestCase { try XCTAssertEqual(render(template, ["now": .int(now)]), expected) } + func testDateWithCustomFormatAndTimeZone() throws { + let now = 1604932200 - Calendar.current.timeZone.secondsFromGMT() + + let templateNewYork = """ + The date is #date(now, "yyyy-MM-dd'T'HH:mm", "America/New_York") + """ + + let expectedNewYork = """ + The date is 2020-11-09T09:30 + """ + + try XCTAssertEqual(render(templateNewYork, ["now": .int(now)]), expectedNewYork) + + let templateCalifornia = """ + The date is #date(now, "yyyy-MM-dd'T'HH:mm", "America/Los_Angeles") + """ + + let expectedCalifornia = """ + The date is 2020-11-09T06:30 + """ + + try XCTAssertEqual(render(templateCalifornia, ["now": .int(now)]), expectedCalifornia) + } + func testDumpContext() throws { let data: [String: LeafData] = ["value": 12345] let template = """