4 ms·
Personally, I think there is a more subtle conceptual idea that it glosses over that makes the more explicit Java version more "clean". > D + 1m1d != D + 1d1m
by 32bitkid 11y ago
Personally, I think there is a more subtle conceptual idea that it glosses over that makes the more explicit Java version more "clean".
> D + 1m1d != D + 1d1m
Mixing time units like days, months, years (where units are intransitive) is, in my opinion, a bad idea.
- nickpsecurity 11y agoShould be able to type or sanity check them like anything else. What specific issue are you worried about?
- henrikschroder 11y ago2011-02-28 + 1m1d = 2011-03-29 2011-02-28 + 1d1m = 2011-04-01 (edit: stupid leap-year!)
- nickpsecurity 11y agoWhy does that happen? I'd think they were equivalent. I don't do time-series or anything to be clear. I would just expect the a + b = b + a principle to apply for the m/d value.
- 32bitkid 11y agobecause 1m = [28d,29d,30d,31d] depending on which month you are in. By adding 1d, pre or post, it can alter the definition of 1m.
- xixi77 11y agoSure (assuming that's how a "month" is defined in this context -- I haven't actually read the whole article, so I'm not sure); but why do you find the Java syntax less confusing? They look pretty equivalent, except for one is shorter than the other.
- henrikschroder 11y agoBy splitting the addition up into two discrete calls, the ordering is made much more explicit, there's no room for ambiguity. In C#, the largest part of a TimeSpan is a day, you can't have a TimeSpan of "one month and one day", because adding that to a DateTime would add a different amount of absolute time depending on the DateTime. You can have a TimeSpan of "32 days", and adding that to any DateTime yields a consistent result.
- nickpsecurity 11y ago1h4m is 1 hr then 4 min. Order is explicit unless language rules obfuscate it.
- nickpsecurity 11y agoIh ok. Sounds like we can keep first style just make order of operations clear and with rules to catch this. Second syntax woukd appear to require same checks. Again, equivalent intent and problems with more verbosity in 2.
- henrikschroder 11y ago2011-02-28 + 1m1d = (2011-02-28 + 1m) + 1d = 2011-03-28 + 1d = 2011-03-29 2011-02-28 + 1d1m = (2011-02-28 + 1d) + 1m = 2011-03-01 + 1m = 2011-04-01 Also, you have these overflow rules: 2011-03-31 + 1m = 2011-04-30 2011-04-30 - 1m = 2011-03-30
- TimWolla 11y agoFebruary 2012 has got 29 days.
- henrikschroder 11y agoThis is why everyone hates working with time and date functions. :-)
- nickpsecurity 11y agoOh it's been a pleasant trip down memory lane for me let me tell ya. Probably why I haven't done that sort of thing in a long time haha.