-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathFormatterController.swift
73 lines (61 loc) · 2.04 KB
/
FormatterController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import Vapor
import Leaf
struct FormatterController: RouteCollection {
struct DateFormatExample: Codable {
let format: String
let value: String
}
struct ViewData: Content {
let examples: [DateFormatExample]
let locales: [String]
let defaultLocale: String
let inputDate: String
let exampleFormat: String
let exampleResult: String
}
func boot(routes: RoutesBuilder) {
routes.get("viewData", use: renderViewData)
}
func renderViewData(_ req: Request) async throws -> ViewData {
let date = Date()
let formatter = DateFormatter()
formatter.locale = Locale(identifier: defaultLocaleIdentifier)
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm"
let inputDate = formatter.string(from: date)
let examples = examples(for: date, formatter: formatter)
let viewData = ViewData(
examples: examples,
locales: buildLocales(),
defaultLocale: defaultLocaleIdentifier,
inputDate: inputDate,
exampleFormat: examples.first?.format ?? "MMM yyyy",
exampleResult: examples.first?.value ?? ""
)
return viewData
}
private let defaultLocaleIdentifier = "en_US_POSIX"
private func buildLocales() -> [String] {
Locale.availableIdentifiers.sorted()
}
private func examples(for date: Date, formatter: DateFormatter) -> [DateFormatExample] {
exampleFormats().map { format in
formatter.dateFormat = format
let value = formatter.string(from: date)
return DateFormatExample(format: format, value: value)
}
}
private func exampleFormats() -> [String] {
[
"EEEE, MMM d, yyyy",
"MM/dd/yyyy",
"MM-dd-yyyy HH:mm",
"MMM d, h:mm a",
"MMMM yyyy",
"MMM d, yyyy",
"E, d MMM yyyy HH:mm:ss Z",
"yyyy-MM-dd'T'HH:mm:ssZ",
"dd.MM.yy",
"HH:mm:ss.SSS"
]
}
}