• (cs) in reply to DateTime.Now
    DateTime.Now:
    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().
    Which is why Noda Time exists. If you think that date arithmetic is simple, you are TRWTF.
  • Nobulate (unregistered)
    auth_server = auth_server_url.reverse.chomp('http://'.reverse).reverse.reverse.chomp('https://'.reverse).reverse

    Essentially, this:

    [image]
  • Nobulate (unregistered)
    // has to be a negative value

    Naturally one wants to do this in binary:

    0xFFFFFFFE   //-2
    xor 0xFFFFFFFF   //binary complement 
    ---------------
        0x00000001   //result of complement
    +   0x00000001   //add 1
    ---------------
        0x00000002   //Result of negation is 2

    And like second nature in x86 Assembly:

    Data Segment
      num dw 00000010B
    Data Ends
    
    Code Segment
      Assume cs:code, ds:data
    
      Begin:
        mov ax, data
        mov ds, ax
        mov es, ax
        mov eax, num
        NEG eax
    
      Exit:
        mov ax, 4c00h
        int 21h
    Code Ends

    Why do people insist on making things so hard for themselves? pffft, amateurs.

  • Ol' Bob (unregistered)

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

    IA!! IA!! CTHULHU FTHAGN!!!!!!!

    CATCHA: duis - whoever wrote that code was a real duis.

  • Peter Wolff (unregistered) in reply to faoileag
    faoileag:
    QJo:
    I used z because then its easier to migrate to in case we need to implement Hour as a complex number.
    That would also be a nice alternative for today's second wtf:
    if (discountTotal > 0)
    {
     discountTotal = discountTotal * (i * i);
    }
    
    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?
    Combine these, and we'll have something like
    auth_server_url = auth_server_url.Duplicate(i).Duplicate(i);
    

    There is a reason you can transform a number into a string and vice versa. As everyone knows, that's the right way to test for the sign of a number. Here in C#:

    // has to be a negative value
    string discountTotalString = discountTotal.ToString();
    if (discountTotalString.Substring(0, 1) == "-") {
      return double.Parse(discountTotalString);
    } else {
      discountTotalString = "-" + discountTotalString;
      return double.Parse(dsdiscountTotalString);
    }  
    
  • tchize (unregistered)

    Actualy, the date calculation is a way to go when you need to do something for each hour. Simply adding the difference between date and aimed hour is not enough to take into account daylight saving. (2 hours +1) can be 3hours, 2 hours or 4 hours depending on daylight saving ^^

    Of course, considering code does nothing but changing the hour, simply calling setHour(10) would have been much easier ^^

  • Maurizio (unregistered)

    What is wrong in doing arithmetics with while loops ? Never heard of Peano Arithmetics ? Essentially, give me '+1' and a while, and i can build up the whole integer arithmetics. TRWTF is to have a loop using minus one, instead of building subtraction with a loop using '+1'. (left as an exercise to the reader)

  • David Conrad (unregistered)

    I'm surprised no one commented, on the reimplementing of negating a number, that in languages where overflow throws an exception instead of being silently ignored, there are some numbers that would cause overflow instead of giving you a negative value.

    Of course, with two's-complement arithmetic and overflow ignored, you'll get the same results as you would from an unary minus operator.

  • Paul Neumann (unregistered) 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.
    Make sure that 10 =< wishDate =< 16 or you may me assigned a different wishDate.
  • Norman Diamond's cat (unregistered) in reply to Nobulate
    Nobulate:
    You can't do that.

    You'll put all those poor makers of laser pointers out of work.

    Hissssssssssssssssssssssss.

  • (cs) in reply to Matt Westwood
    Matt Westwood:
    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.

    I lied for the sake of the joke. Ellen's way too young for me too, but if I'd said someone my own age (next youngest female celebrity that people would be likely to recognize appears to be Emma Thompson) I'd have to deal with "eewww, old broad!" responses from the Gen-X and Y crowd here.

  • Alex (unregistered)

    Everyone knows this is the correct way to turn a positive number into a negative number.

        // has to be a negative value
        if (discountTotal > 0)
        {
            discountTotal = double.Parse("-" + discountTotal);
        }
  • (cs) in reply to da Doctah
    da Doctah:
    Matt Westwood:
    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.

    I lied for the sake of the joke. Ellen's way too young for me too, but if I'd said someone my own age (next youngest female celebrity that people would be likely to recognize appears to be Emma Thompson) I'd have to deal with "eewww, old broad!" responses from the Gen-X and Y crowd here.

    The "Early life" section on her Wikipedia page is a red flag.

  • (cs) in reply to Alex
    Alex:
    Everyone knows this is the correct way to turn a positive number into a negative number.
        // has to be a negative value
        if (discountTotal > 0)
        {
            discountTotal = double.Parse("-" + discountTotal);
        }

    That looks familiar.

  • Rudolf (unregistered)

    The problem with date arithmetic is that people seem to want to do it on local times. This just doesn't work.

    Hint - if you must roll your own date functions, ALWAYS use UTC internally.

    Eg, given an 'AddHour()' function, what is the result (in the UK) of

    (26 Oct 2014 02:00).AddHour(1)

    Is it (26 Oct 2014 02:00) or is it (26 Oct 2014 03:00)?

    Answer - both...

    That's why you store and calculate in UTC, and then convert to local as necessary.

    IMHO, any date class which has a 'GetHour' function is broken. There should be a 'GetUTCHour()' and a 'GetLocalHour()' function. GetHour() is ambiguous and bad design.

  • Rudolf (unregistered) in reply to Maurizio
    Maurizio:
    What is wrong in doing arithmetics with while loops ?

    I remember when multiple and divide WERE done with loops on most common CPUs around...

    (not quite repeated adds, but still loops - using the rather neat characteristics of binary arithmetic)

  • nmclean (unregistered) in reply to Rudolf
    Rudolf:
    The problem with date arithmetic is that people seem to want to do it on local times. This just doesn't work.

    Hint - if you must roll your own date functions, ALWAYS use UTC internally.

    Eg, given an 'AddHour()' function, what is the result (in the UK) of

    (26 Oct 2014 02:00).AddHour(1)

    Is it (26 Oct 2014 02:00) or is it (26 Oct 2014 03:00)?

    Answer - both...

    That's why you store and calculate in UTC, and then convert to local as necessary.

    IMHO, any date class which has a 'GetHour' function is broken. There should be a 'GetUTCHour()' and a 'GetLocalHour()' function. GetHour() is ambiguous and bad design.

    Are you sure you should ALWAYS use UTC? The WTF code seems to be trying to constrain to business hours, which can be dictated by local time. Do business hours always start exactly 10 hours after midnight and last 6 hours, or do they respect DST and begin and/or end 1 hour earlier that day? If it's the latter then we should use local time.

    The .NET DateTime structure (which the given code appears to be using) is UTC. "Hour" is always UTC hour. If you want

  • Dyspeptic Curmudgeon (unregistered)

    Or two lines of shell script

    #!/bin/bash

    auth_server is set somewhere prior to here:

    auth_server has only two versions: https: OR http:

    eg: auth_server="https://thisisanUrl.com"

    OR auth_server="http://thisisanUrl.com

    auth_server=echo ${auth_server/https:\/\//} auth_server=echo ${auth_server/http:\/\//}

    echo $auth_server etc, etc.

    C* versions are left to the student as an exercise

  • anonymous (unregistered) in reply to Paul Neumann
    Paul Neumann:
    da Doctah:
    My wishDate used to be Ellen Page. Apparently now I need to come up with a new wishDate.
    Make sure that 10 =< wishDate =< 16 or you may me assigned a different wishDate.
    Um... none of those are legal wishDates in my state of residence...
  • (cs) in reply to ammoQ
    ammoQ:
    Code operating on Date objects that works? DON'T TOUCH IT.

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

    Of course. If it ain't broke, don't fix it.

    For one, you might introduce a bug.

    You'll need someone to review your change and have a backout strategy.

    You'll need to write regression tests that will run with the build and remain doing so forever, thus slowing down the build process.

    You will need to write a document expressing the change and how the testing team can run tests against your change.

    You'll need several managers to sign it off too.

  • Assert(NULL) (unregistered) in reply to DWalker

    In Helvetica yes, in "Old English Text MT" no. You must follow your code style.

Leave a comment on “Code that Works”

Log In or post as a guest

Replying to comment #:

« Return to Article