Git Product home page Git Product logo

time's Introduction

Time

This micro-library is made for you if:

  • You have ever written something like this:
let interval: TimeInterval = 10 * 60

To represent 10 minutes.

Usage

Showcase

import Time

let tenMinutes = 10.minutes
let afterTenMinutes = Date() + 10.minutes
let tenMinutesAndSome = 10.minutes + 15.seconds
let tenMinutesInSeconds = 10.minutes.inSeconds
if 10.minutes > 500.seconds {
    print("That's right")
}

Basics

Time is not just a bunch of Double conversion functions. The main advantage of it is that all time units are strongly-typed. So, for example:

let tenMinutes = 10.minutes

Here tenMinutes will actually be of type Interval<Minute> (not to be confused with Foundation's TimeInterval). There are seven time units available, from nanoseconds to days:

public extension Double {
    
    var seconds: Interval<Second> {
        return Interval<Second>(self)
    }
    
    var minutes: Interval<Minute> {
        return Interval<Minute>(self)
    }
    
    var milliseconds: Interval<Millisecond> {
        return Interval<Millisecond>(self)
    }
    
    var microseconds: Interval<Microsecond> {
        return Interval<Microsecond>(self)
    }
    
    var nanoseconds: Interval<Nanosecond> {
        return Interval<Nanosecond>(self)
    }
    
    var hours: Interval<Hour> {
        return Interval<Hour>(self)
    }
    
    var days: Interval<Day> {
        return Interval<Day>(self)
    }
    
}

Operations

You can perform all basic arithmetic operations on time intervals, even of different units:

let interval = 10.minutes + 15.seconds - 3.minutes + 2.hours // Interval<Minute>
let doubled = interval * 2

let seconds = 10.seconds + 3.minutes // Interval<Second>

You can also use these operations on Date:

let oneHourAfter = Date() + 1.hours

Conversions

Time intervals are easily convertible:

let twoMinutesInSeconds = 2.minutes.inSeconds // Interval<Second>

You can also convert intervals to Foundation's TimeInterval, if needed:

let timeInterval = 5.minutes.timeInterval

You can also use converted(to:) method:

let fiveSecondsInHours = 5.seconds.converted(to: Hour.self) // Interval<Hour>
// or
let fiveSecondsInHours: Interval<Hour> = 5.seconds.converted()

Although, in my opinion, you would rarely need to.

Comparison

You can compare different time units as well

50.minutes < 1.hour

Creating your own time units

If, for some reason, you need to create your own time unit, that's super easy to do:

public enum Week : TimeUnit {
    
    public static var toTimeIntervalRatio: Double {
        return 604800
    }
    
}

Now you can use it as any other time unit:

let fiveWeeks = Interval<Week>(5)

For the sake of convenience, don't forget to write those handy extensions:

public enum Week : TimeUnit {
    
    public static var toTimeIntervalRatio: Double {
        return 604800
    }
    
}

extension Interval {
    
    public var inWeeks: Interval<Week> {
        return converted()
    }
    
}

extension Double {
    
    public var weeks: Interval<Week> {
        return Interval<Week>(self)
    }
    
}

extension Int {
    
    public var weeks: Interval<Week> {
        return Interval<Week>(Double(self))
    }
    
}

Also

Also available:

  • Get conversion rate:
let conversionRate = Hour.conversionRate(to: Second.self) // 3600.0
  • GCD integration:
DispatchQueue.main.asyncAfter(after: 5.seconds) {
	// do stuff
}

Disclaimer

Time is in very early stage. Some stuff will probably be broken at some point.

Installation

Time is available through Carthage. To install, just write into your Cartfile:

github "dreymonde/Time" ~> 0.2.0

You can also use SwiftPM. Just add to your Package.swift:

import PackageDescription

let package = Package(
    dependencies: [
        .Package(url: "https://github.com/dreymonde/Time.git", majorVersion: 0, minor: 2),
    ]
)

time's People

Contributors

dreymonde avatar rafaelguerreiro avatar foscomputerservices avatar

Stargazers

MohsinAli avatar

Watchers

MohsinAli avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.