Tempo

Date and time manager for iOS/OSX written in Swift

Download .zip Download .tar.gz View on GitHub

What is this concretely ?

Tempo is a small and ligth librairy for the OSX and iOS developper for save time and reduce code. Tempo allows you to perform simple operations on dates and manage the display. Tempo is written only on swift but can be used on Objective-c project. It was designed to be very simple to use.

Usage

The Tempo class is a wrapper of your, and can be manage simply. How to create a new Tempo :

  var birthdayTempo = Tempo { (newTemp) -> () in
    newTemp.years = 2014
    newTemp.months = 12
    newTemp.days = 12
  }
  var currentTime = Tempo()
  var tempoWithDate = Tempo(date: customNSDate)

The format allows you to transform a Tempo date to a string, which can be read by a user or use by NSDate. You can use all the available format in NSDateFormatter (see the Apple documentation for more details).
Tempo provides for you a default format : yyyy MM dd HH:mm:ss

  Tempo().formatDate()                        // "2014 12 12 00:00:00"
  Tempo().formatDate("yyyy MM dd")            // "2014 12 12"
  Tempo().formatDate("dd EE / MMMM / yyyy")   // "12 Fri / December / 2014"

With tempo you can chain some operations on the date components

  var currentTime = Tempo()

  println("Before operation: \(currentTime.formatDate())") // "2014 12 18 15:28:12"
  currentTime.add(.Years, value: 1).add(.Months, value: 3).subtract(.Hours, value: 3).subtract(.Minutes, value: 80)
  println("After operation: \(currentTime.formatDate())") // "2016 03 18 11:08:12"

With Tempo, it's very simple to compare two dates. You can check if a date is after, before, or the same, on one line of code.

var newDate = Tempo { (newTemp) -> () in
  newTemp.years = 2014
  newTemp.months = 10
  newTemp.days = 12
  newTemp.minutes = 2
}

println("newDate: \(newDate.formatDate())")          // "2014 10 12 00:02:00"
println("current time : \(Tempo().formatDate())")    // "2014 12 18 15:40:11"

Tempo().isAfter(newDate)                // "True"
Tempo().isBefore(newDate)               // "False"
Tempo().isAfter(newDate, .Months)       // "False"
Tempo().isSame(newDate)                 // "False"
Tempo().isSame(newDate, .Years)         // "True"

Tempo allows you also to compare and know the difference for a specific date component between two dates.

var newDate = Tempo { (newTemp) -> () in
  newTemp.years = 2014
  newTemp.months = 10
  newTemp.days = 12
  newTemp.minutes = 2
}

Tempo().diff(.Years, date: newDate)     // "0"
Tempo().diff(.Months, date: newDate)    // "-2"
newDate.diff(.Secondes, date: Tempo())  // "5849274"

Tempo displayed to you in an understandable format and visual the difference between two dates. The display format is of the form "x time ago". You can display a past date or a future date, both work.

var date = Tempo { (newTemp) -> () in
  newTemp.years = 2014
  newTemp.months = 10
  newTemp.days = 25
}

println(date.timeAgoFromNow())      // "1 months ago"

newTemp.months = 12
newTemp.days = 18
newTemp.hours = 12

println(date.timeAgoFromNow())      // "3 hours ago"
println(Tempo().timeAgoFromNow())   // "seconds ago"
date.timeAgoFrom(otherTempo)