Skip to content

Commit

Permalink
Fix for IOS 8 Beta 5. Revised sample project. Components are no longe…
Browse files Browse the repository at this point in the history
…r functions.
  • Loading branch information
melvitax committed Aug 5, 2014
1 parent 7528f50 commit edf7e2f
Show file tree
Hide file tree
Showing 5 changed files with 272 additions and 203 deletions.
2 changes: 1 addition & 1 deletion AF+Date+Helper.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'AF+Date+Helper'
s.version = '1.01'
s.version = '1.02'
s.platform = :ios
s.license = 'MIT'
s.summary = 'Convenience extension for NSDate in Swift.'
Expand Down
107 changes: 51 additions & 56 deletions AF+Date+Helper/AF+Date+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,57 +17,53 @@ enum DateFormat {
extension NSDate {

// For private use
class func minuteInSeconds() -> Double { return 60 }
class func hourInSeconds() -> Double { return 3600 }
class func dayInSeconds() -> Double { return 86400 }
class func weekInSeconds() -> Double { return 604800 }
class func yearInSeconds() -> Double { return 31556926 }
private class func minuteInSeconds() -> Double { return 60 }
private class func hourInSeconds() -> Double { return 3600 }
private class func dayInSeconds() -> Double { return 86400 }
private class func weekInSeconds() -> Double { return 604800 }
private class func yearInSeconds() -> Double { return 31556926 }

// #pragma mark - Components
class func componentFlags() -> NSCalendarUnit { return .YearCalendarUnit | .MonthCalendarUnit | .DayCalendarUnit | .WeekCalendarUnit | .HourCalendarUnit | .MinuteCalendarUnit | .SecondCalendarUnit | .WeekdayCalendarUnit | .WeekdayOrdinalCalendarUnit | .CalendarUnitWeekOfYear }
private class func componentFlags() -> NSCalendarUnit { return .YearCalendarUnit | .MonthCalendarUnit | .DayCalendarUnit | .WeekCalendarUnit | .HourCalendarUnit | .MinuteCalendarUnit | .SecondCalendarUnit | .WeekdayCalendarUnit | .WeekdayOrdinalCalendarUnit | .CalendarUnitWeekOfYear }

class func components(#fromDate: NSDate) -> NSDateComponents! {
private class func components(#fromDate: NSDate) -> NSDateComponents! {
return NSCalendar.currentCalendar().components(NSDate.componentFlags(), fromDate: fromDate)
}

func components() -> NSDateComponents {
private func components() -> NSDateComponents {
return NSDate.components(fromDate: self)
}

func componentsWithOffsetFromDate(date: NSDate) -> NSDateComponents
{
return NSCalendar.currentCalendar().components(NSDate.componentFlags(), fromDate: date, toDate: self, options: nil)
}

//#pragma mark - Date from String

// Date from string
convenience init(fromString string: String, format:DateFormat)
{
if string.isEmpty {
self.init()
return
}

let string = string as NSString

switch format {

case .DotNet:

// Expects "/Date(1268123281843)/"
let startIndex = Int(string.bridgeToObjectiveC().rangeOfString("(").location) + 1
let endIndex = Int(string.bridgeToObjectiveC().rangeOfString(")").location)
let startIndex = string.rangeOfString("(").location + 1
let endIndex = string.rangeOfString(")").location
let range = NSRange(location: startIndex, length: endIndex-startIndex)
let milliseconds = string.bridgeToObjectiveC().substringWithRange(range).bridgeToObjectiveC().longLongValue
let milliseconds = (string.substringWithRange(range) as NSString).longLongValue
let interval = NSTimeInterval(milliseconds / 1000)
self.init(timeIntervalSince1970: interval)

case .ISO8601:

var s = string
var s = string
if string.hasSuffix(" 00:00") {
s = s.bridgeToObjectiveC().substringToIndex(countElements(s)-6) + "GMT"
s = s.substringToIndex(s.length-6) + "GMT"
} else if string.hasSuffix("Z") {
s = s.bridgeToObjectiveC().substringToIndex(countElements(s)-1) + "GMT"
s = s.substringToIndex(s.length-1) + "GMT"
}
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZ"
Expand All @@ -78,7 +74,7 @@ extension NSDate {

var s = string
if string.hasSuffix("Z") {
s = s.bridgeToObjectiveC().substringToIndex(countElements(s)-1) + "GMT"
s = s.substringToIndex(s.length-1) + "GMT"
}
let formatter = NSDateFormatter()
formatter.dateFormat = "EEE, d MMM yyyy HH:mm:ss ZZZ"
Expand All @@ -89,7 +85,7 @@ extension NSDate {

var s = string
if string.hasSuffix("Z") {
s = s.bridgeToObjectiveC().substringToIndex(countElements(s)-1) + "GMT"
s = s.substringToIndex(s.length-1) + "GMT"
}
let formatter = NSDateFormatter()
formatter.dateFormat = "d MMM yyyy HH:mm:ss ZZZ"
Expand Down Expand Up @@ -118,17 +114,17 @@ extension NSDate {

func isToday() -> Bool
{
return self.isEqualToDate(NSDate())
return self.isEqualToDateIgnoringTime(NSDate())
}

func isTomorrow() -> Bool
{
return self.isEqualToDate(NSDate().dateByAddingDays(1))
return self.isEqualToDateIgnoringTime(NSDate().dateByAddingDays(1))
}

func isYesterday() -> Bool
{
return self.isEqualToDate(NSDate().dateBySubtractingDays(1))
return self.isEqualToDateIgnoringTime(NSDate().dateBySubtractingDays(1))
}

func isSameWeekAsDate(date: NSDate) -> Bool
Expand All @@ -155,7 +151,7 @@ extension NSDate {
return self.isSameYearAsDate(date)
}

func isLasttWeek() -> Bool
func isLastWeek() -> Bool
{
let interval: NSTimeInterval = NSDate().timeIntervalSinceReferenceDate - NSDate.weekInSeconds()
let date = NSDate(timeIntervalSinceReferenceDate: interval)
Expand Down Expand Up @@ -319,46 +315,45 @@ extension NSDate {

//#pragma mark - Decomposing Dates

func nearestHour() -> Int
{
var nearestHour: Int {
let halfHour = NSDate.minuteInSeconds() * 30
var interval = self.timeIntervalSinceReferenceDate
if self.seconds() < 30 {
if self.seconds < 30 {
interval -= halfHour
} else {
interval += halfHour
}
let date = NSDate(timeIntervalSinceReferenceDate: interval)
return date.hour()
}

func year() -> Int{ return self.components().year }
func month() -> Int { return self.components().month }
func week() -> Int { return self.components().weekOfYear }
func day() -> Int { return self.components().day }
func hour() -> Int { return self.components().hour }
func minute() -> Int { return self.components().minute }
func seconds() -> Int { return self.components().second }
func weekday() -> Int { return self.components().weekday }
func nthWeekday() -> Int { return self.components().weekdayOrdinal } //// e.g. 2nd Tuesday of the month is 2
func monthDays() -> Int { return NSCalendar.currentCalendar().rangeOfUnit(.DayCalendarUnit, inUnit: .MonthCalendarUnit, forDate: self).length }
func firstDayOfWeek() -> Int {
return date.hour
}

var year: Int { return self.components().year }
var month: Int { return self.components().month }
var week: Int { return self.components().weekOfYear }
var day: Int { return self.components().day }
var hour: Int { return self.components().hour }
var minute: Int { return self.components().minute }
var seconds: Int { return self.components().second }
var weekday: Int { return self.components().weekday }
var nthWeekday: Int { return self.components().weekdayOrdinal } //// e.g. 2nd Tuesday of the month is 2
var monthDays: Int { return NSCalendar.currentCalendar().rangeOfUnit(.DayCalendarUnit, inUnit: .MonthCalendarUnit, forDate: self).length }
var firstDayOfWeek: Int {
let distanceToStartOfWeek = NSDate.dayInSeconds() * Double(self.components().weekday - 1)
let interval: NSTimeInterval = self.timeIntervalSinceReferenceDate - distanceToStartOfWeek
return NSDate(timeIntervalSinceReferenceDate: interval).day()
return NSDate(timeIntervalSinceReferenceDate: interval).day
}
func lastDayOfWeek() -> Int {
var lastDayOfWeek: Int {
let distanceToStartOfWeek = NSDate.dayInSeconds() * Double(self.components().weekday - 1)
let distanceToEndOfWeek = NSDate.dayInSeconds() * Double(7)
let interval: NSTimeInterval = self.timeIntervalSinceReferenceDate - distanceToStartOfWeek + distanceToEndOfWeek
return NSDate(timeIntervalSinceReferenceDate: interval).day()
return NSDate(timeIntervalSinceReferenceDate: interval).day
}
func isWeekday() -> Bool {
return !self.isWeekend()
var isWeekday: Bool {
return !self.isWeekend
}
func isWeekend() -> Bool {
var isWeekend: Bool {
let range = NSCalendar.currentCalendar().maximumRangeOfUnit(.WeekdayCalendarUnit)
return (self.weekday() == range.location || self.weekday() == range.length)
return (self.weekday == range.location || self.weekday == range.length)
}


Expand Down Expand Up @@ -445,32 +440,32 @@ extension NSDate {

func weekdayToString() -> String {
let formatter = NSDateFormatter()
return formatter.weekdaySymbols[self.weekday()-1] as String
return formatter.weekdaySymbols[self.weekday-1] as String
}

func shortWeekdayToString() -> String {
let formatter = NSDateFormatter()
return formatter.shortWeekdaySymbols[self.weekday()-1] as String
return formatter.shortWeekdaySymbols[self.weekday-1] as String
}

func veryShortWeekdayToString() -> String {
let formatter = NSDateFormatter()
return formatter.veryShortWeekdaySymbols[self.weekday()-1] as String
return formatter.veryShortWeekdaySymbols[self.weekday-1] as String
}

func monthToString() -> String {
let formatter = NSDateFormatter()
return formatter.monthSymbols[self.month()-1] as String
return formatter.monthSymbols[self.month-1] as String
}

func shortMonthToString() -> String {
let formatter = NSDateFormatter()
return formatter.shortMonthSymbols[self.month()-1] as String
return formatter.shortMonthSymbols[self.month-1] as String
}

func veryShortMonthToString() -> String {
let formatter = NSDateFormatter()
return formatter.veryShortMonthSymbols[self.month()-1] as String
return formatter.veryShortMonthSymbols[self.month-1] as String
}


Expand Down
70 changes: 55 additions & 15 deletions Demo AF+Date+Helper/Demo AF+Date+Helper/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,25 +1,65 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6185.7" systemVersion="14A283o" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6205" systemVersion="14A314h" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="6Pl-1Z-F17">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6181.2"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6198"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="tne-QT-ifu">
<!--Navigation Controller-->
<scene sceneID="tie-vS-FJr">
<objects>
<viewController id="BYZ-38-t0r" customClass="ViewController" customModule="Swift_Demo_UIView_AF_Additions" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="480" height="480"/>
<navigationController id="6Pl-1Z-F17" sceneMemberID="viewController">
<navigationBar key="navigationBar" contentMode="scaleToFill" id="mfd-js-fbe">
<rect key="frame" x="0.0" y="0.0" width="320" height="44"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
<connections>
<segue destination="sgH-7a-lds" kind="relationship" relationship="rootViewController" id="Ysl-gf-Wu8"/>
</connections>
</navigationController>
<placeholder placeholderIdentifier="IBFirstResponder" id="zU6-Zp-0El" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-438" y="-285"/>
</scene>
<!--AF Date Helper-->
<scene sceneID="Jhf-fe-Yk7">
<objects>
<tableViewController id="sgH-7a-lds" customClass="ViewController" customModule="Swift_Demo_AF_Date_Helper" customModuleProvider="target" sceneMemberID="viewController">
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="60" sectionHeaderHeight="22" sectionFooterHeight="22" id="hdY-Qh-42s">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<prototypes>
<tableViewCell contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="Cell" textLabel="8xd-f2-dkY" detailTextLabel="OOo-dd-wrM" style="IBUITableViewCellStyleSubtitle" id="ebm-IR-qOS" customClass="Cell" customModule="Swift_Demo_AF_Date_Helper" customModuleProvider="target">
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="ebm-IR-qOS" id="DQr-mx-HI7">
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Title" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" minimumScaleFactor="0.5" id="8xd-f2-dkY">
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" name="HelveticaNeue-Light" family="Helvetica Neue" pointSize="20"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Subtitle" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" minimumScaleFactor="0.5" id="OOo-dd-wrM">
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" name="HelveticaNeue-Light" family="Helvetica Neue" pointSize="16"/>
<color key="textColor" red="1" green="0.0" blue="0.50196081400000003" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
</subviews>
</tableViewCellContentView>
</tableViewCell>
</prototypes>
<connections>
<outlet property="dataSource" destination="sgH-7a-lds" id="HUZ-Bg-6fP"/>
<outlet property="delegate" destination="sgH-7a-lds" id="3PU-IP-glO"/>
</connections>
</tableView>
<navigationItem key="navigationItem" title="AF Date Helper" id="rYI-Mr-t9G"/>
</tableViewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="xYL-29-SPv" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="259" y="-285"/>
</scene>
</scenes>
</document>
Loading

0 comments on commit edf7e2f

Please sign in to comment.