decitrig,
"trying to add a time-only value to a date-only value"
On Sun, Jan 13, 2013 at 4:16 PM, Ryan Sims <rws...@gmail.com> wrote:Actually no `time.Time` can be fully expressed by `time.Duration`.
> Then why have a Time type at all, since all times can be expressed as
> durations?
They are fundamentally different. Durations have no locations.
Yes as that's not a duration. If you are adding 15 hours to time,
> When doing time.Parse("15:04", "15:00"), I imagine people rarely
> have the explicit intent of expression 3:00pm on 1/1/0 UTC, i.e. a specific
> point in time. I certainly wouldn't prompt a user for a time of day
> expressed as a duration.
you're adding a duration, not a time.
On Sun, Jan 13, 2013 at 4:41 PM, Ryan Sims <rws...@gmail.com> wrote:It still makes no sense for me to parse "15:04" as time.Time as it is
> Right. So in my original algorithm, I tried to convert a time to a duration
> by subtracting the zero time. This didn't work as expected, because parsing
> "15:04" results in a negative time: this is the odd behavior I wanted to
> highlight. Whether I'm misusing time.Time in this specific case, I think
> it's reasonable to assume that time.Parse has the same zero time as
> time.Time{}, which it doesn't.
not a time, only a time of day.
Maybe let's start from a different
origin. What are your inputs and what should be the output of this
part of your code? Please avoid referring to any specific
implementation and/or types. That could help someone to propose a
solution hopefully.
-j
Right. So in my original algorithm, I tried to convert a time to a duration by subtracting the zero time. This didn't work as expected, because parsing "15:04" results in a negative time: this is the odd behavior I wanted to highlight. Whether I'm misusing time.Time in this specific case, I think it's reasonable to assume that time.Parse has the same zero time as time.Time{}, which it doesn't.
On Sun, Jan 13, 2013 at 11:41 PM, Ryan Sims <rws...@gmail.com> wrote:Right. So in my original algorithm, I tried to convert a time to a duration by subtracting the zero time. This didn't work as expected, because parsing "15:04" results in a negative time: this is the odd behavior I wanted to highlight. Whether I'm misusing time.Time in this specific case, I think it's reasonable to assume that time.Parse has the same zero time as time.Time{}, which it doesn't.You're making assumptions about the internals of time package but it is notdocumented.in fact, a zero time != time.Time{}, and that's why time.IsZero() exists.
if you want to extract the duration correctly, you should subtract it with a zero time (time.Parse("15:04", "0:00")),then you don't need to make the (invalid) assumption.
"The zero value of type Time is January 1, year 1, 00:00:00.000000000 UTC"
I don't see why time.Parse should play by different rules.
Yes, I *know* Parse has a different zero time, *that's why* I started this thread. I don't think it *should* have a different zero time, and I think it's confusing that it does.Let me put it this way: why *should* time.Parse("", "") be different than time.Time{}? Principle of least surprise seems to suggest they should be the same.
Parsing a time to year 0000 just seems wrong. In our calendar, there is no such year.
On Jan 13, 2013 1:04 PM, "Kyle Lemons" <kev...@google.com> wrote:
>
> On Sun, Jan 13, 2013 at 8:33 AM, Ryan Sims <rws...@gmail.com> wrote:
>>
>> On Sun, Jan 13, 2013 at 11:27 AM, minux <minu...@gmail.com> wrote:
>>>
>>>
>>> On Mon, Jan 14, 2013 at 12:10 AM, Ryan Sims <rws...@gmail.com> wrote:
>>>>
>>>> "The zero value of type Time is January 1, year 1, 00:00:00.000000000 UTC"
>>>
>>> yes, but it's not the zero time adopted by time.Parse, see below.
>>>>
>>>> http://play.golang.org/p/fVvMI3qV-u
>>>> I don't see why time.Parse should play by different rules.
>>>
>>> Quoting docs for time.Parse:
>>> // Elements omitted from the value are assumed to be zero or, when
>>> // zero is impossible, one, so parsing "3:04pm" returns the time
>>> // corresponding to Jan 1, year 0, 15:04:00 UTC.
>>
>>
>> Yes, I *know* Parse has a different zero time, *that's why* I started this thread. I don't think it *should* have a different zero time, and I think it's confusing that it does.
>>
>> Let me put it this way: why *should* time.Parse("", "") be different than time.Time{}? Principle of least surprise seems to suggest they should be the same.
>
>
> Even if it is (and I'm not saying I agree with you) it's too late to change it now.
Of course it is. I'm really not trying to be obtuse here. I think this is legitimately confusing and worth singling out. I referred to it as a "puzzler" with a nod to the Java equivalent: its not a bug to fix, its a gotcha to be aware of. My point is not that its broken, its that its inconsistent and subtle. I made some incorrect assumptions, but in my defense I think the API, while perfectly reasonable and elegant, makes it easy to make those wrong assumptions.
Of course it is. I'm really not trying to be obtuse here. I think this is legitimately confusing and worth singling out. I referred to it as a "puzzler" with a nod to the Java equivalent: its not a bug to fix, its a gotcha to be aware of. My point is not that its broken, its that its inconsistent and subtle. I made some incorrect assumptions, but in my defense I think the API, while perfectly reasonable and elegant, makes it easy to make those wrong assumptions.