• seebs (unregistered)

    The resulting single line of code has two bugs.

    1.  When date1 is greater than date2, it returns a positive value rather than zero.
    2.  When date2 is greater than date1, it returns a negative value instead of a positive one, although they should have the same magnitude.

    So, uhm.  Maybe try harder next time?

  • Farce (unregistered) in reply to OneFactor
    OneFactor:
    foobish:

    Anonymous:

    biziclop:

    And they'll hire you back as a highly payed contractor.

     

    No one who can't spell "paid" deserves to be highly "payed".

    Someone who doesn't understand double negatives, shouldn't discriminate on "paid" versus "payed".

    Your comment on double negatives is the least benightedly unintelligent one it has ever been my extreme displeasure not to be able to avoid reading.

    Touché!

  • Bob the Contractor (unregistered) in reply to seebs

    They must have been getting paid by the line...imagine the code they must have used to establish the date in the first place...

  • Chris (unregistered) in reply to Bob the Contractor

    I almost wrote something like this, trying to get a UNIX timestamp from a date in AppleScript. However you can just subtract "jan 1 1970 12:00 AM" from a date to get it. Nifty.

  • (cs)

    Am I the only one who really enjoys this code pattern?

    <font face="Courier New">if(condA || condB) {
      if(condA) {
      }
      if(condB) {
      }
    }</font>

  • Gene Pool Lifeguard (unregistered) in reply to ParkinT
    ParkinT:
    Anonymous:
    Anonymous:

    biziclop:

    And they'll hire you back as a highly payed contractor.

     

    No one who can't spell "paid" deserves to be highly "payed".



    And no one who corrects grammar on a WTF forum gets "layed" [sic]

    Now That is funny!  (sadly true, too)

    Sadly?  You think such people should reproduce?

  • (cs) in reply to CornedBee

    If the language supports subtracting Date objects from each other, I do hope it also supports arrays.

    You know, arrays. Useful variables for storing multiple fields of data without having to resort to int values and multiply the data fields by powers of ten to keep them separate.

    I did something comparable to year10000+month100+day on my first day of learning Java. It's one of those ideas that look brillant while you're implementing them.

  • (cs) in reply to The Anonymous Coward
    Anonymous:

    short isValidDate[99991232]; // fix the y10k bug later

    /* load in data that populates our array... probably from some sort of XML file... the first 10101 elements are 0, then the next 31 are 1, then 69 more 0's, then 28 1's... you get teh idea, right? */

    d = 0;

    for(i = d1; i < d2; i++) {d += isValidDate[i];}

    BAM!

     



    Emmm,
    short isValidDate[99991232];

    This uses 192Mb of memory when it is populated..... I think I prefer the original WTF

    (unless you were joking, which I think you were :)
  • (cs) in reply to Gene Wirchenko
    Gene Wirchenko:
    Anonymous:
    Ugh, I hate it when people use "retval" for a return value. You might as well call it "variable".


    I use it.  In a language which does not have the return value in a variable of the same name as the function, it serves the same purpose.  All I want to state is that the variable is the return value under construction.  That makes "retval" a good name (for me).

    Sincerely,

    Gene Wirchenko



    Or you could have the dubious pleasure of programming Eiffel where there is no return statement, you have to set a variable call Result to return a value. I wish I was kidding but I'm not.
  • Konrad (unregistered) in reply to Wm
    Anonymous:
    Actually this function assumes every month is 31 days so it becomes grossly inaccurate the greater the span between the two dates.
     
    I have encountered a similer problem before in a web calander application. Pressing forward one month actually moved you forward by 31 days.  This worked fine for the best part of a year and
    noone noticed untill the last week of Jnauary came around. and pressing forward one month would
    take you stright to march.
  • (cs) in reply to marvin_rabbit
    marvin_rabbit:
    Anonymous:
    Ugh, I hate it when people use "retval" for a return value. You might as well call it "variable".

    Hey, have you been looking at my code again?  You can tell it's mine because it's called 'PROGRAM.EXE'.



    At least you didn't copy the name I use, a.out.
  • (cs) in reply to Otto
    Otto:
    I've written something similar to that, except:
    a) mine worked, and
    b) the language didn't support it natively (no "Date" type).
    I'll assume that you know the difference (though you may have confused others) between what a language supports natively and what its standard or common libraries support.  It would seem rather limiting to narrow one's definition of Date type in the language, when there are so many calendar systems available to choose among around the world.
  • (cs) in reply to Eq
    Anonymous:
    Ugh, I hate it when people use "retval" for a return value. You might as well call it "variable".
    How about "result"? That's what I always call it.
    --
    I am NOT a robot
  • (cs) in reply to CornedBee
    CornedBee:
    Am I the only one who really enjoys this code pattern?

    <font face="Courier New">if(condA || condB) {
      if(condA) {
      }
      if(condB) {
      }
    }</font>
    What do you mean "enjoys"? Have you been off in the corner with this code pattern again?
  • N/K (unregistered)

    if ((dat/100 % 2)==(dat/100 / 8)) daymo -= 1;


    Despite all the wtf-ness, I must say this line is cryptic, but somewhat clever.
  • (cs) in reply to CornedBee
    CornedBee:
    Am I the only one who really enjoys this code pattern?

    <FONT face="Courier New">if(condA || condB) {
      if(condA) {
      }
      if(condB) {
      }
    }</FONT>


    <FONT face=Tahoma>the more i look at it, it seems to form some sort of a figure...



    </FONT>
  • Paula (unregistered) in reply to xrT

    The REAL WTF is this forum software.

    Seriously though, I've seen worse date functions than this written in TSQL!

  • Algirdas Kepezinskas (unregistered) in reply to seebs

    Hey, woah, it made it to front page - even though someone posted a link to the original thread, with original function (before anonymisation - it's even worse! =)

    As to answer to this comment - it might not be appearent from this function alone, but the logic where it was used didnt require it to return 0 for date1 > date2. The anonymisation swapped dates in my one-liner aswell.. besides, the original function didnt even work (i dont remember what was it anymore, but it just got days wrong).

  • Tei (unregistered) in reply to Rank Amateur
    Rank Amateur:

    That's a known bug. The original coder wanted that line to be:

    <font face="Courier New" size="2">if (date1 > date2) return -(functionName(date2, date1));</font>

    But then he heard that recursion can blow your stack and couldn't figure out how else to do it.

    --Rank



    or a swap :D

    <font face="Courier New">if (date1 > date2) {
      temp = date1;
     date1 = date2;
     date2 = temp;
    }</font>

  • Mario (unregistered)

    I could have understood this if the code converted the dates to Julian Day Numbers, and then subtracted those. Who wants to calculate the number of days in a month?

    Am I the only one who thinks this code looks like a mess -stylewise? It's so hard to read ...

    The captcha says I'm "1337" Hehe.

  • piersy (unregistered) in reply to YourName
    Anonymous:
    Anonymous:

    biziclop:

    And they'll hire you back as a highly payed contractor.

     

    No one who can't spell "paid" deserves to be highly "payed".



    And no one who corrects grammar on a WTF forum gets "layed" [sic]


    Actually, I believe he was correcting spelling, but never mind. Right I'm off to pai for my lai.
  • anonymousse (unregistered) in reply to Arancaytar

    Heh. I suppose that bad habit only became common in the 32-bit age. Anyone who began coding on 8-bit systems with 16-bit ints, would probably never do such a thing. He would of course do other silly things.

    For example, I must admit to committing stupid date follies myself. Such as packing a whole date into a 16-bit word to save space. y<<9+m<<5+d or something like that. Not exactly Y2K-compatible. OTOH it was on an 8-bit system, and space was expensive, and Y2K seemed aeons away.

  • wolf550e (unregistered)

    I bet it was originally PL/SQL. The local DBA team lead once told me a story how she cought a newbie college grad unfamiliar with the wonders of that language who implemented an algorithm that counts the difference between two days by subtracting a single day from the larger date, recursively. I completely sympthized when she told me he had his grants revoked and had to submit every schema change (on debug DB, of course) on paper to DBA team for a month. No, I can't post the code.

  • Arjen (unregistered) in reply to retman

    "...but is usually far too long to type."

    This is a tru wtf! You probably only write programs with max. 26 variables and functions. Or do you use the capitals as seperate names?

  • Mark (unregistered) in reply to emurphy

    emurphy wrote the following
    > The February "logic" has dat/100 and dat%100 switched, plus (as noted) it fails to implement the 100-year rule (granted you have to go outside 1901-2099 for that rule to matter).

    A agree with the 2099 date. The logic breaks down for dates before about1930 - you cannot use the contempory calculation for days-in-month and leap years prior to about 1930, you need to know the location to which the date applies, for the same reason that the Russian October revolution occurred in November (after which 31 January 1918 was followed by 14 February 1918). Go back a bit further and the year didn't even start on the 1st January!

    If someone was born on 2 Jun 1906, they may or may not celebrate their 100th birthday today depending on where they were born (and how they decide to handle the Julian-Gregorian switch).

    Of course you can use the Proleptic Gregorian Calendar, but this just introduces another set of problems.



  • TheDoom (unregistered) in reply to Mark
    Anonymous:
    emurphy wrote the following
    > The February "logic" has dat/100 and dat%100 switched, plus (as noted) it fails to implement the 100-year rule (granted you have to go outside 1901-2099 for that rule to matter).

    A agree with the 2099 date. The logic breaks down for dates before about1930 - you cannot use the contempory calculation for days-in-month and leap years prior to about 1930, you need to know the location to which the date applies, for the same reason that the Russian October revolution occurred in November (after which 31 January 1918 was followed by 14 February 1918). Go back a bit further and the year didn't even start on the 1st January!

    If someone was born on 2 Jun 1906, they may or may not celebrate their 100th birthday today depending on where they were born (and how they decide to handle the Julian-Gregorian switch).

    Of course you can use the Proleptic Gregorian Calendar, but this just introduces another set of problems.





    And then you've got to watch out for the yanks! With their strange date formats, I mean MMDDYYYY - what's the deal with that?

    Captcha = Whiskey.

    Not as whiskey as a bank waid!!
  • Tony (unregistered) in reply to TheDoom
    Anonymous:

    And then you've got to watch out for the yanks! With their strange date formats, I mean MMDDYYYY - what's the deal with that?



    Oh!  They just wanted a date format that you couldn't sort lexicographically.

  • schwa-sticker (unregistered) in reply to foobish
    foobish:

    Anonymous:

    biziclop:

    And they'll hire you back as a highly payed contractor.

     

    No one who can't spell "paid" deserves to be highly "payed".

    Someone who doesn't understand double negatives, shouldn't discriminate on "paid" versus "payed".

    (There does not exist a person) (who posesses the quality of being unable to spell paid) (who also deserves large monetary compensation).

    Someone who understands niether double negatives nor comma usage should discriminate(???) on double negatives.

    //captcha:clueless

  • schwa-sticker (unregistered) in reply to schwa-sticker

    *Someone who understands niether double negatives nor comma usage should NOT discriminate(???) on double negatives.

    I can't imagine why I might have trouble proofreading a sentence in a text box 6cm wide.

  • (cs)

    If the method name was EnterpriseGetWrongDateDiff I could understand it but this method just doesn't have that wrath of the almighty feel to it without some XML.  I mean seriously, how can we be expected to make fun of enterprise methods without at least one connection to BizTalk with some XML tossed into the mix.

    So lets rename it EnterpriseGetWrongDateDiffBizTalkToXMLBackToInt(date a, date b) and be done with it.

  • (cs) in reply to Mark
    Anonymous:
    emurphy wrote the following
    > The February "logic" has dat/100 and dat%100 switched, plus (as noted) it fails to implement the 100-year rule (granted you have to go outside 1901-2099 for that rule to matter).

    A agree with the 2099 date. The logic breaks down for dates before about1930 - you cannot use the contempory calculation for days-in-month and leap years prior to about 1930, you need to know the location to which the date applies, for the same reason that the Russian October revolution occurred in November (after which 31 January 1918 was followed by 14 February 1918). Go back a bit further and the year didn't even start on the 1st January!

    If someone was born on 2 Jun 1906, they may or may not celebrate their 100th birthday today depending on where they were born (and how they decide to handle the Julian-Gregorian switch).

    Of course you can use the Proleptic Gregorian Calendar, but this just introduces another set of problems.



    And yet again, history (and the calendar) is the real WTF...

  • Devildog (unregistered) in reply to xrT
    xrT:
    CornedBee:
    Am I the only one who really enjoys this code pattern?

    <font face="Courier New">if(condA || condB) {
      if(condA) {
      }
      if(condB) {
      }
    }</font>


    <font face="Tahoma">the more i look at it, it seems to form some sort of a figure...



    </font>



    This ^ kinda looks like an old Luger...
  • Cheatz (unregistered) in reply to Rank Amateur
    Rank Amateur:
    That's a known bug. The original coder wanted that line to be:

    <font face="Courier New" size="2">if (date1 > date2) return -(functionName(date2, date1));</font>

    But then he heard that recursion can blow your stack and couldn't figure out how else to do it.

    --Rank


    Argh!
    I think that in this particular case a conditional statement is nonsense.

    I hope the programming language supports abs() or Math.abs or something like that.
    The original functionality (zero if first date is later than second) would be max ( 0, date2 - date1 ).

  • (cs) in reply to Tei
    Anonymous:
    Rank Amateur:

    That's a known bug. The original coder wanted that line to be:

    <FONT face="Courier New" size=2>if (date1 > date2) return -(functionName(date2, date1));</FONT>

    But then he heard that recursion can blow your stack and couldn't figure out how else to do it.

    --Rank



    or a swap :D

    <FONT face="Courier New">if (date1 > date2) {
      temp = date1;
     date1 = date2;
     date2 = temp;
    }</FONT>

    No joy. He also heard that only losers do bubble sorting.

    --Rank

  • ChiefCrazyTalk (unregistered) in reply to Eq

    Anonymous:
    Ugh, I hate it when people use "retval" for a return value. You might as well call it "variable".

     

    I like that.  Makes it clear that the variable is "special" and holds the return value.  the function definition and comments should make it clear what it does.

  • The Anonymous Coward (unregistered) in reply to thebilliardplayer

    thebilliardplayer:
    short isValidDate[99991232];

    This uses 192Mb of memory when it is populated..... I think I prefer the original WTF

    (unless you were joking, which I think you were :)

    What the....

    Yes, of course I was joking.  The memory usage is about 1/3 of the joke; the O(N) algorithm (N is length of interval) where O(1) would do is the next 1/3 (although that is shared by teh original WTF if you generalize it); and the gratuitous use of an external XML resource is the final 1/3.

    The code is nice and compact, though :)

    The captcha is enterprisey...

  • Code Commando (unregistered) in reply to The Anonymous Coward

    Is that big O notation? I guess there are a few real programmers left in the world.

  • Anonymous Too (unregistered) in reply to biziclop

    Yeah, you should be paiing more attention!

  • (cs)

    <FONT face=Verdana size=2>Not quite as original as the above, but this was sprinkled liberally throughout an application that we contracted this guy to help with.</FONT>

    <!-- code formatted by http://manoli.net/csharpformat/ --><FONT face=Verdana size=2>if(!Page.IsPostBack)
        {
            DateTime today = System.DateTime.Today;
            DateTime tomorrow = System.DateTime.Today;
            string compTom = Convert.ToString(tomorrow.AddDays(+1));
            string thisDay = Convert.ToString(today);
            string onlyDate = thisDay.Substring(0,10);
            string onlyDays= compTom.Substring(0,10);
            this.dpFromDate.Text = onlyDate.ToString();
            this.dpToDate.Text = onlyDays.ToString();
        }
        else
        {
            DateTime tomorrow = System.DateTime.Today;
            string compTom = Convert.ToString(tomorrow.AddDays(+1));
            string onlyDays= compTom.Substring(0,10);
            this.dpToDate.Text = onlyDays.ToString();
        }</FONT>

    <FONT face=Verdana size=2>All to accomplilish adding 1 to the current date and placing it in the next textbox. And, since this was written in October, the algorithm only works on 2-digit months. We didn't find out about this bug until Janurary rolled around and the client began calling with complaints about the date formatting.</FONT>

    <FONT face=Verdana size=2>Of course, he didn't even include it in a library. We counted at least 30 pages where this was implemented. We've left it commented out in a couple as a reminder to the great Fernando.</FONT>

  • (cs) in reply to GreenLight
    GreenLight:
    Anonymous:
    Ugh, I hate it when people use "retval" for a return value. You might as well call it "variable".
    How about "result"? That's what I always call it.
    --
    I am NOT a robot

    I use "out" cause it only takes 3 keystrokes

  • valdis (unregistered) in reply to makomk
    makomk:
    Anonymous:
    Using unix time_t and taking the difference / (60*60*24)  will be slightly wrong because of leap-seconds.  The library functions that gave you the time_t will have taken leap-seconds into account.

    IIRC, the definition of Unix time values requires that leap seconds are ignored - each day is treated as though it lasts exactly 86400 seconds (even if it isn't really). Simplifies things a lot...



    That one's not Unix's fault, it's carved into the POSIX standard:

    It is inappropriate to require that a time represented as seconds since
    the Epoch precisely represent the number of seconds between the referenced
    time and the Epoch. -IEEE Std 1003.1b-1993 (POSIX)

    Now there is a WTF.  Now you know why early GNU programs checked for an environment
    variable 'POSIX_ME_HARDER' (now sanitized to POSIXLY_CORRECT).

  • Incen (unregistered)

    Here's how I see the code, with suggested fixes:

    // a function to count the days from date1 to date2, without regard to years
    private< int ?????? (Date date1, Date date2)
    {
    int retval = 0;
    if (date1 > date2) return 0;

    int d1 = date1.Year * 10000 + date1.Month * 100 + date1.Day;
    int d2 = date2.Year * 10000 + date2.Month * 100 + date2.Day;
    int dat = d1 / 100;
    int a = 0, d = 0;

    // until all intervening months or one year have been counted
    while ((dat <= (d2 / 100)) && (a < 12))
    {
    // first assume 31 days
    int daymo = 31;
    // if february, reduce days by 3 or 2 depending on leap yearage.
    // does not account for the 100 year rule.
    // if (dat%100 == 2) if ((dat/100 %4)==0) daymo -= 2; else daymo -= 3;
    if (dat/100 == 2) if ((dat%100 %4)==0) daymo -= 2; else daymo -= 3;
    // account for 30-day months
    // else if ((dat/100 %2)==(date1.Month/8)) daymo -= 1;
    if ((dat/100 % 2)==(dat/100 / 8)) daymo -= 1;

    // first and last iteration
    if ((d1/100 == dat) || (d2/100 == dat))
    {
    // the days to month end, inclusive
    if (d1/100 == dat) d = daymo - (d1%100)+1;
    // the days from month start, exclusive
    if (d2/100 == dat)
    {
    // if only one iteration will occur
    if (d1/100 == d2/100) d -= daymo - d2%100;
    // multiple iterations
    else d = d2%100;
    }
    }
    // all other iterations
    else d = daymo;
    // store this month's results
    retval += d;

    // move to next year's january from december
    if (dat%100 == 12) dat = (dat/100 + 1)*100 + 1;
    // move to next month from other months
    else dat += 1;

    // increase iteration counter
    a +=1;
    }

    return retval;
    }
  • (cs) in reply to masklinn
    masklinn:
    GreenLight:
    Anonymous:
    Ugh, I hate it when people use "retval" for a return value. You might as well call it "variable".
    How about "result"? That's what I always call it.
    --
    I am NOT a robot

    I use "out" cause it only takes 3 keystrokes



    out is a reserved work in some languages. My personal preference is either ret or retter... Don't know the story behind adding ter to ret...
  • Anonymous (unregistered) in reply to thebilliardplayer
    thebilliardplayer:
    Anonymous:

    short isValidDate[99991232]; // fix the y10k bug later

    /* load in data that populates our array... probably from some sort of XML file... the first 10101 elements are 0, then the next 31 are 1, then 69 more 0's, then 28 1's... you get teh idea, right? */

    d = 0;

    for(i = d1; i < d2; i++) {d += isValidDate[i];}

    BAM!

     



    Emmm,
    short isValidDate[99991232];

    This uses 192Mb of memory when it is populated..... I think I prefer the original WTF

    (unless you were joking, which I think you were :)


    That's easy to fix, just use a std::vector<bool>.

    (one of C++'s WTFs: std::vector<bool> is completely different from any other std::vector)

    CAPTCHA: genius
  • Gene Pool Lifeguard (unregistered) in reply to Code Commando

    Anonymous:
    Is that big O notation? I guess there are a few real programmers left in the world.

    Although in standard Big-O notation, where n is the length of the input data, the algorithm is a much scarier-sounding O(2^n).  I would describe that as, "Impressively Bad."

  • (cs) in reply to valdis
    Anonymous:
    makomk:
    Anonymous:
    Using unix time_t and taking the difference / (60*60*24)  will be slightly wrong because of leap-seconds.  The library functions that gave you the time_t will have taken leap-seconds into account.

    IIRC, the definition of Unix time values requires that leap seconds are ignored - each day is treated as though it lasts exactly 86400 seconds (even if it isn't really). Simplifies things a lot...



    That one's not Unix's fault, it's carved into the POSIX standard:

    It is inappropriate to require that a time represented as seconds since
    the Epoch precisely represent the number of seconds between the referenced
    time and the Epoch. -IEEE Std 1003.1b-1993 (POSIX)

    Now there is a WTF.  Now you know why early GNU programs checked for an environment
    variable 'POSIX_ME_HARDER' (now sanitized to POSIXLY_CORRECT).

    I suspect this particular one probably predates even POSIX (i.e. dates back to the original Unix) and changing it would just break far too much stuff...

  • (cs) in reply to N/K
    Anonymous:

    if ((dat/100 % 2)==(dat/100 / 8)) daymo -= 1;



    Despite all the wtf-ness, I must say this line is cryptic, but somewhat clever.

    How is this clever? If the eveness of the year matches the year divisible by 8 we will subtract 1 from the day?

  • (cs) in reply to Bus Raker
    Bus Raker:

    Maybe this is another case of being payed (as well as layed) by the line.  And who can really trust the built in date calculations?  If you want to be REALLY sure...

    If the calendar ever changed (once the terrorists take over the world) his code will be easily (well not quite) maintained and my date2 - date1 will need extra code to compensate for the changes.


    Isn't the code meant to be easily mayntayned?
  • MatveiMS (unregistered)

    Perhaps the developer just wanted to make a function flexible enough to compensate for relativistic time dilation in case a nearby black hole alters the gravitational field in the spacetime continuum adjacent to contemporary Solar Sytem. This developer had a vision! A forward-thinking type, you know.

  • ledlogic (unregistered) in reply to MatveiMS

        I think I understand the context.  This is used to compute interest due on a mortgage/loan credit account.  The financial institution uses the other algorithm for interest earned for the account.

    1.  Install java and ANT
    2.  ?????
    3.  Profit!

Leave a comment on “The Trouble with Blind Dates”

Log In or post as a guest

Replying to comment #:

« Return to Article