Skip to content

Commit

Permalink
Updated sources
Browse files Browse the repository at this point in the history
  • Loading branch information
mozharovsky committed Dec 5, 2015
1 parent e9efa6f commit 51d5ba9
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 37 deletions.
95 changes: 70 additions & 25 deletions CVCalendarKit/CVCalendarKitExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public enum Weekday: Int {
}

/**
Date format for string description and date construction.
*/
Date format for string description and date construction.
*/
public enum DateFormat: String {
case YYMMDD = "yy-MM-dd"
case YYYYMMDD = "yyyy-MM-dd"
Expand All @@ -59,14 +59,14 @@ 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)
private let AllUnits: NSCalendarUnit = [YearUnit , MonthUnit , WeekUnit , WeekOfYearUnit , WeekdayUnit , DayUnit, HourUnit, MinuteUnit, SecondUnit]

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

- parameter Date: The date for components construction.
- returns: The NSDateComponents instance for all main units.
:param: 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 @@ -80,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 All @@ -89,6 +89,31 @@ public extension NSDate {
return (comps.year, comps.month, comps.day)
}

/**
Calculates the specific date values.

:returns: A tuple with date hours and minutes.
*/
private typealias DateSpecificRange = (hours: Int, minutes: Int)
private func dateSpecificRange() -> DateSpecificRange {
let calendar = NSCalendar.currentCalendar()
let comps = calendar.allComponentsFromDate(self)

return (comps.hour, comps.minute)
}

var hour: DateUnit {
get {
return .Hour(self, dateSpecificRange().hours)
}
}

var minute: DateUnit {
get {
return .Minute(self, dateSpecificRange().minutes)
}
}

/**
Current date weekday.
*/
Expand Down Expand Up @@ -128,7 +153,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 @@ -137,7 +162,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 @@ -146,7 +171,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 @@ -155,20 +180,40 @@ 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)
}

/**
Returns a date description string with the given locale and format.
convenience init?(date: NSDate, hour: Int, minute: Int) {
let calendar = NSCalendar.currentCalendar()
let comps = calendar.components([.Hour , .Minute], fromDate: date)
comps.hour = 0
comps.minute = 0

let first = calendar.dateFromComponents(comps)!

let compss = calendar.components([.Hour , .Minute], fromDate: first)
compss.hour = hour
compss.minute = minute

if let result = calendar.dateFromComponents(compss) {
self.init(timeIntervalSince1970: result.timeIntervalSince1970)
} else {
self.init()
return nil
}
}

- 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.
*/
/**
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 {
let formatter = NSDateFormatter()
formatter.dateFormat = format.rawValue
Expand All @@ -187,12 +232,12 @@ public extension NSDate {

public extension String {
/**
Returns an optional associated with date from the given string and format.

- 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.
*/
Returns an optional associated with date from the given string and format.
- 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()
formatter.dateFormat = format.rawValue
Expand All @@ -204,8 +249,8 @@ public extension String {
}

/**

*/
*/
var localized: String {
return NSLocalizedString(self, comment: self)
}
Expand Down
38 changes: 26 additions & 12 deletions CVCalendarKit/CVCalendarKitOperators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,35 @@ public enum DateUnit {
case Year(NSDate, Int)
case Month(NSDate, Int)
case Day(NSDate, Int)
case Hour(NSDate, Int)
case Minute(NSDate, Int)

/**
- returns: An associated value with a particular case.
:returns: An associated value with a particular case.
*/
public func value() -> Int {
switch self {
case .Year(_, let x): return x
case .Month(_, let x): return x
case .Day(_, let x): return x
case .Hour(_, let x): return x
case .Minute(_, let x): return x
}
}
}

/**
A structure for marking an offset for a date. Used for date contruction.
*/
/**
A structure for marking an offset for a date. Used for date contruction.
*/
public struct Offset {
var year: Int
var month: Int
var day: Int
var hour: Int
var minute: Int
}

// MARK: - Helper functions
Expand All @@ -48,39 +57,44 @@ private typealias DateOffset = (Offset, DateOperation) -> NSDate
/**
Constructs a date with the offset from the source date.

- parameter date: The given date for applying the offset.
:param: 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)
return { offset, operation in
comps.year = offset.year == 0 ? comps.year : operation(comps.year, offset.year)
comps.month = offset.month == 0 ? comps.month : operation(comps.month, offset.month)
comps.day = offset.day == 0 ? comps.day : operation(comps.day, offset.day)
comps.hour = offset.hour == 0 ? comps.hour : operation(comps.hour, offset.hour)
comps.minute = offset.minute == 0 ? comps.minute : operation(comps.minute, offset.minute)

return NSCalendar.currentCalendar().dateFromComponents(comps)!
}
}

private typealias DateOperation = (Int, Int) -> (Int)

/**
A bridge between construction function and the given options.

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

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

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

- parameter operation: Comparison operation.
- parameter resultMerge: The way of merging the results.
- returns: A comparison function.
:param: operation Comparison operation.
:param: resultMerge The way of merging the results.
:returns: A comparison function.
*/
private func compareWithOperation(operation: ComparisonOperation, resultMerge: ResultMerge) -> ComparisonResult {
return { dateA, dateB in
Expand Down

0 comments on commit 51d5ba9

Please sign in to comment.