Skip to content

Commit

Permalink
Add support for time zones in DateTag.
Browse files Browse the repository at this point in the history
  • Loading branch information
fpseverino committed Jul 1, 2024
1 parent 547e48c commit 0f1d4be
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Sources/LeafKit/LeafSyntax/LeafTag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
24 changes: 24 additions & 0 deletions Tests/LeafKitTests/TagTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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-09T07: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-09T04:30
"""

try XCTAssertEqual(render(templateCalifornia, ["now": .int(now)]), expectedCalifornia)
}

func testDumpContext() throws {
let data: [String: LeafData] = ["value": 12345]
let template = """
Expand Down

0 comments on commit 0f1d4be

Please sign in to comment.