shuklavatsal1992

Vatsal Shukla

Oct 4th, 2017
701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 7.98 KB | None | 0 0
  1. //**********************************
  2. // Firebase Function to compare server current time & device's current
  3. // Use this function and
  4. //**********************************
  5. func checkServerTime() {      
  6.        
  7.         //GET CURRENT SERVER TIME TO COMPARE POST'S CREATE TIME
  8.         let serverTIme = FIRServerValue.timestamp()
  9.         let userRoot = Manager.sharedInstance.rootDBRef.child("users").child(key).child("time")
  10.         userRoot.setValue(serverTIme)
  11.         userRoot.observeSingleEvent(of: .value, with: { (snapshot) in
  12.             //print(snapshot.value ?? "NO VALUE FOUND")
  13.             if let number = snapshot.value as? NSNumber {
  14.                 let UNIXinterval = number.doubleValue
  15.                 let interval = TimeInterval(UNIXinterval/1000)
  16.                 let sourceDate = Date(timeIntervalSince1970: interval)
  17.                
  18.                
  19.                 let formatter1 = DateFormatter()
  20.                 formatter1.locale = Locale.current
  21.                 formatter1.dateFormat = "yyyy-MM-dd hh:mm:ss a"
  22.                 let str1 = formatter1.string(from: sourceDate)
  23.                 print("\n\n\t ##ServerDateTime:-> \(str1)\n\n")
  24.                 let serverDate = formatter1.date(from: str1)!
  25.                
  26.                
  27.                 let formatter2 = DateFormatter()
  28.                 formatter2.locale = Locale.current
  29.                 formatter2.dateFormat = "yyyy-MM-dd hh:mm:ss a"
  30.                 let str = formatter2.string(from: Date())
  31.                 print("\n\nLocalDateTime:-> \(str)\n\n")
  32.                 let localDate = formatter1.date(from: str)!
  33.                
  34.                 let minutes = localDate.differenceOfMinutes(to: serverDate)
  35.                 print(minutes)
  36.                 if minutes > -5 && minutes < 5 {
  37.                     User.shouldFreez = false
  38.                 }else {
  39.                     User.shouldFreez = true
  40.                 }
  41.                
  42.                 print("\n\n\t ##ServerDateTime Difference-> \(minutes)\n\n")
  43.                 //Send Notification
  44.                 DispatchQueue.main.async {
  45.                     let userInfo:[String : Any] = [User.kUserFreezKey:User.shouldFreez,User.kUserMinuteKey:minutes]
  46.                     NotificationCenter.default.post(name: .kTimeMismatchNotification, object: nil, userInfo: userInfo)
  47.                 }
  48.             }
  49.         })
  50.     }
  51.  
  52.  
  53.     //*********************************************************************************
  54.     // Date extension :-
  55.     // Use this extension for 'checkServerTime()'
  56.     //*********************************************************************************
  57.     extension Date{
  58.     typealias Minute = Int
  59.     typealias Second = Int
  60.     typealias Hour = Int
  61.     typealias Day = Int
  62.     typealias WeekOfYear = Int
  63.     typealias Month = Int
  64.     typealias Year = Int
  65.    
  66.     typealias Result = (Second,Minute,Hour,Day,WeekOfYear,Month,Year)
  67.    
  68.     public func timeAgoSince() -> String {
  69.         let calendar = Calendar.current
  70.         let now = Date()
  71.         let unitFlags: NSCalendar.Unit = [.second, .minute, .hour, .day, .weekOfYear, .month, .year]
  72.         let components = (calendar as NSCalendar).components(unitFlags, from: self, to: now, options: [])
  73.        
  74.         if components.year! >= 2 {
  75.             let time:Int = Int(components.year!)
  76.             let text = "\(time)y"
  77.             let string = text
  78.             return string
  79.         }
  80.        
  81.         if components.year! >= 1 {
  82.             return "1y"
  83.         }
  84.        
  85.         if components.month! >= 2 {
  86.             let time:Int = Int(components.month!)
  87.             let text = "\(time)M"
  88.             let string = text
  89.             return string
  90.         }
  91.        
  92.         if components.month! >= 1 {
  93.             return "1M"
  94.         }
  95.        
  96.         if components.weekOfYear! >= 2 {
  97.             let time:Int = Int(components.weekOfYear!)
  98.             let text = "\(time)w"
  99.             let string = text
  100.             return string
  101.         }
  102.        
  103.         if components.weekOfYear! >= 1 {
  104.             return "1w"
  105.         }
  106.        
  107.         if components.day! >= 2 {
  108.             let time:Int = Int(components.day!)
  109.             let text = "\(time)d"
  110.             let string = text
  111.             return string
  112.         }
  113.        
  114.         if components.day! >= 1 {
  115.             return "1d"
  116.         }
  117.        
  118.         if components.hour! >= 2 {
  119.             let time:Int = Int(components.hour!)
  120.             let text = "\(time)h"
  121.             let string = text
  122.             return string
  123.         }
  124.        
  125.         if components.hour! >= 1 {
  126.             return "1h"
  127.         }
  128.        
  129.         if components.minute! >= 2 {
  130.             let time:Int = Int(components.minute!)
  131.             let text = "\(time)m"
  132.             let string = text
  133.             return string
  134.         }
  135.        
  136.         if components.minute! >= 1 {
  137.             return "1m"
  138.         }
  139.        
  140.         if components.second! >= 3 {
  141.             let time:Int = Int(components.second!)
  142.             let text = "\(time)s"
  143.             let string = text
  144.             return string
  145.         }
  146.        
  147.         return "1s"
  148.        
  149.     }
  150.    
  151.     func timeAgo(fromDate from:Date) -> Result {
  152.         let calendar = Calendar.current
  153.         //let now = Date()
  154.         let unitFlags: NSCalendar.Unit = [.second, .minute, .hour, .day, .weekOfYear, .month, .year]
  155.         let components = (calendar as NSCalendar).components(unitFlags, from: self, to: from, options: [])
  156.         var year:Year = 0, month:Month = 0, weekOfYear:WeekOfYear = 0, day:Day = 0, hour:Hour = 0, minute:Minute = 0, second:Second = 0
  157.        
  158.         if components.year! >= 2 {
  159.             year = components.year!
  160.         }
  161.        
  162.         if components.year! >= 1 {
  163.             year = 1
  164.         }
  165.        
  166.         if components.month! >= 2 {
  167.             month = components.month!
  168.         }
  169.        
  170.         if components.month! >= 1 {
  171.             month = 1
  172.         }
  173.        
  174.         if components.weekOfYear! >= 2 {
  175.             weekOfYear = components.weekOfYear!
  176.         }
  177.        
  178.         if components.weekOfYear! >= 1 {
  179.             weekOfYear = 1
  180.         }
  181.        
  182.         if components.day! >= 2 {
  183.             day = components.day!
  184.         }
  185.        
  186.         if components.day! >= 1 {
  187.             day = 1
  188.         }
  189.        
  190.         if components.hour! >= 2 {
  191.             hour = components.hour!
  192.         }
  193.        
  194.         if components.hour! >= 1 {
  195.             hour = 1
  196.         }
  197.        
  198.         if components.minute! >= 2 {
  199.             minute = components.minute!
  200.         }
  201.        
  202.         if components.minute! >= 1 {
  203.             minute = 1
  204.         }
  205.        
  206.         if components.second! >= 2 {
  207.             second = components.second!
  208.         }
  209.        
  210.         if components.second! >= 1 {
  211.             second = 1
  212.         }
  213.        
  214.         return Result(second, minute, hour, day, weekOfYear, month, year)
  215.     }
  216.    
  217.     func differenceOfMinutes(to date:Date) -> Int {
  218.         var minutes:Int = 0
  219.         let calendar = Calendar.current
  220.         let units:Set<Calendar.Component> = [.minute]
  221.         let components = calendar.dateComponents(units, from: self, to: date)
  222.         if let minute = components.minute {
  223.             minutes = minute
  224.         }
  225.         return minutes
  226.     }
  227.    
  228.     func differenceOfHours(to date:Date) -> Int {
  229.         var hours:Int = 0
  230.         let calendar = Calendar.current
  231.         let units:Set<Calendar.Component> = [.hour]
  232.         let components = calendar.dateComponents(units, from: self, to: date)
  233.         if let hour = components.hour {
  234.             hours = hour
  235.         }
  236.         return hours
  237.     }
  238.    
  239.     func differenceOfDays(to date:Date) -> Int {
  240.         var days:Int = 0
  241.         let calendar = Calendar.current
  242.         let units:Set<Calendar.Component> = [.day]
  243.         let components = calendar.dateComponents(units, from: self, to: date)
  244.         if let day = components.day {
  245.             days = day
  246.         }
  247.         return days
  248.     }
  249. }
Add Comment
Please, Sign In to add comment