Skip to content

Instantly share code, notes, and snippets.

@Catharz
Created August 1, 2014 03:00
Show Gist options
  • Save Catharz/601dbe8c11787a02ef86 to your computer and use it in GitHub Desktop.
Save Catharz/601dbe8c11787a02ef86 to your computer and use it in GitHub Desktop.
/*
Some syntactic sugar for dealing with Time in Swift
Examples:
3.days.inHours
3.6.hours.ago
12.hours.inDays
*/
import Cocoa
extension Double {
var seconds: Double { return self }
var minutes: Double { return self.seconds * 60 }
var hours: Double { return self.minutes * 60 }
var days: Double { return self.hours * 24 }
var weeks: Double { return self.days * 7 }
var inMinutes: Double { return self.seconds / 60 }
var inHours: Double { return self.inMinutes / 60 }
var inDays: Double { return self.inHours / 24 }
var inWeeks: Double { return self.inDays / 7 }
var ago: NSDate { return NSDate(timeInterval: -NSTimeInterval(self), sinceDate: NSDate()) }
}
extension Int {
var seconds: Int { return self }
var minutes: Int { return self.seconds * 60 }
var hours: Int { return self.minutes * 60 }
var days: Int { return self.hours * 24 }
var weeks: Int { return self.days * 7 }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment