//
//  NSCalendar+Conversion.swift
//  Datez
//
//  Created by Mazyad Alabduljaleel on 11/7/15.
//  Copyright © 2015 kitz. All rights reserved.
//

import Foundation


extension NSCalendar {
    
    func components(units: NSCalendarUnit, fromTimeInterval timeInterval: NSTimeInterval) -> NSDateComponents {
        
        let baseDate = NSDate(timeIntervalSinceReferenceDate: 0)
        return components(
            units,
            fromDate: baseDate,
            toDate: baseDate + round(timeInterval),
            options: []
        )
    }
}

public extension NSCalendar {
    
    static var Gregorian: NSCalendar {
        return NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
    }
    
    static var IslamicCivil: NSCalendar {
        return NSCalendar(calendarIdentifier: NSCalendarIdentifierIslamicCivil)!
    }
}

//
//  NSCalendar+Conversion.swift
//  Datez
//
//  Created by Mazyad Alabduljaleel on 11/7/15.
//  Copyright © 2015 kitz. All rights reserved.
//

import Foundation


extension NSCalendar {
    
    func components(units: NSCalendarUnit, fromTimeInterval timeInterval: NSTimeInterval) -> NSDateComponents {
        
        return components(
            units,
            fromDate: NSDate(),
            toDate: NSDate(timeIntervalSinceNow: round(timeInterval)),
            options: []
        )
    }
}

public extension NSCalendar {
    
    static var Gregorian: NSCalendar {
        return NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
    }
    
    static var IslamicCivil: NSCalendar {
        return NSCalendar(calendarIdentifier: NSCalendarIdentifierIslamicCivil)!
    }
}

--- 
+++ 
@@ -13,10 +13,11 @@
     
     func components(units: NSCalendarUnit, fromTimeInterval timeInterval: NSTimeInterval) -> NSDateComponents {
         
+        let baseDate = NSDate(timeIntervalSinceReferenceDate: 0)
         return components(
             units,
-            fromDate: NSDate(),
-            toDate: NSDate(timeIntervalSinceNow: round(timeInterval)),
+            fromDate: baseDate,
+            toDate: baseDate + round(timeInterval),
             options: []
         )
     }
Fix time interval calculation
A well defined reference date should be used instead of the unpredictable NSDate()
