All Projects → dreymonde → Time

dreymonde / Time

Licence: mit
🕰 Type-safe time calculations in Swift

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Time

Time
Type-safe time calculations in Kotlin, powered by generics.
Stars: ✭ 939 (-9.01%)
Mutual labels:  generics, time
Covid19 Brazil Timeseries
Data collection to analyze the dissemination of COVID-19 through Brazilian states. Contributions are welcome.
Stars: ✭ 43 (-95.83%)
Mutual labels:  time
Pytorch Forecasting
Time series forecasting with PyTorch
Stars: ✭ 849 (-17.73%)
Mutual labels:  time
Genericn Arytree
Simple Generic N-ary Tree implementation in Java, with many utility methods.
Stars: ✭ 40 (-96.12%)
Mutual labels:  generics
Dayjs
⏰ Day.js 2kB immutable date-time library alternative to Moment.js with the same modern API
Stars: ✭ 37,373 (+3521.41%)
Mutual labels:  time
One Liner
Constraint-based generics
Stars: ✭ 41 (-96.03%)
Mutual labels:  generics
Goderive
Code Generation for Functional Programming, Concurrency and Generics in Golang
Stars: ✭ 848 (-17.83%)
Mutual labels:  generics
Nova Time Field
Laravel Nova Time Field
Stars: ✭ 45 (-95.64%)
Mutual labels:  time
Pickadate.js
The mobile-friendly, responsive, and lightweight jQuery date & time input picker.
Stars: ✭ 7,753 (+651.26%)
Mutual labels:  time
Tinydate
A tiny (349B) reusable date formatter. Extremely fast!
Stars: ✭ 990 (-4.07%)
Mutual labels:  time
Time ago in words
Humanize elapsed time from some Time instance to Time.now, e.g. '2 hours and 1 minute ago'. ATTENTION: NOT MAINTAINED
Stars: ✭ 33 (-96.8%)
Mutual labels:  time
React Picky Date Time
A react component for date time picker. Online demo examples
Stars: ✭ 31 (-97%)
Mutual labels:  time
Letnice
A simple javascript calendar
Stars: ✭ 41 (-96.03%)
Mutual labels:  time
Time
Windows tool for measuring command/program execution speed
Stars: ✭ 21 (-97.97%)
Mutual labels:  time
Tempo
A Kotlin library for Android to get the current time from multiple sources: SNTP, GPS; or your own time source.
Stars: ✭ 44 (-95.74%)
Mutual labels:  time
Anti Testportal
Bypass for blur check on testportal.pl(net)
Stars: ✭ 39 (-96.22%)
Mutual labels:  time
Genericdatasource
Generic Data Source in Swift.
Stars: ✭ 33 (-96.8%)
Mutual labels:  generics
Mvvm Example
iOS protocol-oriented MVVM examples
Stars: ✭ 45 (-95.64%)
Mutual labels:  generics
Pretty Time
Easily format the time from node.js `process.hrtime`. Works with timescales ranging from weeks to nanoseconds.
Stars: ✭ 44 (-95.74%)
Mutual labels:  time
Period
Complex period comparisons
Stars: ✭ 1,001 (-3%)
Mutual labels:  time

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
}

Installation

Swift Package Manager

Starting with Xcode 11, Time is officially available only via Swift Package Manager.

In Xcode 11 or greater, in you project, select: File > Swift Packages > Add Pacakage Dependency

In the search bar type

https://github.com/dreymonde/Time

Then proceed with installation.

If you can't find anything in the panel of the Swift Packages you probably haven't added yet your github account. You can do that under the Preferences panel of your Xcode, in the Accounts section.

For command-line based apps, you can just add this directly to your Package.swift file:

dependencies: [
    .package(url: "https://github.com/dreymonde/Time", from: "1.0.1"),
]

Manual

Of course, you always have an option of just copying-and-pasting the code - Time is just two files, so feel free.

Deprecated dependency managers

Last Time version to support Carthage and Cocoapods is 1.0.1. Carthage and Cocoapods will no longer be officially supported.

Carthage:

github "dreymonde/Time" ~> 1.0.1

Cocoapods:

pod 'TimeIntervals', '~> 1.0.1'
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].