Skip to content

Commit

Permalink
Merge pull request #2 from Killectro/swift-2.0
Browse files Browse the repository at this point in the history
Swift 2.0
  • Loading branch information
mozharovsky committed Oct 8, 2015
2 parents 2f27a7a + 7e49e58 commit e9efa6f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 47 deletions.
51 changes: 25 additions & 26 deletions CVCalendarKit/CVCalendarKitExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public enum Weekday: Int {
func stringValue() -> String {
switch self {
case .Sunday: return "Sunday".localized
case .Sunday: return "Sunday".localized
case .Monday: return "Monday".localized
case .Tuesday: return "Tuesday".localized
case .Wednesday: return "Wednesday".localized
Expand Down Expand Up @@ -51,23 +50,23 @@ public enum DateFormat: String {
case DDDDMMMMYYYY = "dddd-MMMM-yyyy"
}

private let YearUnit = NSCalendarUnit.CalendarUnitYear
private let MonthUnit = NSCalendarUnit.CalendarUnitMonth
private let WeekUnit = NSCalendarUnit.CalendarUnitWeekOfMonth
private let WeekOfYearUnit = NSCalendarUnit.CalendarUnitWeekOfYear
private let WeekdayUnit = NSCalendarUnit.CalendarUnitWeekday
private let DayUnit = NSCalendarUnit.CalendarUnitDay
private let HourUnit = NSCalendarUnit.CalendarUnitHour
private let MinuteUnit = NSCalendarUnit.CalendarUnitMinute
private let SecondUnit = NSCalendarUnit.CalendarUnitSecond
private let AllUnits = YearUnit | MonthUnit | WeekUnit | WeekOfYearUnit | WeekdayUnit | DayUnit | HourUnit | MinuteUnit | DayUnit
private let YearUnit = NSCalendarUnit.Year
private let MonthUnit = NSCalendarUnit.Month
private let WeekUnit = NSCalendarUnit.WeekOfMonth
private let WeekOfYearUnit = NSCalendarUnit.WeekOfYear
private let WeekdayUnit = NSCalendarUnit.Weekday
private let DayUnit = NSCalendarUnit.Day
private let HourUnit = NSCalendarUnit.Hour
private let MinuteUnit = NSCalendarUnit.Minute
private let SecondUnit = NSCalendarUnit.Second
private let AllUnits = YearUnit.union(MonthUnit).union(WeekUnit).union(WeekOfYearUnit).union(WeekdayUnit).union(DayUnit).union(HourUnit).union(MinuteUnit).union(DayUnit)

public extension NSCalendar {
/**
Returns the NSDateComponents instance for all main units.

:param: Date The date for components construction.
:returns: The NSDateComponents instance for all main units.
- parameter Date: The date for components construction.
- returns: The NSDateComponents instance for all main units.
*/
func allComponentsFromDate(date: NSDate) -> NSDateComponents {
return components(AllUnits, fromDate: date)
Expand All @@ -81,7 +80,7 @@ public extension NSDate {
/**
Calculates the date values.

:returns: A tuple with date year, month and day values.
- returns: A tuple with date year, month and day values.
*/
private func dateRange() -> DateRange {
let calendar = NSCalendar.currentCalendar()
Expand Down Expand Up @@ -129,7 +128,7 @@ public extension NSDate {
/**
Returns the first date in the current date's month.

:returns: The first date in the current date's month.
- returns: The first date in the current date's month.
*/
func firstMonthDate() -> NSDate {
return (self.day == 1)
Expand All @@ -138,7 +137,7 @@ public extension NSDate {
/**
Returns the last date in the current date's month.

:returns: The las date in the current date's month.
- returns: The las date in the current date's month.
*/
func lastMonthDate() -> NSDate {
return ((firstMonthDate().month + 1).day - 1)
Expand All @@ -147,7 +146,7 @@ public extension NSDate {
/**
Returns the first date in the current date's year.

:returns: The first date in the current date's year.
- returns: The first date in the current date's year.
*/
func firstYearDate() -> NSDate {
return ((NSDate().month == 1).day == 1)
Expand All @@ -156,7 +155,7 @@ public extension NSDate {
/**
Returns the last date in the current date's year.

:returns: The last date in the current date's year.
- returns: The last date in the current date's year.
*/
func lastYearDate() -> NSDate {
return (((firstYearDate().month == 12).month + 1).day - 1)
Expand All @@ -165,12 +164,12 @@ public extension NSDate {
/**
Returns a date description string with the given locale and format.

:param: locale The locale for converting the date.
:param: format String format for the converted date.
:param: style String style for the converted date.
:returns: A date description string with the given locale and format.
- parameter locale: The locale for converting the date.
- parameter format: String format for the converted date.
- parameter style: String style for the converted date.
- returns: A date description string with the given locale and format.
*/
func descriptionWithLocale(_ locale: NSLocale? = nil, format: DateFormat = .YYMMDD, style: NSDateFormatterStyle?) -> String {
func descriptionWithLocale(locale: NSLocale? = nil, format: DateFormat = .YYMMDD, style: NSDateFormatterStyle?) -> String {
let formatter = NSDateFormatter()
formatter.dateFormat = format.rawValue

Expand All @@ -190,9 +189,9 @@ public extension String {
/**
Returns an optional associated with date from the given string and format.

:param: format Date format used for date conversion.
:param: style Date style for date conversion.
:returns: Either an NSDate instance or nil if a String can't be converted.
- parameter format: Date format used for date conversion.
- parameter style: Date style for date conversion.
- returns: Either an NSDate instance or nil if a String can't be converted.
*/
func date(format: DateFormat, style: NSDateFormatterStyle? = .LongStyle) -> NSDate? {
let formatter = NSDateFormatter()
Expand Down
42 changes: 21 additions & 21 deletions CVCalendarKit/CVCalendarKitOperators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public enum DateUnit {
case Day(NSDate, Int)

/**
:returns: An associated value with a particular case.
- returns: An associated value with a particular case.
*/
public func value() -> Int {
switch self {
Expand All @@ -48,9 +48,9 @@ private typealias DateOffset = (Offset, DateOperation) -> NSDate
/**
Constructs a date with the offset from the source date.

:param: date The given date for applying the offset.
- parameter date: The given date for applying the offset.

:returns: A function for getting the date from the offset and the assignment operation.
- returns: A function for getting the date from the offset and the assignment operation.
*/
private func dateWithOffset(date: NSDate) -> DateOffset {
let comps = NSCalendar.currentCalendar().allComponentsFromDate(date)
Expand All @@ -68,18 +68,18 @@ private typealias DateOperation = (Int, Int) -> (Int)
/**
A bridge between construction function and the given options.

:param: dateUnit A date unit providing the necessary data.
:param: offset An offset for
- parameter dateUnit: A date unit providing the necessary data.
- parameter offset: An offset for
*/
private func dateUnitOffset(dateUnit: DateUnit, offset: Int, operation: DateOperation) -> NSDate {
let result: NSDate

switch dateUnit {
case .Year(let date, let value):
case .Year(let date, _):
result = dateWithOffset(date)(Offset(year: offset, month: 0, day: 0), operation)
case .Month(let date, let value):
case .Month(let date, _):
result = dateWithOffset(date)(Offset(year: 0, month: offset, day: 0), operation)
case .Day(let date, let value):
case .Day(let date, _):
result = dateWithOffset(date)(Offset(year: 0, month: 0, day: offset), operation)
}

Expand All @@ -93,9 +93,9 @@ private typealias ComparisonResult = (NSDate, NSDate) -> Bool
/**
Compares dates via return closure.

:param: operation Comparison operation.
:param: resultMerge The way of merging the results.
:returns: A comparison function.
- parameter operation: Comparison operation.
- parameter resultMerge: The way of merging the results.
- returns: A comparison function.
*/
private func compareWithOperation(operation: ComparisonOperation, resultMerge: ResultMerge) -> ComparisonResult {
return { dateA, dateB in
Expand All @@ -110,45 +110,45 @@ private func compareWithOperation(operation: ComparisonOperation, resultMerge: R
// MARK: - DateUnit operators overload

public func + (lhs: DateUnit, rhs: Int) -> NSDate {
return dateUnitOffset(lhs, rhs, { x, y in x + y })
return dateUnitOffset(lhs, offset: rhs, operation: { x, y in x + y })
}

public func - (lhs: DateUnit, rhs: Int) -> NSDate {
return dateUnitOffset(lhs, rhs, { x, y in x - y })
return dateUnitOffset(lhs, offset: rhs, operation: { x, y in x - y })
}

public func * (lhs: DateUnit, rhs: Int) -> NSDate {
return dateUnitOffset(lhs, rhs, { x, y in x * y })
return dateUnitOffset(lhs, offset: rhs, operation: { x, y in x * y })
}

public func / (lhs: DateUnit, rhs: Int) -> NSDate {
return dateUnitOffset(lhs, rhs, { x, y in x / y })
return dateUnitOffset(lhs, offset: rhs, operation: { x, y in x / y })
}

public func == (lhs: DateUnit, rhs: Int) -> NSDate {
return dateUnitOffset(lhs, rhs, { _, y in y })
return dateUnitOffset(lhs, offset: rhs, operation: { _, y in y })
}

// MARK: - NSDate operators overload

public func == (lhs: NSDate, rhs: NSDate) -> Bool {
return compareWithOperation({ $0 == $1 }, { $0 && $1 && $2 })(lhs, rhs)
return compareWithOperation({ $0 == $1 }, resultMerge: { $0 && $1 && $2 })(lhs, rhs)
}

public func > (lhs: NSDate, rhs: NSDate) -> Bool {
return compareWithOperation({ $0 > $1 }, { $0 || $1 || $2 })(lhs, rhs)
return compareWithOperation({ $0 > $1 }, resultMerge: { $0 || $1 || $2 })(lhs, rhs)
}

public func >= (lhs: NSDate, rhs: NSDate) -> Bool {
return compareWithOperation({ $0 > $1 || lhs == rhs }, { $0 || $1 || $2 })(lhs, rhs)
return compareWithOperation({ $0 > $1 || lhs == rhs }, resultMerge: { $0 || $1 || $2 })(lhs, rhs)
}

public func < (lhs: NSDate, rhs: NSDate) -> Bool {
return compareWithOperation({ $0 < $1 }, { $0 || $1 || $2 })(lhs, rhs)
return compareWithOperation({ $0 < $1 }, resultMerge: { $0 || $1 || $2 })(lhs, rhs)
}

public func <= (lhs: NSDate, rhs: NSDate) -> Bool {
return compareWithOperation({ $0 < $1 || lhs == rhs }, { $0 || $1 || $2 })(lhs, rhs)
return compareWithOperation({ $0 < $1 || lhs == rhs }, resultMerge: { $0 || $1 || $2 })(lhs, rhs)
}

public func != (lhs: NSDate, rhs: NSDate) -> Bool {
Expand Down

0 comments on commit e9efa6f

Please sign in to comment.