Code Monkey home page Code Monkey logo

nanoseconds's Issues

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")

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.

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.