Expiry.swift 831 B

1234567891011121314151617181920212223242526272829303132
  1. import Foundation
  2. /**
  3. Helper enum to set the expiration date
  4. */
  5. public enum Expiry {
  6. /// Object will be expired in the nearest future
  7. case never
  8. /// Object will be expired in the specified amount of seconds
  9. case seconds(TimeInterval)
  10. /// Object will be expired on the specified date
  11. case date(Date)
  12. /// Returns the appropriate date object
  13. public var date: Date {
  14. switch self {
  15. case .never:
  16. // Ref: http://lists.apple.com/archives/cocoa-dev/2005/Apr/msg01833.html
  17. return Date(timeIntervalSince1970: 60 * 60 * 24 * 365 * 68)
  18. case .seconds(let seconds):
  19. return Date().addingTimeInterval(seconds)
  20. case .date(let date):
  21. return date
  22. }
  23. }
  24. /// Checks if cached object is expired according to expiration date
  25. public var isExpired: Bool {
  26. return date.inThePast
  27. }
  28. }