FAQ
I find the absence of "Time Minus Duration = Time" to be frustrating.
Developers can do it themselves just as they can now do "Time Plus Duration
= Time" and "Time Minus Time = Duration."

func SubDuration(t time.Time, d time.Duration) time.Time {
  ns := t.UnixNano() - d.Nanoseconds()
return time.Unix(ns/1e9, ns%1e9)
}

... or we can add this to time.go:

SubDuration returns the time t-d.
func (t Time) SubDuration(d Duration) Time {
  t.sec -= int64(d / 1e9)
nsec := int32(t.nsec) - int32(d%1e9)
  if nsec >= 1e9 {
t.sec++
  nsec -= 1e9
} else if nsec < 0 {
  t.sec--
nsec += 1e9
  }
t.nsec = uintptr(nsec)
  return t
}

I'm proposing the latter so that the time methods are complete. Any
objection? If not I'll open an issue and submit the code.

--
*Michael T. Jones | Chief Technology Advocate | mtj@google.com
<mtj@google.com> | +1 650-335-5765*

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

  • David Symonds at Jul 4, 2014 at 12:10 am
    The time package already has a very big API, and your t.SubDuration(d)
    method is simply equivalent to t.Add(-d) but with fewer keystrokes. I
    don't see much confusion around this that isn't momentary.

    --
    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.
  • Michael Jones at Jul 4, 2014 at 12:22 am
    Do we ever say in the documentation that negative durations are OK? I'll
    look...

    On Thu, Jul 3, 2014 at 5:10 PM, David Symonds wrote:

    The time package already has a very big API, and your t.SubDuration(d)
    method is simply equivalent to t.Add(-d) but with fewer keystrokes. I
    don't see much confusion around this that isn't momentary.


    --
    *Michael T. Jones | Chief Technology Advocate | mtj@google.com
    <mtj@google.com> | +1 650-335-5765*

    --
    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.
  • Chris Hines at Jul 4, 2014 at 12:32 am
    func ParseDuration

    func ParseDuration(s string) (Duration, error)

    ParseDuration parses a duration string. A duration string is a possibly signed sequence of decimal numbers, each with optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
    func (Time) Sub

    func (t Time) Sub(u Time) Duration

    Sub returns the duration t-u. If the result exceeds the maximum (or minimum) value that can be stored in a Duration, the maximum (or minimum) duration will be returned. To compute t-d for a duration d, use t.Add(-d).

    --
    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.
  • Michael Jones at Jul 4, 2014 at 12:39 am
    I had missed that. Thanks!

    On Thu, Jul 3, 2014 at 5:32 PM, Chris Hines wrote:

    func ParseDuration

    func ParseDuration(s string) (Duration, error)

    ParseDuration parses a duration string. A duration string is a possibly
    signed sequence of decimal numbers, each with optional fraction and a unit
    suffix, such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns",
    "us" (or "µs"), "ms", "s", "m", "h".
    func (Time) Sub

    func (t Time) Sub(u Time) Duration

    Sub returns the duration t-u. If the result exceeds the maximum (or
    minimum) value that can be stored in a Duration, the maximum (or minimum)
    duration will be returned. To compute t-d for a duration d, use t.Add(-d).

    --
    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.


    --
    *Michael T. Jones | Chief Technology Advocate | mtj@google.com
    <mtj@google.com> | +1 650-335-5765*

    --
    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.
  • Rui Ueyama at Jul 4, 2014 at 1:17 am
    You may find it interesting that sync.atomic package also provides only Add
    functions because of the same reason -- you can always add a negative
    number.

    On Thu, Jul 3, 2014 at 5:39 PM, 'Michael Jones' via golang-nuts wrote:

    I had missed that. Thanks!

    On Thu, Jul 3, 2014 at 5:32 PM, Chris Hines wrote:

    func ParseDuration

    func ParseDuration(s string) (Duration, error)

    ParseDuration parses a duration string. A duration string is a possibly
    signed sequence of decimal numbers, each with optional fraction and a unit
    suffix, such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns",
    "us" (or "µs"), "ms", "s", "m", "h".
    func (Time) Sub

    func (t Time) Sub(u Time) Duration

    Sub returns the duration t-u. If the result exceeds the maximum (or
    minimum) value that can be stored in a Duration, the maximum (or minimum)
    duration will be returned. To compute t-d for a duration d, use t.Add(-d).

    --
    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.


    --
    *Michael T. Jones | Chief Technology Advocate | mtj@google.com
    <mtj@google.com> | +1 650-335-5765 <%2B1%20650-335-5765>*

    --
    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.
  • Michael Jones at Jul 4, 2014 at 1:33 am
    What? I'm no longer clever, insightful, persuasive, or informed because I
    did not know that the documentation suggested that negative durations were
    encouraged? That's funny. I've made much bigger mistakes than that! At
    least you've left me with courageous and cheerful. That's enough to move
    mountains. ;-)

    On Thu, Jul 3, 2014 at 6:16 PM, Rui Ueyama wrote:

    You may find it interesting that sync.atomic package also provides only
    Add functions because of the same reason -- you can always add a negative
    number.


    On Thu, Jul 3, 2014 at 5:39 PM, 'Michael Jones' via golang-nuts <
    golang-nuts@googlegroups.com> wrote:
    I had missed that. Thanks!

    On Thu, Jul 3, 2014 at 5:32 PM, Chris Hines wrote:

    func ParseDuration

    func ParseDuration(s string) (Duration, error)

    ParseDuration parses a duration string. A duration string is a possibly
    signed sequence of decimal numbers, each with optional fraction and a unit
    suffix, such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns",
    "us" (or "µs"), "ms", "s", "m", "h".
    func (Time) Sub

    func (t Time) Sub(u Time) Duration

    Sub returns the duration t-u. If the result exceeds the maximum (or
    minimum) value that can be stored in a Duration, the maximum (or minimum)
    duration will be returned. To compute t-d for a duration d, use t.Add(-d).

    --
    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.


    --
    *Michael T. Jones | Chief Technology Advocate | mtj@google.com
    <mtj@google.com> | +1 650-335-5765 <%2B1%20650-335-5765>*

    --
    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.

    --
    *Michael T. Jones | Chief Technology Advocate | mtj@google.com
    <mtj@google.com> | +1 650-335-5765*

    --
    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.
  • Caleb Spare at Jul 4, 2014 at 12:11 am
    I've always just added a negative time.

    http://play.golang.org/p/Z5UGD0PQs0

    I assumed that's part of the reason a duration is an int64 rather than uint64.

    On Thu, Jul 3, 2014 at 5:06 PM, 'Michael Jones' via golang-nuts
    wrote:
    I find the absence of "Time Minus Duration = Time" to be frustrating.
    Developers can do it themselves just as they can now do "Time Plus Duration
    = Time" and "Time Minus Time = Duration."

    func SubDuration(t time.Time, d time.Duration) time.Time {
    ns := t.UnixNano() - d.Nanoseconds()
    return time.Unix(ns/1e9, ns%1e9)
    }

    ... or we can add this to time.go:

    SubDuration returns the time t-d.
    func (t Time) SubDuration(d Duration) Time {
    t.sec -= int64(d / 1e9)
    nsec := int32(t.nsec) - int32(d%1e9)
    if nsec >= 1e9 {
    t.sec++
    nsec -= 1e9
    } else if nsec < 0 {
    t.sec--
    nsec += 1e9
    }
    t.nsec = uintptr(nsec)
    return t
    }

    I'm proposing the latter so that the time methods are complete. Any
    objection? If not I'll open an issue and submit the code.

    --
    Michael T. Jones | Chief Technology Advocate | mtj@google.com | +1
    650-335-5765

    --
    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.
  • Akwillis at Jul 4, 2014 at 1:10 am
    It might be time to update the tagline in your google profile and remove
    the first four descriptive adjectives.
    On Thursday, July 3, 2014 8:07:26 PM UTC-4, Michael Jones wrote:

    I find the absence of "Time Minus Duration = Time" to be frustrating.
    Developers can do it themselves just as they can now do "Time Plus Duration
    = Time" and "Time Minus Time = Duration."

    func SubDuration(t time.Time, d time.Duration) time.Time {
    ns := t.UnixNano() - d.Nanoseconds()
    return time.Unix(ns/1e9, ns%1e9)
    }

    ... or we can add this to time.go:

    SubDuration returns the time t-d.
    func (t Time) SubDuration(d Duration) Time {
    t.sec -= int64(d / 1e9)
    nsec := int32(t.nsec) - int32(d%1e9)
    if nsec >= 1e9 {
    t.sec++
    nsec -= 1e9
    } else if nsec < 0 {
    t.sec--
    nsec += 1e9
    }
    t.nsec = uintptr(nsec)
    return t
    }

    I'm proposing the latter so that the time methods are complete. Any
    objection? If not I'll open an issue and submit the code.

    --
    *Michael T. Jones | Chief Technology Advocate | m...@google.com
    <javascript:> | +1 650-335-5765*
    --
    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.
  • Dan Kortschak at Jul 4, 2014 at 1:17 am

    On Thu, 2014-07-03 at 18:10 -0700, akwillis wrote:
    It might be time to update the tagline in your google profile and
    remove the first four descriptive adjectives.
    Please.

    --
    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
postedJul 4, '14 at 12:07a
activeJul 4, '14 at 1:33a
posts10
users7
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase