FAQ
To log a time difference I initially coded:


start := time.Now()
...
fmt.Printf("...took %s\n", time.Since(start))


but that resulted in an output of 19.7996387s which is impressive in its
resolution but I really only wanted a display resolution to the second.

So I thought the natural thing would be to call:

time.Since(start).Round(time.Second)

but there is no Round method on Duration, only on a Time. So I resorted to:


start := time.Now().Round(time.Second)
...
end := := time.Now().Round(time.Second)
fmt.Printf("...took %s\n", end.Sub(start))


which gives me the requisite display of "20s" but isn't as clean or precise
as rounding the duration itself.

Am I missing an obvious way of doing this or should there be a Round method
on Duration?

Thanks,
Gary



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Jan Mercl at Sep 2, 2014 at 3:57 pm
    On Tue, Sep 2, 2014 at 5:54 PM, Gary Scarr wrote:

    secs := (duration + time.Second/2) / time.Second

    -j

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Gary Scarr at Sep 2, 2014 at 4:27 pm
    Thanks Jan but that results in a display of "20ns" because of the inherent
    resolution of a Duration on my machine. So yes, I can then multiply the
    whole thing by time.second and get 20s but it seems very ugly compared to a
    Round method on Duration:

    duration := time.Since(start)
    duration = ((duration + time.Second/2) / time.Second ) * time.Second

    vs

    time.Since(start).Round(time.Second)

    Gary
    On Tuesday, September 2, 2014 11:58:01 AM UTC-4, Jan Mercl wrote:

    On Tue, Sep 2, 2014 at 5:54 PM, Gary Scarr <sca...@gmail.com <javascript:>>
    wrote:

    secs := (duration + time.Second/2) / time.Second

    -j
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Jan Mercl at Sep 2, 2014 at 4:34 pm
    Well, I forgot to mention that secs will contain an integer number of
    seconds (assumed "%ds" fmt.Printf format). The example should have used an
    explicit conversion to an int, though.

    (phone)
    -j

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Ian Davis at Sep 2, 2014 at 4:38 pm
    On Tue, Sep 2, 2014, at 04:54 PM, Gary Scarr wrote:


    Am I missing an obvious way of doing this or should there be a
    Round method on Duration?



    A Duration is just an int64 number of nanoseconds, so you could
    do:



    seconds := int64(time.Since(start)) / 1e9



    That would give you seconds as an int64



    Ian

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Gary Scarr at Sep 2, 2014 at 4:53 pm
    Thanks but then I lose the inherent formatting ability of the Duration
    object. Some of my durations run to hours and minutes (where even seconds
    are irrelevant) and I like the built-in logic that switches the display
    based upon the actual period.
    On Tuesday, September 2, 2014 12:38:15 PM UTC-4, Ian Davis wrote:


    On Tue, Sep 2, 2014, at 04:54 PM, Gary Scarr wrote:


    Am I missing an obvious way of doing this or should there be a Round
    method on Duration?


    A Duration is just an int64 number of nanoseconds, so you could do:

    seconds := int64(time.Since(start)) / 1e9

    That would give you seconds as an int64

    Ian
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Rob Pike at Sep 2, 2014 at 5:15 pm
    Just write the code.

    -rob

    On Tue, Sep 2, 2014 at 9:53 AM, Gary Scarr wrote:
    Thanks but then I lose the inherent formatting ability of the Duration
    object. Some of my durations run to hours and minutes (where even seconds
    are irrelevant) and I like the built-in logic that switches the display
    based upon the actual period.

    On Tuesday, September 2, 2014 12:38:15 PM UTC-4, Ian Davis wrote:


    On Tue, Sep 2, 2014, at 04:54 PM, Gary Scarr wrote:


    Am I missing an obvious way of doing this or should there be a Round
    method on Duration?


    A Duration is just an int64 number of nanoseconds, so you could do:

    seconds := int64(time.Since(start)) / 1e9

    That would give you seconds as an int64

    Ian
    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Jason Woods at Sep 2, 2014 at 7:16 pm

    On 2 Sep 2014, at 17:53, Gary Scarr wrote:

    Thanks but then I lose the inherent formatting ability of the Duration object. Some of my durations run to hours and minutes (where even seconds are irrelevant) and I like the built-in logic that switches the display based upon the actual period.
    On Tuesday, September 2, 2014 12:38:15 PM UTC-4, Ian Davis wrote:

    On Tue, Sep 2, 2014, at 04:54 PM, Gary Scarr wrote:

    Am I missing an obvious way of doing this or should there be a Round method on Duration?
    A Duration is just an int64 number of nanoseconds, so you could do:

    seconds := int64(time.Since(start)) / 1e9

    That would give you seconds as an int64

    Ian
    I use:

    now := time.Now()

    ... Stuff ...

    duration := time.Since(now)
    log.Printf("duration = %v", duration-(duration%time.Second))

    To get it rounded to minute etc just change the modulus.

    Is this what you want?

    Jason


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Andrew Bursavich at Sep 2, 2014 at 9:53 pm
    Here's a rounding function: http://play.golang.org/p/QHocTHl8iR

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Gary Scarr at Sep 3, 2014 at 3:16 am
    Bravo Andrew!!!

    This is indeed what I had been wanting: the automatic formatting range of
    the existing Duration with the arbitrary precision of the Time.Round
    function. With all due respect to Rob with his "Just write the code", it
    didn't seem that obvious and I would certainly never have come up with
    anything this elegant. My first instinct was along the lines of Jan's:(
    (dur + resolution/2)/resolution) * resolution but there's no hint of a 2 in
    your code so it will take me a while to grok it. But it does far better
    than my simple rounding of both timing endpoints so it doesn't affect their
    precision.

    As is often the case, after posing my "simple" question and then being
    asked "Is this what you want" it occurred to me that what might also be
    useful was to print a duration with a "sensible" precision like 2 or 3
    digits. Is there a way to use your function (other than a switch or
    if/else chain based on the value of d) that would achieve this? I tried a
    very naive making r a function of d as here where n was intended to be the
    approx digits of precision but it failed horribly:
    switch n {
    case 1:
    r = d / 10
    case 2:
    r = d / 100
    case 3:
    r = d / 1000
    case 4:
    r = d / 10000
    case 5:
    r = d / 100000
    case 6:
    r = d / 10000000
    }

    Thanks to you and all the others for your suggestions,
    Gary
    On Tuesday, September 2, 2014 5:52:57 PM UTC-4, Andrew Bursavich wrote:

    Here's a rounding function: http://play.golang.org/p/QHocTHl8iR
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Harmen B at Sep 3, 2014 at 8:14 am
    Maybe you can do something with this:

    fmt.Printf("Hello number: %.*f", 3, 12.3456789)

    http://play.golang.org/p/ZMR-xKDUbC

    The '*' operator is explained in the fmt godoc.

    On Wed, Sep 3, 2014 at 5:16 AM, Gary Scarr wrote:

    Bravo Andrew!!!

    This is indeed what I had been wanting: the automatic formatting range of
    the existing Duration with the arbitrary precision of the Time.Round
    function. With all due respect to Rob with his "Just write the code", it
    didn't seem that obvious and I would certainly never have come up with
    anything this elegant. My first instinct was along the lines of Jan's:(
    (dur + resolution/2)/resolution) * resolution but there's no hint of a 2 in
    your code so it will take me a while to grok it. But it does far better
    than my simple rounding of both timing endpoints so it doesn't affect their
    precision.

    As is often the case, after posing my "simple" question and then being
    asked "Is this what you want" it occurred to me that what might also be
    useful was to print a duration with a "sensible" precision like 2 or 3
    digits. Is there a way to use your function (other than a switch or
    if/else chain based on the value of d) that would achieve this? I tried a
    very naive making r a function of d as here where n was intended to be the
    approx digits of precision but it failed horribly:
    switch n {
    case 1:
    r = d / 10
    case 2:
    r = d / 100
    case 3:
    r = d / 1000
    case 4:
    r = d / 10000
    case 5:
    r = d / 100000
    case 6:
    r = d / 10000000
    }

    Thanks to you and all the others for your suggestions,
    Gary
    On Tuesday, September 2, 2014 5:52:57 PM UTC-4, Andrew Bursavich wrote:

    Here's a rounding function: http://play.golang.org/p/QHocTHl8iR
    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Andrew Bursavich at Sep 3, 2014 at 8:50 am

    On Tuesday, September 2, 2014 8:16:04 PM UTC-7, Gary Scarr wrote:
    Bravo Andrew!!!

    This is indeed what I had been wanting: the automatic formatting range of
    the existing Duration with the arbitrary precision of the Time.Round
    function. With all due respect to Rob with his "Just write the code", it
    didn't seem that obvious and I would certainly never have come up with
    anything this elegant.
    With all due respect to Rob, my solution was elegant and quick to arrive at
    because I basically jacked it from the standard library. You mentioned
    Time.Round <http://golang.org/pkg/time/#Time.Round> so I took a peek at the
    source <http://golang.org/src/pkg/time/time.go?s=32024:32060#L1097>. From
    there it just took a couple minutes. In my experience, if you want to do
    something similar to the standard library it's usually a good idea to start
    by reading it... and then maybe by stealing from it.


    My first instinct was along the lines of Jan's:( (dur +
    resolution/2)/resolution) * resolution but there's no hint of a 2 in your
    code so it will take me a while to grok it. But it does far better than my
    simple rounding of both timing endpoints so it doesn't affect their
    precision.
    There's a hint of a 2 with "m+m" ;)


    As is often the case, after posing my "simple" question and then being
    asked "Is this what you want" it occurred to me that what might also be
    useful was to print a duration with a "sensible" precision like 2 or 3
    digits. Is there a way to use your function (other than a switch or
    if/else chain based on the value of d) that would achieve this? I tried a
    very naive making r a function of d as here where n was intended to be the
    approx digits of precision but it failed horribly:
    switch n {
    case 1:
    r = d / 10
    case 2:
    r = d / 100
    case 3:
    r = d / 1000
    case 4:
    r = d / 10000
    case 5:
    r = d / 100000
    case 6:
    r = d / 10000000
    }
    Because it was kind of fun, here's a significant figure rounding function:
    http://play.golang.org/p/WjfKwhhjL5. I cheated a bit with math.Pow10
    <http://golang.org/pkg/math/#Pow10>. If you want anything more custom than
    that (e.g. "3.22h", "15.7m", etc.), you'll need to write your own string
    conversion function. In which case, this
    <http://golang.org/src/pkg/time/time.go?s=15097:15130#L454> would be a good
    place to start.


    Thanks to you and all the others for your suggestions,
    Gary
    Here's a rounding function: http://play.golang.org/p/QHocTHl8iR
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Andrew Bursavich at Sep 3, 2014 at 11:34 am
    Better version with the floats and math dependency
    removed: http://play.golang.org/p/E_nutl-BEf

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Egon at Sep 2, 2014 at 4:51 pm

    On Tuesday, 2 September 2014 18:54:27 UTC+3, Gary Scarr wrote:
    To log a time difference I initially coded:


    start := time.Now()
    ...
    fmt.Printf("...took %s\n", time.Since(start))


    but that resulted in an output of 19.7996387s which is impressive in its
    resolution but I really only wanted a display resolution to the second.

    So I thought the natural thing would be to call:

    time.Since(start).Round(time.Second)

    but there is no Round method on Duration, only on a Time. So I resorted
    to:


    start := time.Now().Round(time.Second)
    ...
    end := := time.Now().Round(time.Second)
    fmt.Printf("...took %s\n", end.Sub(start))
    start := time.Now()
    ...
    diff := time.Since(start)
    fmt.Printf("%v\n", diff.Nanoseconds()/time.Second.Nanoseconds())


    which gives me the requisite display of "20s" but isn't as clean or
    precise as rounding the duration itself.

    Am I missing an obvious way of doing this or should there be a Round
    method on Duration?

    Thanks,
    Gary


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Gary Scarr at Sep 2, 2014 at 5:21 pm
    This wins the originality prize for use of time.Seconds.Nanoseconds(). My
    immediate thought was "how can you call a method on a constant?" but then I
    realized "in Go you can call a method on anything". Unfortunately it also
    suffers from the limitation of Ian's solution since it ends up with an
    uint64. It's apparent that I didn't make clear in my original post that I
    want to continue to have a Duration object (so that it can display longer
    durations) but round the output to seconds.
    Gary
    On Tuesday, September 2, 2014 12:51:18 PM UTC-4, egon wrote:


    On Tuesday, 2 September 2014 18:54:27 UTC+3, Gary Scarr wrote:

    To log a time difference I initially coded:


    start := time.Now()
    ...
    fmt.Printf("...took %s\n", time.Since(start))


    but that resulted in an output of 19.7996387s which is impressive in its
    resolution but I really only wanted a display resolution to the second.

    So I thought the natural thing would be to call:

    time.Since(start).Round(time.Second)

    but there is no Round method on Duration, only on a Time. So I resorted
    to:


    start := time.Now().Round(time.Second)
    ...
    end := := time.Now().Round(time.Second)
    fmt.Printf("...took %s\n", end.Sub(start))
    start := time.Now()
    ...
    diff := time.Since(start)
    fmt.Printf("%v\n", diff.Nanoseconds()/time.Second.Nanoseconds())


    which gives me the requisite display of "20s" but isn't as clean or
    precise as rounding the duration itself.

    Am I missing an obvious way of doing this or should there be a Round
    method on Duration?

    Thanks,
    Gary


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Matthew Zimmerman at Sep 2, 2014 at 5:31 pm
    In (github.com/mzimmerman/racergo) I created a ```type HumanDuration
    time.Duration``` then defined String() to display 5k race times.

    func (hd HumanDuration) String() string {
       seconds := time.Duration(hd).Seconds()
       seconds -= float64(time.Duration(hd) / time.Minute * 60)
       return fmt.Sprintf("%#02d:%#02d:%05.2f",
    time.Duration(hd)/time.Hour, time.Duration(hd)/time.Minute%60,
    seconds)
    }

    Your exact use case isn't known so this may match or not.
    On Tue, Sep 2, 2014 at 1:20 PM, Gary Scarr wrote:
    This wins the originality prize for use of time.Seconds.Nanoseconds(). My
    immediate thought was "how can you call a method on a constant?" but then I
    realized "in Go you can call a method on anything". Unfortunately it also
    suffers from the limitation of Ian's solution since it ends up with an
    uint64. It's apparent that I didn't make clear in my original post that I
    want to continue to have a Duration object (so that it can display longer
    durations) but round the output to seconds.
    Gary

    On Tuesday, September 2, 2014 12:51:18 PM UTC-4, egon wrote:


    On Tuesday, 2 September 2014 18:54:27 UTC+3, Gary Scarr wrote:

    To log a time difference I initially coded:


    start := time.Now()
    ...
    fmt.Printf("...took %s\n", time.Since(start))


    but that resulted in an output of 19.7996387s which is impressive in its
    resolution but I really only wanted a display resolution to the second.

    So I thought the natural thing would be to call:

    time.Since(start).Round(time.Second)

    but there is no Round method on Duration, only on a Time. So I resorted
    to:


    start := time.Now().Round(time.Second)
    ...
    end := := time.Now().Round(time.Second)
    fmt.Printf("...took %s\n", end.Sub(start))

    start := time.Now()
    ...
    diff := time.Since(start)
    fmt.Printf("%v\n", diff.Nanoseconds()/time.Second.Nanoseconds())


    which gives me the requisite display of "20s" but isn't as clean or
    precise as rounding the duration itself.

    Am I missing an obvious way of doing this or should there be a Round
    method on Duration?

    Thanks,
    Gary

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 2, '14 at 3:54p
activeSep 3, '14 at 11:34a
posts16
users9
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase