Skip to content

Add a convenient way to get the Milliseconds between two Instants #98

Closed

Description

@i-am-the-slime

This is a use case I have often when timing things.
I would like to avoid running unInstant in application code.

Maybe like this:

-- | Get the amount of milliseconds between the first and second Instant
-- | for example:
-- | ```
-- | do
-- |   start <- Instant.now
-- |   aLongRunningEffect
-- |   end <- Instant.now
-- |   let duration = Instant.delta end start
-- |   log ("A long running effect took " <> show duration)
-- | ```
delta ∷ Instant → Instant → Milliseconds
delta i1 i2 = (unInstant i1) <> negateDuration (unInstant i2)

Activity

  1. garyb commented on May 19, 2022

    @garyb
    Member

    Yeah agreed, I was doing something with this just the other day and was surprised we'd not included something for it already.

    I'd be tempted to make the type something like duration :: { start :: Instant, end :: Instant } -> Milliseconds to avoid confusion over argument order.

  2. hdgarrood commented on May 19, 2022

    @hdgarrood
    Contributor

    This feels to me like it could be a nice use case for the Duration class?

    delta :: forall a. Duration a => Instant -> Instant -> a
    
  3. i-am-the-slime commented on May 19, 2022

    @i-am-the-slime
    ContributorAuthor

    Yes, but it risks to also be a bit annoying when trying to just log it with show for example, right?

  4. hdgarrood commented on May 19, 2022

    @hdgarrood
    Contributor

    Yeah. Either way it's easy to get what you want. I guess the other Instant functions work in Milliseconds so maybe it'd make more sense to do the same here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions