Created
April 15, 2017 02:18
-
-
Save anthonycastelli/2048574445e53299daec8633bbca9c8c to your computer and use it in GitHub Desktop.
Fluent+Date.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Fluent+Date.swift | |
// | |
// | |
// Created by Anthony Castelli on 12/10/16. | |
// | |
// | |
import Foundation | |
import Fluent | |
import Vapor | |
extension Node { | |
public var date: Date? { | |
do { | |
return try Date(node: self) | |
} catch { | |
return nil | |
} | |
} | |
} | |
extension Date: NodeConvertible { | |
public func makeNode(context: Context = EmptyNode) throws -> Node { | |
// return .string(dateFormatterMySQL.string(from: self)) | |
return .number(.double(self.timeIntervalSince1970)) | |
} | |
public init(node: Node, in context: Context) throws { | |
switch node { | |
case .number(let number): | |
self = Date(timeIntervalSince1970: number.double) | |
case .string(let dateString): | |
if let dateMySQL = dateFormatterMySQL.date(from: dateString) { | |
self = dateMySQL | |
} else if let dateISO8601 = dateFormatterISO8601.date(from: dateString) { | |
self = dateISO8601 | |
} else { | |
throw NodeError.unableToConvert( | |
node: node, | |
expected: "MySQL DATETIME or ISO8601 formatted date." | |
) | |
} | |
default: | |
throw NodeError.unableToConvert( | |
node: node, | |
expected: "\(String.self), \(Int.self) or \(Double.self))" | |
) | |
} | |
} | |
} | |
// DateFormatter init is slow, need to reuse. Reusing two DateFormatters is | |
// significantly faster than toggling the formatter's `dateFormat` property. | |
// On Linux, toggling is actually slower than initializing DateFormatter. | |
private var _dfMySQL: DateFormatter? | |
private var dateFormatterMySQL: DateFormatter { | |
if let df = _dfMySQL { | |
return df | |
} | |
let df = DateFormatter() | |
df.timeZone = TimeZone(abbreviation: "UTC") | |
df.dateFormat = "yyyy-MM-dd HH:mm:ss" | |
_dfMySQL = df | |
return df | |
} | |
private var _dfISO8601: DateFormatter? | |
private var dateFormatterISO8601: DateFormatter { | |
if let df = _dfISO8601 { | |
return df | |
} | |
let df = DateFormatter() | |
df.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ" | |
_dfISO8601 = df | |
return df | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When you create the database in the
prepare
method, define a type as a.double("name"