Swiftpack.co -  dominicegginton/Nanoseconds as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
dominicegginton/Nanoseconds
Simple high-resolution timing for Swift
.package(url: "https://github.com/dominicegginton/Nanoseconds.git", from: "1.1.1")

Simple high resolution time stamps for swift developers, along with helpful tools to work with them.

Key Features

  • High resolution time stamp
  • Built-in operators overloads
  • Useful extensions for unit conversion
  • Covert time intervals to a human readable strings
  • Linux compatible

Install

Install via the Swift Package Manger by declaring Nanoseconds as a dependency in your Package.swift:

.package(url: "https://github.com/dominicegginton/Nanoseconds", from: "1.0.0")

Remember to add Nanoseconds to your target as a dependency.

Documentation

Creating Timestamps

Creating high resolution timestamps with nanosecond accuracy is easy, simply create a instant of Now struct

let foo = Now()

Creating Time Intervals

Use the built-in operators overloads to easily calculate and initialize TimeInterval:

let start = Now()
sleep(1)
let end = Now()
let duration = end - start
print(duration)  //=> 1004222113.0

Convert Time Intervals from Nanoseconds to Other Time Units

To convert TimeInterval objects from nanosecond to a different unit simply use a provided extension:

  • nanoseconds
  • microseconds
  • milliseconds
  • seconds
  • minutes
  • hours
print(duration.milliseconds)  //=> 1004.22211
print(duration.seconds)  //=> 1.004222113

Create TimeIntervals Objects from any time unit

Use a provided init method to create a TimeInterval object from any time unit

let duration = TimeInterval(seconds: 8)

Covert TimeIntervals Objects to human readable strings

Use the provided timeString variable to convert TimeInterval objects into a human readable format:

let duration = TimeInterval(hours: 1) + TimeInterval(seconds: 120)
print(duration.timeString)  //=> 1h 1m 30s

GitHub

link
Stars: 3
Last commit: 1 week ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

17 weeks ago

Documentation

  • added documentation for converting TimeInterval objects to human readable strings (d874aa99470f12f38a1b9315c0a481adfaacc8b5)
  • introducing a cover image and logo for Nanoseconds (f4a266d9e1be92b232ab893e9c766ab2374b68ac)

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API