Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //**********************************
- // Firebase Function to compare server current time & device's current
- // Use this function and
- //**********************************
- func checkServerTime() {
- //GET CURRENT SERVER TIME TO COMPARE POST'S CREATE TIME
- let serverTIme = FIRServerValue.timestamp()
- let userRoot = Manager.sharedInstance.rootDBRef.child("users").child(key).child("time")
- userRoot.setValue(serverTIme)
- userRoot.observeSingleEvent(of: .value, with: { (snapshot) in
- //print(snapshot.value ?? "NO VALUE FOUND")
- if let number = snapshot.value as? NSNumber {
- let UNIXinterval = number.doubleValue
- let interval = TimeInterval(UNIXinterval/1000)
- let sourceDate = Date(timeIntervalSince1970: interval)
- let formatter1 = DateFormatter()
- formatter1.locale = Locale.current
- formatter1.dateFormat = "yyyy-MM-dd hh:mm:ss a"
- let str1 = formatter1.string(from: sourceDate)
- print("\n\n\t ##ServerDateTime:-> \(str1)\n\n")
- let serverDate = formatter1.date(from: str1)!
- let formatter2 = DateFormatter()
- formatter2.locale = Locale.current
- formatter2.dateFormat = "yyyy-MM-dd hh:mm:ss a"
- let str = formatter2.string(from: Date())
- print("\n\nLocalDateTime:-> \(str)\n\n")
- let localDate = formatter1.date(from: str)!
- let minutes = localDate.differenceOfMinutes(to: serverDate)
- print(minutes)
- if minutes > -5 && minutes < 5 {
- User.shouldFreez = false
- }else {
- User.shouldFreez = true
- }
- print("\n\n\t ##ServerDateTime Difference-> \(minutes)\n\n")
- //Send Notification
- DispatchQueue.main.async {
- let userInfo:[String : Any] = [User.kUserFreezKey:User.shouldFreez,User.kUserMinuteKey:minutes]
- NotificationCenter.default.post(name: .kTimeMismatchNotification, object: nil, userInfo: userInfo)
- }
- }
- })
- }
- //*********************************************************************************
- // Date extension :-
- // Use this extension for 'checkServerTime()'
- //*********************************************************************************
- extension Date{
- typealias Minute = Int
- typealias Second = Int
- typealias Hour = Int
- typealias Day = Int
- typealias WeekOfYear = Int
- typealias Month = Int
- typealias Year = Int
- typealias Result = (Second,Minute,Hour,Day,WeekOfYear,Month,Year)
- public func timeAgoSince() -> String {
- let calendar = Calendar.current
- let now = Date()
- let unitFlags: NSCalendar.Unit = [.second, .minute, .hour, .day, .weekOfYear, .month, .year]
- let components = (calendar as NSCalendar).components(unitFlags, from: self, to: now, options: [])
- if components.year! >= 2 {
- let time:Int = Int(components.year!)
- let text = "\(time)y"
- let string = text
- return string
- }
- if components.year! >= 1 {
- return "1y"
- }
- if components.month! >= 2 {
- let time:Int = Int(components.month!)
- let text = "\(time)M"
- let string = text
- return string
- }
- if components.month! >= 1 {
- return "1M"
- }
- if components.weekOfYear! >= 2 {
- let time:Int = Int(components.weekOfYear!)
- let text = "\(time)w"
- let string = text
- return string
- }
- if components.weekOfYear! >= 1 {
- return "1w"
- }
- if components.day! >= 2 {
- let time:Int = Int(components.day!)
- let text = "\(time)d"
- let string = text
- return string
- }
- if components.day! >= 1 {
- return "1d"
- }
- if components.hour! >= 2 {
- let time:Int = Int(components.hour!)
- let text = "\(time)h"
- let string = text
- return string
- }
- if components.hour! >= 1 {
- return "1h"
- }
- if components.minute! >= 2 {
- let time:Int = Int(components.minute!)
- let text = "\(time)m"
- let string = text
- return string
- }
- if components.minute! >= 1 {
- return "1m"
- }
- if components.second! >= 3 {
- let time:Int = Int(components.second!)
- let text = "\(time)s"
- let string = text
- return string
- }
- return "1s"
- }
- func timeAgo(fromDate from:Date) -> Result {
- let calendar = Calendar.current
- //let now = Date()
- let unitFlags: NSCalendar.Unit = [.second, .minute, .hour, .day, .weekOfYear, .month, .year]
- let components = (calendar as NSCalendar).components(unitFlags, from: self, to: from, options: [])
- var year:Year = 0, month:Month = 0, weekOfYear:WeekOfYear = 0, day:Day = 0, hour:Hour = 0, minute:Minute = 0, second:Second = 0
- if components.year! >= 2 {
- year = components.year!
- }
- if components.year! >= 1 {
- year = 1
- }
- if components.month! >= 2 {
- month = components.month!
- }
- if components.month! >= 1 {
- month = 1
- }
- if components.weekOfYear! >= 2 {
- weekOfYear = components.weekOfYear!
- }
- if components.weekOfYear! >= 1 {
- weekOfYear = 1
- }
- if components.day! >= 2 {
- day = components.day!
- }
- if components.day! >= 1 {
- day = 1
- }
- if components.hour! >= 2 {
- hour = components.hour!
- }
- if components.hour! >= 1 {
- hour = 1
- }
- if components.minute! >= 2 {
- minute = components.minute!
- }
- if components.minute! >= 1 {
- minute = 1
- }
- if components.second! >= 2 {
- second = components.second!
- }
- if components.second! >= 1 {
- second = 1
- }
- return Result(second, minute, hour, day, weekOfYear, month, year)
- }
- func differenceOfMinutes(to date:Date) -> Int {
- var minutes:Int = 0
- let calendar = Calendar.current
- let units:Set<Calendar.Component> = [.minute]
- let components = calendar.dateComponents(units, from: self, to: date)
- if let minute = components.minute {
- minutes = minute
- }
- return minutes
- }
- func differenceOfHours(to date:Date) -> Int {
- var hours:Int = 0
- let calendar = Calendar.current
- let units:Set<Calendar.Component> = [.hour]
- let components = calendar.dateComponents(units, from: self, to: date)
- if let hour = components.hour {
- hours = hour
- }
- return hours
- }
- func differenceOfDays(to date:Date) -> Int {
- var days:Int = 0
- let calendar = Calendar.current
- let units:Set<Calendar.Component> = [.day]
- let components = calendar.dateComponents(units, from: self, to: date)
- if let day = components.day {
- days = day
- }
- return days
- }
- }
Add Comment
Please, Sign In to add comment