Last active
August 29, 2015 14:27
-
-
Save NicholasTD07/664bc8526f7ea7504674 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NSDate+timeAgo.swift | |
// | |
// | |
// Created by Nicholas Tian on 19/08/2015. | |
// Copyright (c) 2015 nickTD. All rights reserved. | |
// | |
import Foundation | |
// Note: Inspired by NSDate-TimeAgo | |
// https://github.com/kevinlawler/NSDate-TimeAgo/blob/master/NSDate%2BExtension.swift | |
// TODO: | |
// 1. Create a Xcode project | |
// 2. Test with Quick and Nimble | |
// 3. Carthage compatible | |
// 4. i18n maybe? | |
private struct Seconds { | |
static let Minute = 60 as Double | |
static let Hour = 60 * Seconds.Minute | |
static let Day = 24 * Seconds.Hour | |
static let Week = 7 * Seconds.Day | |
static let Month = 31 * Seconds.Day | |
static let Year = 365 * Seconds.Day | |
static func toSeconds(seconds: Double) -> Int { | |
return flooredAsInt(seconds) | |
} | |
static func toMinutes(seconds: Double) -> Int { | |
return flooredAsInt(seconds/Seconds.Minute) | |
} | |
static func toHours(seconds: Double) -> Int { | |
return flooredAsInt(seconds/Seconds.Hour) | |
} | |
static func toDays(seconds: Double) -> Int { | |
return flooredAsInt(seconds/Seconds.Day) | |
} | |
static func toWeeks(seconds: Double) -> Int { | |
return flooredAsInt(seconds/Seconds.Week) | |
} | |
static func toMonths(seconds: Double) -> Int { | |
return flooredAsInt(seconds/Seconds.Month) | |
} | |
static func toYears(seconds: Double) -> Int { | |
return flooredAsInt(seconds/Seconds.Year) | |
} | |
private static func flooredAsInt(value: Double) -> Int { | |
return Int(floor(value)) | |
} | |
} | |
extension NSDate { | |
var timeAgo: String { | |
let now = NSDate() | |
let deltaSeconds = fabs(timeIntervalSinceNow) as Double | |
switch deltaSeconds { | |
case 0..<Seconds.Minute: // within a minute, seconds | |
let seconds = Seconds.toSeconds(deltaSeconds) | |
return "\(seconds)secs" | |
case Seconds.Minute..<Seconds.Hour: // within an hour, minutes | |
let minutes = Seconds.toMinutes(deltaSeconds) | |
return "\(minutes)mins" | |
case Seconds.Hour..<Seconds.Day: // within a day, hours | |
let hours = Seconds.toHours(deltaSeconds) | |
return "\(hours)hours" | |
case Seconds.Day..<Seconds.Week: // within a week, days | |
let days = Seconds.toDays(deltaSeconds) | |
return "\(days)days" | |
case Seconds.Week..<Seconds.Month: // within a month, weeks | |
let weeks = Seconds.toWeeks(deltaSeconds) | |
return "\(weeks)weeks" | |
case Seconds.Month..<Seconds.Year: // within a year, months | |
let months = Seconds.toMonths(deltaSeconds) | |
return "\(months)months" | |
case Seconds.Year...Double.infinity: // years | |
let years = Seconds.toYears(deltaSeconds) | |
return "\(years)years" | |
default: | |
return "oops" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment