Skip to content

Commit

Permalink
Merge pull request #76 from vapor/mysql-date-conv
Browse files Browse the repository at this point in the history
MySQL Date convenience functions
  • Loading branch information
BrettRToomey authored Jan 17, 2017
2 parents 54b9a04 + 9203a6f commit be6cfa6
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 2 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# MySQL for Swift

![Swift](http://img.shields.io/badge/swift-3.0-brightgreen.svg)
![Swift](https://camo.githubusercontent.com/0727f3687a1e263cac101c5387df41048641339c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f53776966742d332e302d6f72616e67652e7376673f7374796c653d666c6174)
[![Swift](http://img.shields.io/badge/swift-3.0-brightgreen.svg)](https://swift.org)
[![Build Status](https://travis-ci.org/vapor/mysql.svg?branch=master)](https://travis-ci.org/vapor/mysql)

A Swift wrapper for MySQL.
Expand Down
44 changes: 44 additions & 0 deletions Sources/MySQL/MySQL+Date.swift
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
}
34 changes: 34 additions & 0 deletions Tests/MySQLTests/DateTests.swift
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)`")
}
}
}
3 changes: 3 additions & 0 deletions Tests/MySQLTests/MySQLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ class MySQLTests: XCTestCase {
("testSelectVersion", testSelectVersion),
("testTables", testTables),
("testParameterization", testParameterization),
("testTimestamps", testTimestamps),
("testSpam", testSpam),
("testError", testError),
]

var mysql: MySQL.Database!
Expand Down

0 comments on commit be6cfa6

Please sign in to comment.