-
-
Save asaday/30e960981676708168b1789a5bc0eb00 to your computer and use it in GitHub Desktop.
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
public extension Date { | |
static func dataformatter() -> DateFormatter { | |
let f = DateFormatter() | |
f.locale = Locale(identifier: "en_US_POSIX") | |
f.timeZone = TimeZone.autoupdatingCurrent | |
f.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ" // ISO8601 | |
return f | |
} | |
static var isoDataformatter: DateFormatter { return Date.dataformatter() } | |
init?(string: String, format: String? = nil) { | |
let f = Date.dataformatter() | |
var formats = [ | |
"yyyy-MM-dd'T'HH:mm:ssZ", // 1999-12-31T12:59:59+0900 1999-12-31T12:59:59Z 1999-12-31T12:59:59+09:00 | |
"yyyy-MM-dd'T'HH:mm:ss", // 1999-12-31T12:59:59 | |
"yyyy-MM-dd'T'HH:mm:ss.S", // 1999-12-31T12:59:59.999 | |
"yyyy-MM-dd'T'HH:mm:ss.SZ", // 1999-12-31T12:59:59.999+0900 | |
"yyyy-MM-dd HH:mm:ss", // 1999-12-31 12:59:59 | |
"yyyy-MM-dd HH:mm", // 1999-12-31 12:56 | |
"EEE, dd MM yyyy HH:mm:ss", // Tue, 31 Mar 2015 10:52:33 RFC822 | |
"EEE, dd MM yyyy HH:mm:ss Z", // Tue, 31 Mar 2015 10:52:33 +0900 RFC822 | |
"yyyy-MM-dd"]; // yyyy-MM-dd also yyyy/MM/dd | |
if let af = format { formats = [af] } | |
for fmt in formats { | |
f.dateFormat = fmt | |
if let d = f.date(from: string) { | |
self.init(timeInterval: 0, since: d) | |
return | |
} | |
} | |
return nil | |
} | |
init?(year: Int, month: Int, day: Int, hour: Int, minute: Int, second: Int) { | |
guard let d = (Calendar.current as NSCalendar).date(era: 1, year: year, month: month, day: day, hour: hour, minute: minute, second: second, nanosecond: 0) else { return nil } | |
self.init(timeInterval: 0, since: d) | |
} | |
init(unix: NSInteger) { self.init(timeIntervalSince1970: TimeInterval(unix)) } | |
var string: String { return Date.isoDataformatter.string(from: self) } | |
func format(_ format: String) -> String { | |
let f = Date.dataformatter() | |
f.dateFormat = format | |
return f.string(from: self) | |
} | |
var year: Int { return Calendar.current.component(Calendar.Component.year, from: self) } | |
var month: Int { return Calendar.current.component(Calendar.Component.month, from: self) } | |
var day: Int { return Calendar.current.component(Calendar.Component.day, from: self) } | |
var hour: Int { return Calendar.current.component(Calendar.Component.hour, from: self) } | |
var miniute: Int { return Calendar.current.component(Calendar.Component.minute, from: self) } | |
var second: Int { return Calendar.current.component(Calendar.Component.second, from: self) } | |
var weekday: Int { return Calendar.current.component(Calendar.Component.weekday, from: self) } | |
var unix: Int { return Int(self.timeIntervalSince1970) } | |
var lapTime: TimeInterval { return -timeIntervalSince(Date()) } | |
} | |
public extension TimeInterval { | |
var msec: String { return String(format: "%0.2fms", self * 1000) } | |
} | |
public func == (lhs: Date, rhs: Date) -> Bool { return lhs.compare(rhs) == .orderedSame } | |
public func < (lhs: Date, rhs: Date) -> Bool { return lhs.compare(rhs) == .orderedAscending } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment