From 51d5ba917d6d02e082af65696480cbc537b9bc1c Mon Sep 17 00:00:00 2001 From: Eugene Mozharovsky Date: Sat, 5 Dec 2015 18:38:04 +0300 Subject: [PATCH] Updated sources --- CVCalendarKit/CVCalendarKitExtensions.swift | 95 +++++++++++++++------ CVCalendarKit/CVCalendarKitOperators.swift | 38 ++++++--- 2 files changed, 96 insertions(+), 37 deletions(-) diff --git a/CVCalendarKit/CVCalendarKitExtensions.swift b/CVCalendarKit/CVCalendarKitExtensions.swift index 5ee47d4..2e1d4b8 100644 --- a/CVCalendarKit/CVCalendarKitExtensions.swift +++ b/CVCalendarKit/CVCalendarKitExtensions.swift @@ -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" @@ -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) @@ -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() @@ -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. */ @@ -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) @@ -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) @@ -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) @@ -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 @@ -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 @@ -204,8 +249,8 @@ public extension String { } /** - - */ + + */ var localized: String { return NSLocalizedString(self, comment: self) } diff --git a/CVCalendarKit/CVCalendarKitOperators.swift b/CVCalendarKit/CVCalendarKitOperators.swift index ed86d3c..564d972 100644 --- a/CVCalendarKit/CVCalendarKitOperators.swift +++ b/CVCalendarKit/CVCalendarKitOperators.swift @@ -19,19 +19,26 @@ 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. */ @@ -39,6 +46,8 @@ public struct Offset { var year: Int var month: Int var day: Int + var hour: Int + var minute: Int } // MARK: - Helper functions @@ -48,9 +57,9 @@ 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) @@ -58,29 +67,34 @@ private func dateWithOffset(date: NSDate) -> DateOffset { 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 @@ -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