Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Date+Extension not public #23

Open
wants to merge 1 commit into
base: new
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 40 additions & 40 deletions Source/Extensions/Date+Extension.swift
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import Foundation

public extension Date {
extension Date {

/// User’s current calendar.
public var calendar: Calendar {
var calendar: Calendar {
return Calendar.current
}

/// Era.
public var era: Int {
var era: Int {
return calendar.component(.era, from: self)
}

/// Year.
public var year: Int {
var year: Int {
get {
return calendar.component(.year, from: self)
}
Expand All @@ -23,12 +23,12 @@ public extension Date {
}

/// Quarter.
public var quarter: Int {
var quarter: Int {
return calendar.component(.quarter, from: self)
}

/// Month.
public var month: Int {
var month: Int {
get {
return calendar.component(.month, from: self)
}
Expand All @@ -38,22 +38,22 @@ public extension Date {
}

/// Week of year.
public var weekOfYear: Int {
var weekOfYear: Int {
return calendar.component(.weekOfYear, from: self)
}

/// Week of month.
public var weekOfMonth: Int {
var weekOfMonth: Int {
return calendar.component(.weekOfMonth, from: self)
}

/// Weekday.
public var weekday: Int {
var weekday: Int {
return calendar.component(.weekday, from: self)
}

/// Day.
public var day: Int {
var day: Int {
get {
return calendar.component(.day, from: self)
}
Expand All @@ -63,7 +63,7 @@ public extension Date {
}

/// Hour.
public var hour: Int {
var hour: Int {
get {
return calendar.component(.hour, from: self)
}
Expand All @@ -73,7 +73,7 @@ public extension Date {
}

/// Minutes.
public var minute: Int {
var minute: Int {
get {
return calendar.component(.minute, from: self)
}
Expand All @@ -83,7 +83,7 @@ public extension Date {
}

/// Seconds.
public var second: Int {
var second: Int {
get {
return calendar.component(.second, from: self)
}
Expand All @@ -93,27 +93,27 @@ public extension Date {
}

/// Nanoseconds.
public var nanosecond: Int {
var nanosecond: Int {
return calendar.component(.nanosecond, from: self)
}

/// Check if date is in future.
public var isInFuture: Bool {
var isInFuture: Bool {
return self > Date()
}

/// Check if date is in past.
public var isInPast: Bool {
var isInPast: Bool {
return self < Date()
}

/// Check if date is in today.
public var isInToday: Bool {
var isInToday: Bool {
return self.day == Date().day && self.month == Date().month && self.year == Date().year
}

/// ISO8601 string of format (yyyy-MM-dd'T'HH:mm:ss.SSS) from date.
public var iso8601String: String {
var iso8601String: String {
// https://github.com/justinmakaila/NSDate-ISO-8601/blob/master/NSDateISO8601.swift
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
Expand All @@ -124,7 +124,7 @@ public extension Date {
}

/// Nearest five minutes to date.
public var nearestFiveMinutes: Date {
var nearestFiveMinutes: Date {
var components = Calendar.current.dateComponents([.year, .month , .day , .hour , .minute], from: self)
guard let min = components.minute else {
return self
Expand All @@ -138,7 +138,7 @@ public extension Date {
}

/// Nearest ten minutes to date.
public var nearestTenMinutes: Date {
var nearestTenMinutes: Date {
var components = Calendar.current.dateComponents([.year, .month , .day , .hour , .minute], from: self)
guard let min = components.minute else {
return self
Expand All @@ -152,7 +152,7 @@ public extension Date {
}

/// Nearest quarter to date.
public var nearestHourQuarter: Date {
var nearestHourQuarter: Date {
var components = Calendar.current.dateComponents([.year, .month , .day , .hour , .minute], from: self)
guard let min = components.minute else {
return self
Expand All @@ -166,7 +166,7 @@ public extension Date {
}

/// Nearest half hour to date.
public var nearestHalfHour: Date {
var nearestHalfHour: Date {
var components = Calendar.current.dateComponents([.year, .month , .day , .hour , .minute], from: self)
guard let min = components.minute else {
return self
Expand All @@ -180,27 +180,27 @@ public extension Date {
}

/// Time zone used by system.
public var timeZone: TimeZone {
var timeZone: TimeZone {
return self.calendar.timeZone
}

/// UNIX timestamp from date.
public var unixTimestamp: Double {
var unixTimestamp: Double {
return timeIntervalSince1970
}

}


// MARK: - Methods
public extension Date {
extension Date {

/// Add calendar component to date.
///
/// - Parameters:
/// - component: component type.
/// - value: multiples of compnenet to add.
public mutating func add(_ component: Calendar.Component, value: Int) {
mutating func add(_ component: Calendar.Component, value: Int) {
switch component {
case .second:
self = calendar.date(byAdding: .second, value: value, to: self) ?? self
Expand Down Expand Up @@ -241,7 +241,7 @@ public extension Date {
/// - component: component type.
/// - value: multiples of compnenets to add.
/// - Returns: original date + multiples of compnenet added.
public func adding(_ component: Calendar.Component, value: Int) -> Date {
func adding(_ component: Calendar.Component, value: Int) -> Date {
switch component {
case .second:
return calendar.date(byAdding: .second, value: value, to: self) ?? self
Expand Down Expand Up @@ -275,7 +275,7 @@ public extension Date {
/// - component: component type.
/// - value: new value of compnenet to change.
/// - Returns: original date + multiples of compnenets added.
public func changing(_ component: Calendar.Component, value: Int) -> Date {
func changing(_ component: Calendar.Component, value: Int) -> Date {
switch component {
case .second:
var date = self
Expand Down Expand Up @@ -316,7 +316,7 @@ public extension Date {
///
/// - Parameter component: calendar component to get date at the beginning of.
/// - Returns: date at the beginning of calendar component (if applicable).
public func beginning(of component: Calendar.Component) -> Date? {
func beginning(of component: Calendar.Component) -> Date? {
switch component {
case .second:
return calendar.date(from: calendar.dateComponents([.year, .month, .day, .hour, .minute, .second], from: self))
Expand Down Expand Up @@ -348,7 +348,7 @@ public extension Date {
///
/// - Parameter component: calendar component to get date at the end of.
/// - Returns: date at the end of calendar component (if applicable).
public func end(of component: Calendar.Component) -> Date? {
func end(of component: Calendar.Component) -> Date? {
switch component {
case .second:
var date = self.adding(.second, value: 1)
Expand Down Expand Up @@ -425,7 +425,7 @@ public extension Date {
///
/// - Parameter style: DateFormatter style (default is .medium)
/// - Returns: date and time string
public func dateTimeString(ofStyle style: DateFormatter.Style = .medium) -> String {
func dateTimeString(ofStyle style: DateFormatter.Style = .medium) -> String {
let dateFormatter = DateFormatter()
dateFormatter.timeStyle = style
dateFormatter.dateStyle = style
Expand All @@ -436,7 +436,7 @@ public extension Date {
///
/// - Parameter component: calendar componenet to check.
/// - Returns: true if date is in current given calendar component.
public func isInCurrent(_ component: Calendar.Component) -> Bool {
func isInCurrent(_ component: Calendar.Component) -> Bool {
switch component {
case .second:
return second == Date().second && minute == Date().minute && hour == Date().hour && day == Date().day
Expand Down Expand Up @@ -473,7 +473,7 @@ public extension Date {
}

/// Time string from date
public func timeString(ofStyle style: DateFormatter.Style = .medium) -> String {
func timeString(ofStyle style: DateFormatter.Style = .medium) -> String {
let dateFormatter = DateFormatter()
dateFormatter.timeStyle = style
dateFormatter.dateStyle = .none
Expand All @@ -484,7 +484,7 @@ public extension Date {


// MARK: - Initializers
public extension Date {
extension Date {

/// Create a new date form calendar components.
///
Expand All @@ -499,7 +499,7 @@ public extension Date {
/// - minute: Minute (default is current minute).
/// - second: Second (default is current second).
/// - nanosecond: Nanosecond (default is current nanosecond).
public init(
init(
calendar: Calendar? = Calendar.current,
timeZone: TimeZone? = TimeZone.current,
era: Int? = Date().era,
Expand Down Expand Up @@ -529,7 +529,7 @@ public extension Date {
/// Create date object from ISO8601 string.
///
/// - Parameter iso8601String: ISO8601 string of format (yyyy-MM-dd'T'HH:mm:ss.SSSZ).
public init(iso8601String: String) {
init(iso8601String: String) {
// https://github.com/justinmakaila/NSDate-ISO-8601/blob/master/NSDateISO8601.swift
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
Expand All @@ -541,15 +541,15 @@ public extension Date {
/// Create new date object from UNIX timestamp.
///
/// - Parameter unixTimestamp: UNIX timestamp.
public init(unixTimestamp: Double) {
init(unixTimestamp: Double) {
self.init(timeIntervalSince1970: unixTimestamp)
}

}

public extension Date {
extension Date {
/// SwiftRandom extension
public static func randomWithinDaysBeforeToday(_ days: Int) -> Date {
static func randomWithinDaysBeforeToday(_ days: Int) -> Date {
let today = Date()
let gregorian = Calendar(identifier: Calendar.Identifier.gregorian)

Expand All @@ -572,7 +572,7 @@ public extension Date {
}

/// SwiftRandom extension
public static func random() -> Date {
static func random() -> Date {
let randomTime = TimeInterval(arc4random_uniform(UInt32.max))
return Date(timeIntervalSince1970: randomTime)
}
Expand Down