-
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from vapor/mysql-date-conv
MySQL Date convenience functions
- Loading branch information
Showing
4 changed files
with
82 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import Foundation | ||
|
||
public enum MySQLDateError: Swift.Error { | ||
/** | ||
Date wasn't a proper DATETIME-formatted String. | ||
*/ | ||
case invalidDate | ||
} | ||
|
||
extension Date { | ||
/** | ||
Instantiates a `Date` from a MySQL DATETIME-formatted String. | ||
|
||
- Parameters: | ||
- mysql: yyyy-MM-dd HH:mm:ss | ||
*/ | ||
public init(mysql date: String) throws { | ||
guard let date = dateFormatter.date(from: date) else { | ||
throw MySQLDateError.invalidDate | ||
} | ||
|
||
self = date | ||
} | ||
|
||
/** | ||
A MySQL DATETIME formatted String. | ||
*/ | ||
public var mysql: String { | ||
return dateFormatter.string(from: self) | ||
} | ||
} | ||
|
||
// DateFormatter init is slow, need to reuse | ||
private var _df: DateFormatter? | ||
private var dateFormatter: DateFormatter { | ||
if let df = _df { | ||
return df | ||
} | ||
|
||
let df = DateFormatter() | ||
df.dateFormat = "yyyy-MM-dd HH:mm:ss" | ||
_df = df | ||
return df | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import XCTest | ||
import Foundation | ||
|
||
@testable import MySQL | ||
|
||
class DateTests: XCTestCase { | ||
static let allTests = [ | ||
("testMySQLDateInit", testMySQLDateInit), | ||
("testMySQLDateInitFailed", testMySQLDateInitFailed), | ||
] | ||
|
||
func testMySQLDateInit() { | ||
let mysqlDateString = "2003-09-15 10:05:00" | ||
|
||
do { | ||
_ = try Date(mysql: mysqlDateString) | ||
} catch { | ||
XCTFail("Init failed: \(error)") | ||
} | ||
} | ||
|
||
func testMySQLDateInitFailed() { | ||
let malformedMysqlDateString = "2003-09-110:05:00" | ||
|
||
do { | ||
_ = try Date(mysql: malformedMysqlDateString) | ||
XCTFail("Init should have thrown") | ||
} catch MySQLDateError.invalidDate { | ||
// success | ||
} catch { | ||
XCTFail("Expected `MySQLDateError.invalidDate` caught `\(error)`") | ||
} | ||
} | ||
} |
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