• v (unregistered)

    Now, maybe you know enough arithmetic to calculate differences without while loops

    Eh, not right around daylight savings time transitions, I don't. The loops are quite OK. Especially if setHours is really deprecated, as my quick google-fu seems to show.

  • foo AKA fooo (unregistered) in reply to neveralull
    neveralull:
    One of the tricks I taught my dog in doggie training is "reverse" You say "Reverse, reverse, reverse, reverse," and each time the dog takes a step backwards you throw him a treat.
    Now teach him "chomp" and send a video.
  • Wukl (unregistered) in reply to Cantabrigian
    Cantabrigian:
    OzPeter:
    // has to be a negative value if (discountTotal > 0) { discountTotal = discountTotal - (discountTotal * 2); }

    // has to be a negative value if (discountTotal > 0) { discountTotal = -1;
    }

    See, now it's a negative value, as per spec. No arithmetic needed!

    I'd go with discountTotal = 1 - (2 * 1);

  • Guestimate (unregistered) in reply to MightyM
    MightyM:
    If you look at the code closely you see that the return value of the AddHours method is assigned back to the original variable.
    Blimy! I wholly overlooked that tidbit. :-\

    So, something else instead ? Like a "wishDate.SetHours(..)" method ? If not, any (logical) reason to its absense ? I guess it's not available, as I've not seen anyone even hinting at it.

  • (cs) in reply to Guestimate
    Guestimate:
    So, something else instead ? Like a "wishDate.SetHours(..)" method ? If not, any (logical) reason to its absense ? I guess it's not available, as I've not seen anyone even hinting at it.

    Not having setable fragments of a DateTime is just common sense - it makes sure you don't have to perform validation on every change (e.g when someone tries to set Days to 30 when Month is February). This way you have to manipulate DateTime objects consistently regardless of which fragment you're changing.

    And yes, DateTime is really hard to implement nicely.

    EDIT: although I'm sad we've gone through more than 50 comments already and no-one's done a "Limitations? On WISHES?" yet.... sigh

  • Guestimate (unregistered) in reply to Neil
    Neil:
    Kabi:
    wishDate = wishDate.AddHours(10 - wishDate.Hour);
    Sadly this goes wrong at (say) midnight on a DST change day.
    What about this (awfull, round-about) way instead:
      wishDate = wishDate.AddHours(-wishDate.Hour).AddHours(10);
    ?

    Its ugly as sin ofcourse, but it gets the job done if there is no better method for it available.

  • CigarDoug (unregistered) in reply to Anon
    Anon:
    Guestimate:
    I don't quite get the "basic calculus" of the first example: Whats wrong with simply, on below or above, setting the hour to the apropriate limit ?
    if (wishDate.Hour < 10) {
      wishDate.Hour = 10
    } else if (wishDate.Hour > 16) {
      wishDate.Hour = 16
    }

    Having said that, I do hope that whomever programmed that realizes that the "16" check means that the wishDate can go upto 16.59 (almost 5 o'clock).

    Or, if rounding is implicitily applied, the result can range from 9.30 upto 16.29 ...

    Congratulations! You just explained the whole joke without even realizing it.

    Guestimate:
    You know, this DailyWTF is the worst developer forum I have ever been on! It has more wrong answers than TechNet!
  • (cs) in reply to Guestimate
    Guestimate:
    Neil:
    Kabi:
    wishDate = wishDate.AddHours(10 - wishDate.Hour);
    Sadly this goes wrong at (say) midnight on a DST change day.
    What about this (awfull, round-about) way instead:
      wishDate = wishDate.AddHours(-wishDate.Hour).AddHours(10);
    ?

    Its ugly as sin ofcourse, but it gets the job done if there is no better method for it available.

    How is that any different mathematically than the solution that you're trying to fix?

  • (cs)
    auth_server = auth_server_url.reverse.chomp('http://'.reverse).reverse.reverse.chomp('https://'.reverse).reverse
    

    So is this a submission from a web version of Pac-Man?

  • Guestimate (unregistered) in reply to JimM
    JimM:
    Not having setable fragments of a DateTime is just common sense - it makes sure you don't have to perform validation on every change (e.g when someone tries to set Days to 30 when Month is February).
    That sounds like logic, but really isn't.

    I mean, if you think that is true what about adding something to a date resulting in one (or more) of its fragments rolling over ? The check for, and adjustment of that would be the same for setting something. Or do I see that wrong ?

  • anonymous (unregistered) in reply to Guestimate
    Guestimate:
    Neil:
    Kabi:
    wishDate = wishDate.AddHours(10 - wishDate.Hour);
    Sadly this goes wrong at (say) midnight on a DST change day.
    What about this (awfull, round-about) way instead:
      wishDate = wishDate.AddHours(-wishDate.Hour).AddHours(10);
    ?

    Its ugly as sin ofcourse, but it gets the job done if there is no better method for it available.

    That will fail any time between midnight and just before 2 AM when the time changes on a date when DST begins/ends.
  • (cs)

    Why wouldn't you use strings for negative numbers?

    if (discountTotal > 0) 
    { 
       discountTotal = double.Parse(("-" + discountTotal.ToString()));
    }
    
  • nmclean (unregistered) in reply to Guestimate
    Guestimate:
    JimM:
    Not having setable fragments of a DateTime is just common sense - it makes sure you don't have to perform validation on every change (e.g when someone tries to set Days to 30 when Month is February).
    That sounds like logic, but really isn't.

    I mean, if you think that is true what about adding something to a date resulting in one (or more) of its fragments rolling over ? The check for, and adjustment of that would be the same for setting something. Or do I see that wrong ?

    Are you suggesting that (Feb 5).SetDay(30) should result in a value of (March 2)?

  • (cs) in reply to Warren
    Warren:
    OK, I see the problem! You don't need the ifs, because the while loops themselves test at each iteration!

    +1

    Except... think of all the overhead you save by not invoking the while loop when the if condition isn't met!

  • anonymous (unregistered) in reply to nmclean
    nmclean:
    Guestimate:
    JimM:
    Not having setable fragments of a DateTime is just common sense - it makes sure you don't have to perform validation on every change (e.g when someone tries to set Days to 30 when Month is February).
    That sounds like logic, but really isn't.

    I mean, if you think that is true what about adding something to a date resulting in one (or more) of its fragments rolling over ? The check for, and adjustment of that would be the same for setting something. Or do I see that wrong ?

    Are you suggesting that (Feb 5).SetDay(30) should result in a value of (March 2)?

    If constructing a date from (Feb 30) results in a value of (March 2), then yes.

  • Guestimate (unregistered) in reply to orokanasaru
    orokanasaru:
    How is that any different mathematically than the solution that you're trying to fix?
    Ehrmm ... You're right.

    Somehow I was thinking about hour-values not ordered in a logical sequential order when using a 12-hour clock, and somehow thought that would solve it. I guess I got caught up in my own thoughts again. I can only blame a lack of coffee.

  • (cs)

    How many programmers does it take to do Date/Time calculations?

    Apparently quite a few, and they ALL get it wrong. On the other hand, simple arithmetic is way out of bounds for some people's comprehension. The learning of simple identities from 6th grade math was the part of class many slept through.

    Life goes on (SIGH) Now=1393349503.

  • Guestimate (unregistered) in reply to nmclean
    nmclean:
    Are you suggesting that (Feb 5).SetDay(30) should result in a value of (March 2)?
    Do you think that (Feb 5).AddDay(15) should result in (March 2) ? If so, than yes.

    Question: Why should .SetDay() do something different than .AddDay() when encountering an impossible result ?

  • (cs) in reply to faoileag
    faoileag:
    Guestimate:
    then whishDate.Hour will end up holding 17.
    I get the feeling you either have never seen a ">=" comperator, or you're trolling ...
    Argh. Sorry, my mistake. No, not trolling. I don't know what I had been thinking. I think I'll better get myself another coffee.
    Guestimate:
    orokanasaru:
    How is that any different mathematically than the solution that you're trying to fix?
    Ehrmm ... You're right.

    Somehow I was thinking about hour-values not ordered in a logical sequential order when using a 12-hour clock, and somehow thought that would solve it. I guess I got caught up in my own thoughts again. I can only blame a lack of coffee.

    TDWTF needs a new sponsor...

  • (cs) in reply to Guestimate
    Guestimate:
    nmclean:
    Are you suggesting that (Feb 5).SetDay(30) should result in a value of (March 2)?
    Do you think that (Feb 5).AddDay(15) should result in (March 2) ? If so, than yes.

    Question: Why should .SetDay() do something different than .AddDay() when encountering an impossible result ?

    You can see why AddDay is more intuitive than SetDay. I can see the (pseudo)code/unit test:

    (April 5).SetDay(31) Assert(DateObject.Month == 4)

  • Guestimate (unregistered) in reply to chubertdev
    chubertdev:
    You can see why AddDay is more intuitive than SetDay.
    Yeah, I can't but to agree with you there.

    The date object could ofcourse just throw an error when it detects a faulty argument, but I thought that automatically adjusting it would possibly be more usefull.

    Maybe the SetXxxx method could have a default "throw error" setting, using a second, not required argument to overrule it (or vise-verse).

  • Beast (unregistered) in reply to nmclean
    nmclean:
    Are you suggesting that (Feb 5).SetDay(30) should result in a value of (March 2)?

    I would suggest that (Feb 5).SetDay(30) should result in a value of (Feb 30). Of course I would also expect (Feb 30).IsValid() to return false. Alternatively it could throw ArgumentOutOfRangeException just like DateTime(2014, 2, 30) does.

    Still I don't see the connection of not having a SetDay and not having a SetHour. That the first could yield invalid dates does not automatically mean the second one would do so, too. Or are there any days, where the hour is not in the range [0..23]?

  • mrputter (unregistered) in reply to chubertdev
    chubertdev:
    auth_server = auth_server_url.reverse.chomp('http://'.reverse).reverse.reverse.chomp('https://'.reverse).reverse
    
    So is this a submission from a web version of Pac-Man?

    Nope. "Twitch plays DateTime programming."

  • (cs) in reply to Guestimate
    Guestimate:
    chubertdev:
    You can see why AddDay is more intuitive than SetDay.
    Yeah, I can't but to agree with you there.

    The date object could ofcourse just throw an error when it detects a faulty argument, but I thought that automatically adjusting it would possibly be more usefull.

    Maybe the SetXxxx method could have a default "throw error" setting, using a second, not required argument to overrule it (or vise-verse).

    Yeah, but I think that would make the code even more convoluted and harder to maintain. Much better to have Add(TimePart) instead of Set(TimePart). You could say that it's relatively better. And I definitely do agree that it's more useful, so if I want a specific date and/or time, I construct one, but if I want to change an existing one, I make the change to the existing value, and let the compiler figure out where that goes.

  • nmclean (unregistered) in reply to Guestimate
    Guestimate:
    nmclean:
    Are you suggesting that (Feb 5).SetDay(30) should result in a value of (March 2)?
    Do you think that (Feb 5).AddDay(15) should result in (March 2) ? If so, than yes.

    Question: Why should .SetDay() do something different than .AddDay() when encountering an impossible result ?

    You should not expect the same behavior because the meaning of the language is different. If someone tells you to "add days" to, say, a deadline, you will naturally adjust the other components accordingly, because that's what it means to add days. It does not mean "set the Day field to the current Day field's value plus 25".

    Question: Do you think that (twenty-five plus nine) should result in (twenty-fourteen)? If so, then yes, I think that (Feb 5).AddDays(25) should result in Feb 30.

  • (cs) in reply to anonymous
    anonymous:
    nmclean:
    Guestimate:
    JimM:
    Not having setable fragments of a DateTime is just common sense - it makes sure you don't have to perform validation on every change (e.g when someone tries to set Days to 30 when Month is February).
    That sounds like logic, but really isn't.

    I mean, if you think that is true what about adding something to a date resulting in one (or more) of its fragments rolling over ? The check for, and adjustment of that would be the same for setting something. Or do I see that wrong ?

    Are you suggesting that (Feb 5).SetDay(30) should result in a value of (March 2)?

    If constructing a date from (Feb 30) results in a value of (March 2), then yes.
    Unless it's a leap year, in which case Feb 30 -> Mar 1 (dates are hard!)

  • foo AKA fooo (unregistered) in reply to Beast
    Beast:
    nmclean:
    Are you suggesting that (Feb 5).SetDay(30) should result in a value of (March 2)?

    I would suggest that (Feb 5).SetDay(30) should result in a value of (Feb 30). Of course I would also expect (Feb 30).IsValid() to return false. Alternatively it could throw ArgumentOutOfRangeException just like DateTime(2014, 2, 30) does.

    Still I don't see the connection of not having a SetDay and not having a SetHour. That the first could yield invalid dates does not automatically mean the second one would do so, too. Or are there any days, where the hour is not in the range [0..23]?

    The question should be: are there any days, where any hour in the range [0..23] doesn't make a valid DateTime, and the answer is of course, yes. (If you don't get it, read the comments. This specific issue has been mentioned several times by now.)

  • anonymous (unregistered) in reply to snoofle
    snoofle:
    anonymous:
    nmclean:
    Guestimate:
    JimM:
    Not having setable fragments of a DateTime is just common sense - it makes sure you don't have to perform validation on every change (e.g when someone tries to set Days to 30 when Month is February).
    That sounds like logic, but really isn't.

    I mean, if you think that is true what about adding something to a date resulting in one (or more) of its fragments rolling over ? The check for, and adjustment of that would be the same for setting something. Or do I see that wrong ?

    Are you suggesting that (Feb 5).SetDay(30) should result in a value of (March 2)?

    If constructing a date from (Feb 30) results in a value of (March 2), then yes.
    Unless it's a leap year, in which case Feb 30 -> Mar 1 (dates are hard!)
    Eh, it worked for what I wanted it to prove. If the date should roll over to the next month for one, it should roll over to the next month for both. Assume, for the sake of argument, that I was talking about a year that isn't leap year (or used a month other than February to make my example).

  • cowardly man (unregistered)

    Obivously it is: Math.min(discountTotal-2discountTotal, 2discountTotal-discountTotal);

  • Beast (unregistered) in reply to foo AKA fooo
    foo AKA fooo:
    The question should be: are there any days, where any hour in the range [0..23] doesn't make a valid DateTime, and the answer is of course, yes.

    True. What does (Day before DST switch 02:30).AddDays(1) return? What about the occasional leap second?

    Of course there's always a (small) chance you'll end up with an invalid date. With days this is potentially more common than with hours (as long as you use integers within the reasonable 1..31 / 0..23 ranges), but why would this keep you from adding a perfectly useful function to the interface?

    As I wrote above: You could also end up with an invalid date/an exception with DateTime's constructor, so why would you leave out functions because they could have the same result?

    On a side note: Banks actually use 30 days for all months when calculating prorated interests.

  • (cs) in reply to snoofle
    snoofle:
    Unless it's a leap year, in which case Feb 30 -> Mar 1 (dates are hard!)

    If dates remain hard for you for more than four hours, please consult a doctor.

  • (cs) in reply to foo AKA fooo
    foo AKA fooo:
    no laughing matter:
    Obvious troll is obvious!
    Don't think so, it's actually a bit subtle. The key is that object variables are actually references, so when the result of AddHours is assigned, not the object is modified, but the reference is set to a new object created by this function.
    He claims he looked up the meaning of "immutable" on Wikipedia but was unable to perform a Google search on ".Net DateTime AddHours"?

    Addendum (2014-02-25 13:53): At least that was my reasoning that led me to this conclusion.

    Now having read the other answers my conclusion is: "With a sufficiently advanced lack of coffee any programmer is indistinguishable from a troll".

  • DWalker (unregistered)

    I just saw two reverses in a row and wondered what the h&ll was going on. Maybe trying to make the string get dizzy?

  • (cs) in reply to DWalker
    DWalker:
    I just saw two reverses in a row and wondered what the h&ll was going on. Maybe trying to make the string get dizzy?

    String.YouSpinMeRightRoundBabyRightRoundLikeARecordBabyRightRoundRoundRound()

  • (cs) in reply to no laughing matter
    no laughing matter:
    foo AKA fooo:
    no laughing matter:
    Obvious troll is obvious!
    Don't think so, it's actually a bit subtle. The key is that object variables are actually references, so when the result of AddHours is assigned, not the object is modified, but the reference is set to a new object created by this function.
    He claims he looked up the meaning of "immutable" on Wikipedia but was unable to perform a Google search on ".Net DateTime AddHours"?
    If Boolean can have the value FILE_NOT_FOUND, it stands to reason that you would never ever assume that immutable is always immutable!
  • (cs) in reply to chubertdev
    chubertdev:
    snoofle:
    Unless it's a leap year, in which case Feb 30 -> Mar 1 (dates are hard!)

    If dates remain hard for you for more than four hours, please consult a doctor.

    We already have dates that roll over to the next month in this comment thread!

  • DateTime.Now (unregistered) in reply to Beast
    Beast:
    foo AKA fooo:
    The question should be: are there any days, where any hour in the range [0..23] doesn't make a valid DateTime, and the answer is of course, yes.

    True. What does (Day before DST switch 02:30).AddDays(1) return? What about the occasional leap second?

    DST and leap seconds have nothing to do with it. AddDays(1) will always return a new DateTime object that represents exactly 24 hours elapsed time from the current objects time. DST and leap seconds are a problem for the code that needs to convert DateTime's internal representation to a local time representation, e.g. ToString().

    From the Mono implementation of AddDays:

    public DateTime AddDays (double value)
    {
    	return AddMilliseconds (Math.Round (value * 86400000));
    }

    Dates are hard. Don't make them harder than they need to be.

  • (cs) in reply to snoofle
    snoofle:
    If Boolean can have the value FILE_NOT_FOUND, it stands to reason that you would never ever assume that immutable is always immutable!
    Well, actually Java's unmodifiableCollection "returns an unmodifiable view of the specified collection".

    Which means if you keep a reference to the original Collection, you can modify the original Collection and will see those changes in the view!

  • (cs)

    TRWTF is the umpteenth long argument about ways around using standard DateTime libraries, replete with "dates are hard to program" complaints and "what about THIS corner case" responses.

    Can't we find something new to argue about?

  • PC Amok (unregistered)
    auth_server = auth_server_url.reverse.chomp('http://'.reverse).reverse.reverse.chomp('https://'.reverse).reverse

    Perhaps it was 80s, New Wave day at the office when that little gem was created.

  • (cs)

    My wishDate used to be Ellen Page. Apparently now I need to come up with a new wishDate.

  • Marvin the Martian (unregistered) in reply to ammoQ
    ammoQ:
    Code operating on Date objects that works? DON'T TOUCH IT.

    (this comment was sponsored by java.util.Date)

    That's exactly what he did, he wouldn't touch these mocha utilimajiggies, and wrote a very brief and clear alternative.

    So what gives?

  • ¯\(°_o)/¯ I DUNNO LOL (unregistered) in reply to Paul Neumann
    Paul Neumann:
    ...
      ToString().Reverse().Chomp("PM".Reverse).
      Reverse().Reverse().Chomp("AM".Reverse).
      Reverse.Reverse.Chomp("M").
      ToUpper();
    FTFY (No need to reverse the "M".)
  • (cs) in reply to da Doctah
    da Doctah:
    My wishDate used to be Ellen Page. Apparently now I need to come up with a new wishDate.

    Bah. A mere child. I prefer Elaine Page myself.

  • DWalker (unregistered) in reply to ¯\(°_o)/¯ I DUNNO LOL
    ¯\(°_o)/¯ I DUNNO LOL:
    Paul Neumann:
    ...
      ToString().Reverse().Chomp("PM".Reverse).
      Reverse().Reverse().Chomp("AM".Reverse).
      Reverse.Reverse.Chomp("M").
      ToUpper();
    FTFY (No need to reverse the "M".)

    Because if you reverse an M it still looks like an M, right?

  • Friedrice The Great (unregistered) in reply to Paul Neumann
    Paul Neumann:
    Time and Date again, we all see that days are hard. For some reason people won't use the logical solution of seconds since Midnight January 1, 1970 Pacific Daylight Time as a sensible standard.

    Because dates and thyme are so hard, so the solution must be equally hard. It takes a well seasoned developer like myself to craft around all of the corner and edge cases that appear. The proper solution to today's problem is:

    Parsley, sage, dates and thyme.
  • default_ex (unregistered) in reply to foo AKA fooo
    foo AKA fooo:
    no laughing matter:
    Guestimate:
    ... because there DateTime objects are immutable.
    I just looked up the meaning of that word, as I wanted to make very sure I understood what it ment. And whatdoyouknow, I did. At least, according to Wikipedia. :-)

    But that creates, to me, a problem: How can an object be considered immutable when you can change its outward appearance using the objects own methods ?

    Or, to put it differently: Why should such a date object be allowed to be changed using an addition method, but not by using a setter method. Whats the logic behind it ?

    Obvious troll is obvious!
    Don't think so, it's actually a bit subtle. The key is that object variables are actually references, so when the result of AddHours is assigned, not the object is modified, but the reference is set to a new object created by this function.

    .NET if far more complex than that. Whether or not an object is actually mutable to the point where changing it's values results in the generation of a new object depends entirely on what the compiler thinks your trying to do. The trick to writing a high performance math library in .NET languages is being incredibly careful, as if walking on glass, to allow it to do in-place modification without triggering the mechanisms that do a create-and-replace.

  • (cs) in reply to default_ex
    default_ex:
    .NET if far more complex than that. Whether or not an object is actually mutable to the point where changing it's values results in the generation of a new object depends entirely on what the compiler thinks your trying to do. The trick to writing a high performance math library in .NET languages is being incredibly careful, as if walking on glass, to allow it to do in-place modification without triggering the mechanisms that do a create-and-replace.

    Yeah, I can't remember how many times that someone has incorrectly guessed at what the compiler would do. As a (non-real world) example:

    string name = "First" + " " + "Second";

    Isn't as bad as it appears thanks to string interning.

  • Anonymous Coward (unregistered)

    Did they test that reversing stripping code on URLs like "http://thpppt.example.com/"?

  • q.kontinuum (unregistered)

    My protocol-chopper works like any protocol chopper, except in the chomper I flip it, reverse it and take the protocol down.

    Sponsored by https://xkcd.com/153/

Leave a comment on “Code that Works”

Log In or post as a guest

Replying to comment #:

« Return to Article