Code Monkey home page Code Monkey logo

nanoseconds's Introduction

Nanoseconds

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.

Getting Started

import Foundation
import Nanoseconds

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

Documentation

Now

/**
    A type that represents a point in time with nanosecond precision.
*/
public struct Now {
    /**
        Creates a new `Now` instance.
    */
    public init()

    /**
        Compares two `Now` instances.
        - Parameters:
            - lhs: The first `Now` instance.
            - rhs: The second `Now` instance.
        - Returns: `true` if the first `Now` instance is less than the second `Now` instance.
     */
    public static func < (lhs: Now, rhs: Now) -> Bool

    /**
        Compares two `Now` instances.
        - Parameters:
            - lhs: The first `Now` instance.
            - rhs: The second `Now` instance.
        - Returns: `true` if the first `Now` instance is less than or equal to the second `Now` instance.
     */
    public static func <= (lhs: Now, rhs: Now) -> Bool

    /**
        Compares two `Now` instances.
        - Parameters:
            - lhs: The first `Now` instance.
            - rhs: The second `Now` instance.
        - Returns: `true` if the first `Now` instance is greater than the second `Now` instance.
     */
    public static func > (lhs: Now, rhs: Now) -> Bool

    /**
        Compares two `Now` instances.
        - Parameters:
            - lhs: The first `Now` instance.
            - rhs: The second `Now` instance.
        - Returns: `true` if the first `Now` instance is greater than or equal to the second `Now` instance.
     */
    public static func >= (lhs: Now, rhs: Now) -> Bool

    /**
        Compares two `Now` instances.
        - Parameters:
            - lhs: The first `Now` instance.
            - rhs: The second `Now` instance.
        - Returns: `true` if the first `Now` instance is equal to the second `Now` instance.
     */
    public static func == (lhs: Now, rhs: Now) -> Bool

    /**
        Compares two `Now` instances.
        - Parameters:
            - lhs: The first `Now` instance.
            - rhs: The second `Now` instance.
        - Returns: `true` if the first `Now` instance is not equal to the second `Now` instance.
     */
    public static func != (lhs: Now, rhs: Now) -> Bool
    
    /**
        Subtract a `Now` instance from another `Now` instance.
        - Parameters:
            - lhs: The first `Now` instance.
            - rhs: The second `Now` instance.
        - Returns: The difference between the two `Now` instances.
     */
    public static func - (lhs: Now, rhs: Now) -> TimeInterval

    /**
        Add a `Now` instance to another `Now` instance.
        - Parameters:
            - lhs: The first `Now` instance.
            - rhs: The second `Now` instance.
        - Returns: The sum of the two `Now` instances.
     */
    public static func + (lhs: Now, rhs: Now) -> TimeInterval
}

TimeInterval

extension TimeInterval {
    /**
        Creates a new `TimeInterval` from the given number of nanoseconds.
        - Parameter nanoseconds: The number of nanoseconds.
     */
    public init (nanoseconds: Double)

    /**
        Creates a new `TimeInterval` from the given number of microseconds.
        - Parameter microseconds: The number of microseconds.
     */
    public init (microseconds: Double)

    /**
        Creates a new `TimeInterval` from the given number of milliseconds.
        - Parameter milliseconds: The number of milliseconds.
     */
    public init (milliseconds: Double)

    /**
        Creates a new `TimeInterval` from the given number of seconds.
        - Parameter seconds: The number of seconds.
     */
    public init (seconds: Double)

    /**
        Creates a new `TimeInterval` from the given number of minutes.
        - Parameter minutes: The number of minutes.
     */
    public init (minutes: Double)

    /**
        Creates a new `TimeInterval` from the given number of hours.
        - Parameter hours: The number of hours.
     */
    public init (hours: Double)

    /**
        The number of nanoseconds in the `TimeInterval`.
     */
    public var nanoseconds: Double

    /**
        The number of microseconds in the `TimeInterval`.
     */
    public var microseconds: Double

    /**
        The number of milliseconds in the `TimeInterval`.
     */
    public var milliseconds: Double

    /**
        The number of seconds in the `TimeInterval`.
     */
    public var seconds: Double

    /**
        The number of minutes in the `TimeInterval`.
     */
    public var minutes: Double

    /**
        The number of hours in the `TimeInterval`.
     */
    public var hours: Double

    /**
        Covert the `TimeInterval` to a string.
        The string will be in the format `[h]h [m]m [s]s`.
        Only the largest unit of time will be displayed.
     */
    public var timeString: String
}

nanoseconds's People

Contributors

dominicegginton avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

nanoseconds's Issues

Fundamental misunderstanding of TimeInterval

Unless I'm reading the the code for this project wrong it suggests you have a fundamental misunderstanding of the TimeInterval type in Swift.

TimeInterval is measured in seconds. So if you wanted to initialise a TimeInterval as a specific number of seconds you would initialise it directly, eg:

var seconds: TimeInterval = 10 // 10 seconds.

Your code instead has this:

public init (seconds: Double) { self = seconds * 1e+9 }

Which multiplies the stored TimeInterval by 1_000_000_000. Making the value stored be some multiple of a billion seconds. If you then passed this value to any Swift API expecting a TimeInterval you would be waiting a very long time.

I recommend you re-evaluate your assumptions here. Or better yet adopt the new iOS 16 Clock APIs.

If your intention was to create a Nanosecond type this logic would make sense. But you shouldn't use a Double (aka TimeInterval) for that. Instead look at Int64.

Subtraction is not returning reasonable values

There don't seem to be unit tests that check that the subtraction values are reasonable. I had my own tests in my app, and sleeping for two seconds doesn't yield a difference greater than one second.

CleanShot 2023-06-20 at 01 49 56@2x
let before = Now()
sleep(2)
let after = Now()
XCTAssertGreaterThan((after - before).seconds, 1)
// XCTAssertGreaterThan failed: ("0.048024575") is not greater than ("1.0")

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.