Skip to content
This repository has been archived by the owner on Nov 16, 2020. It is now read-only.

Update DateFormat.swift #37

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Sources/TemplateKit/Tag/DateFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public final class DateFormat: TagRenderer {
public func render(tag: TagContext) throws -> Future<TemplateData> {
/// Require at least one parameter.
switch tag.parameters.count {
case 1, 2: break
default: throw tag.error(reason: "Invalid parameter count: \(tag.parameters.count). 1 or 2 required.")
case 1, 2, 3, 4: break
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be better expressed as 1...4

default: throw tag.error(reason: "Invalid parameter count: \(tag.parameters.count). 1 or 2 or 3 or 4 required.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be a bit less awkward to read as:

1, 2, 3, or 4 required

}

let formatter = DateFormatter()
Expand All @@ -24,6 +24,12 @@ public final class DateFormat: TagRenderer {
} else {
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
}
if tag.parameters.count > 2, let localeIdentifier = tag.parameters[2].string {
formatter.locale = Locale(identifier: localeIdentifier)
}
if tag.parameters.count > 3, let timeZoneAbbreviation = tag.parameters[3].string {
formatter.timeZone = TimeZone(abbreviation: timeZoneAbbreviation)
}

/// Return formatted date
return Future.map(on: tag) { .string(formatter.string(from: date)) }
Expand Down