Leap year calculation



  • I was reviewing code for a Visual Basic 6 application I'm maintaining when I saw this code:

          ComboDay.Clear
          For i = 0 To DaysPerMonth(iMonth)
             ComboDay.AddItem i
          Next i

    It populates a drop-down list with the number of days in a month. The DaysPerMonth array is created earlier and looks like this:

       DaysPerMonth(1) = 31
       DaysPerMonth(2) = 28
       ...
       DaysPerMonth(12) = 31

    So, what about the number of days in February in a leap year?

          If (Val(CurrentYear) Mod 4) = 0 Then DaysPerMonth(2) = 29

    The application only deals with years after 2000 but this last line of code made me cringe.


    [Moved to Sidebar where it seems more appropriate. -ShadowMod]



  • This is actually correct from the year 1901 to 2099, so it's not too much of a WTF.  Using a calendar control would have avoided the whole problem and provided a better user interface.



  • @Jaime said:

    This is actually correct from the year 1901 to 2099, so it's not too much of a WTF.  Using a calendar control would have avoided the whole problem and provided a better user interface.

    DERP!

    RARE BLAKEYRAT PEDANTIC DICKWEED POST! ALSO I WAS WRONG SO LOOKIE THERE!

    But seriously, use the built-in libraries, morons. Believe me, they are written by people smarter than you are.



  • @Jaime said:

    This is actually correct from the year 1901 to 2099, so it's not too much of a WTF.  Using a calendar control would have avoided the whole problem and provided a better user interface.

    It could be code used to calculate the daily interest on 99 year leases, in which case we are already fucked. Doing calendar stuff by hand falls under the category of "Just Don't Do It" like when somebody says "Hey, we're only storing values up to 100,000 in this 32-bit int.. I should use some of those high bits to store flags!"



  • @blakeyrat said:

    Believe me, they are written by people smarter than you are.

    That's just not possible. However, they are written by people less lazy than me, so I'm definitely in favor of using them where possible.



  • @blakeyrat said:

    @Jaime said:

    This is actually correct from the year 1901 to 2099, so it's not too much of a WTF.  Using a calendar control would have avoided the whole problem and provided a better user interface.

    DERP!

    RARE BLAKEYRAT PEDANTIC DICKWEED POST! ALSO I WAS WRONG SO LOOKIE THERE!

    But seriously, use the built-in libraries, morons. Believe me, they are written by people smarter than you are.

    2000 Mod 4 = 0 and the year 2000 was a leap year.  How is the code wrong for the year 2000?


  • @Jaime said:

    @blakeyrat said:

    @Jaime said:

    This is actually correct from the year 1901 to 2099, so it's not too much of a WTF.  Using a calendar control would have avoided the whole problem and provided a better user interface.

    DERP!

    RARE BLAKEYRAT PEDANTIC DICKWEED POST! ALSO I WAS WRONG SO LOOKIE THERE!

    But seriously, use the built-in libraries, morons. Believe me, they are written by people smarter than you are.

    2000 Mod 4 = 0 and the year 2000 was a leap year.  How is the code wrong for the year 2000?

    Wait, so you replied to him with a correction after he already admitted he was wrong?



  • @Jaime said:

    2000 Mod 4 = 0 and the year 2000 was a leap year. How is the code wrong for the year 2000?

    HEY DUMBFUCK McDICKINYOURFACE when I say "I was wrong" in the post, that actually means I was wrong. So you posting to point out that I was wrong? That's just fucking stupid and now I hate you.



  • @morbiuswilters said:

    @Jaime said:

    @blakeyrat said:

    @Jaime said:

    This is actually correct from the year 1901 to 2099, so it's not too much of a WTF.  Using a calendar control would have avoided the whole problem and provided a better user interface.

    DERP!

    RARE BLAKEYRAT PEDANTIC DICKWEED POST! ALSO I WAS WRONG SO LOOKIE THERE!

    But seriously, use the built-in libraries, morons. Believe me, they are written by people smarter than you are.

    2000 Mod 4 = 0 and the year 2000 was a leap year.  How is the code wrong for the year 2000?
    Wait, so you replied to him with a correction after he already admitted he was wrong?
    I couldn't tell if it was an admission of fault or an attempt to project derp speak on me.


  • @Jaime said:

    I couldn't tell if it was an admission of fault or an attempt to project derp speak on me.

    I usually just assume Blakey is joking or being bombastic rather than acting like an outright asshole. It's easy to set him off but it seems like people almost want him to get angry so the whole thing becomes a self-fulfilling prophecy of sorts. Seems like an alright guy to me, except for the whole pangolin thing. But I'm addicted to clown pornography so who am I to judge?



  • @Jaime said:

    This is actually correct from the year 1901 to 2099, so it's not too much of a WTF
    And what about somebody using that code in Feb. 2100?  Huh?  That's right, they're screwed!!



  •  I'm surprised no-one noticed that i starts at 0. Or did someone add 0 to every month while I was away?


  • Discourse touched me in a no-no place

    @TGV said:

     I'm surprised no-one noticed that i starts at 0. Or did someone add 0 to every month while I was away?

    It's clearly there to take account of the buggy leap-year implementation. March 0th is certain to, correctly, be Feb 29th or Feb 28th.



    Or, perhaps more prosaically, it's simply there to ensure the user actually selects something rather than defaulting to '1.'



  • @blakeyrat said:

    But seriously, use the built-in libraries, morons. Believe me, they are written by people smarter than you are.
    Well, unless you're using PHP, in which case it's a crapshoot.

     



  • @vyznev said:

    @blakeyrat said:

    But seriously, use the built-in libraries, morons. Believe me, they are written by people smarter than you are.
    Well, unless you're using PHP, in which case it's a crapshoot.

     

    Most of the built-in PHP libraries just wrap the corresponding C libraries, just like with many languages. But of course you didn't care about that, you just wanted to sound stupid.



  • @blakeyrat said:

    But seriously, use the built-in libraries, morons. Believe me, they are written by people smarter than you are.

    Maybe, but apparently some think that you don't need to keep track of dates before unix epoch in directory service. eDirectory uses unsigned 32 bit int for Generalized Time syntax. Kinda forces you to roll in your own functions for fields that require dates before it.



  • @Buzer said:

    @blakeyrat said:
    But seriously, use the built-in libraries, morons. Believe me, they are written by people smarter than you are.

    Maybe, but apparently some think that you don't need to keep track of dates before unix epoch in directory service. eDirectory uses unsigned 32 bit int for Generalized Time syntax. Kinda forces you to roll in your own functions for fields that require dates before it.

    So you think my advice is bad, because there's one specific system where it doesn't apply, even though 99.9% of programmers will go their entire career without ever touching that system?

    Or you think my advice is good and are lamenting that it doesn't work in this one case?

    Or are you just bragging about how clever you are to store pre-1970 dates in an unsigned 32-bit int where 0 is 1970? Or are you complaining about how shitty eDirectory and "Generalized Time syntax" are?

    What is the point of your post exactly? Because I don't get it.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.