Is your code ready for the leap year?

Discussion in 'Mixed Languages' started by lobo11, Feb 4, 2016.

  1. lobo11

    lobo11 TOMAHAWK CHOP

    Feb 16, 2012
    6,539
    4,989
    210
    As we enter February, it's a good time to remember that 2016 is a leap year. For most people, this may just be an interesting oddity; an extra day to work or play. But for software developers, the leap year can cause significant pain.

    If you are just now thinking about checking your code for leap year bugs, you better move quickly. In fact, you might already be experiencing the effects and may not even realize it! What kind of bugs might be lurking in your code?
    •Off-by-one issues, especially around data filtering using date-range queries
    •Unexpected or undesired behavior in user interfaces
    •Potential for exceptions, crashes, or hanging as edge cases are encountered

    “Meh,” you say. “My code is just fine. We have unit tests.”

    “Oh really,” I say. “Do your tests properly mock the clock? Do they test edge cases including February 29 and December 31? Have you tested any low-level C++ code you might have as well as the rest of your system? Do you even know what a leap year bug looks like?”

    Most often, blank stares.
    https://azure.microsoft.com/en-us/blog/is-your-code-ready-for-the-leap-year/
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. JFKI

    JFKI MDL Expert

    Oct 25, 2015
    1,098
    370
    60
    :)

    Back when I got my first copy of Turbo Pascal circa 1992 or 3 the first thing I did was write a routine for exactly that, figuring out leap years, which was incorporated into every other program I have written since.
    That was back when I was still running DOS 3.1 for an operating system.
     
  3. MrMagic

    MrMagic MDL Guru

    Feb 13, 2012
    5,836
    3,924
    180
    In fact, you might already be experiencing the effects and may not even realize it!

    How? lol, we haven't reached the 29th yet
     
  4. Threat

    Threat Lord of the Files

    Feb 23, 2014
    1,064
    835
    60
    Easy ... if you have a program that needs to calculate the date several months from now - for example, a delivery date - and need to know the day of week (so for example, you don't land on a weekend) .... the date your calculating would be after the 29th.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. MrMagic

    MrMagic MDL Guru

    Feb 13, 2012
    5,836
    3,924
    180
    Good point
     
  6. JFKI

    JFKI MDL Expert

    Oct 25, 2015
    1,098
    370
    60
    Ponder the implication of the following...
    From : http://blogs.perl.org/users/ovid/2013/10/fighting-20-year-old-software-bugs.html
     
  7. MrMagic

    MrMagic MDL Guru

    Feb 13, 2012
    5,836
    3,924
    180
    Basically it's just programmers making mistakes - if they had coded it correctly in the first place, a leap year wouldn't make any difference at all
     
  8. JFKI

    JFKI MDL Expert

    Oct 25, 2015
    1,098
    370
    60
    Not so much mistakes, but oversights.

    Who really cares about leap years anyway when your boss ( :ranting: ) is
    screaming over your shoulder for you to get the job done ? :stupid:
     
  9. Michaela Joy

    Michaela Joy MDL Crazy Lady

    Jul 26, 2012
    3,648
    3,950
    120
    #9 Michaela Joy, Feb 7, 2016
    Last edited by a moderator: Apr 20, 2017
    Code:
       if(year % 4 == 0 || year % 400 == 0 || year % 100 == 0)
          cout << "this is a leap year" << endl;
       else
          cout << "this is not a leap year" << endl;
    
    Nope...I don't worry about it. :)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  10. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,485
    1,322
    60
    #10 QuantumBug, Feb 10, 2016
    Last edited by a moderator: Apr 20, 2017
    C#:

    Code:
    using System;
    
    public class IsLeapYear
    {
       public static void Main()
       {
          for (int year = 1994; year <= 2014; year++)
          {
             if (DateTime.IsLeapYear(year))
             {
                Console.WriteLine("{0} is a leap year.", year);
                DateTime leapDay = new DateTime(year, 2, 29);
                DateTime nextYear = leapDay.AddYears(1);
                Console.WriteLine("   One year from {0} is {1}.", 
                                  leapDay.ToString("d"), 
                                  nextYear.ToString("d"));
             }         
          }
       }
    }
    VB.NET:

    Code:
    Module IsLeapYear
       Public Sub Main()
          For year As Integer = 1994 to 2014
             If DateTime.IsLeapYear(year) Then
                Console.WriteLine("{0} is a leap year.", year)
                Dim leapDay As New Date(year, 2, 29)
                Dim nextYear As Date = leapDay.AddYears(1)
                Console.WriteLine("   One year from {0} is {1}.", _
                                  leapDay.ToString("d"), _
                                  nextYear.ToString("d"))
             End If
          Next
       End Sub
    End Module
    These examples displays the following output:

    1996 is a leap year.
    One year from 2/29/1996 is 2/28/1997.

    2000 is a leap year.
    One year from 2/29/2000 is 2/28/2001.

    2004 is a leap year.
    One year from 2/29/2004 is 2/28/2005.

    2008 is a leap year.
    One year from 2/29/2008 is 2/28/2009.

    2012 is a leap year.
    One year from 2/29/2012 is 2/28/2013.
     
  11. Stannieman

    Stannieman MDL Guru

    Sep 4, 2009
    2,232
    1,803
    90
    In Belgium there are the Belgacom/Proximus set top boxes that didn't record episodes they were programmed to record on the 29th. I don't own one of them though (I have Telenet as my provider), so I don't know how it exactly happened.

    But can it really be that hard? If you're using existing datetime libraries like the ones built on with almost every programming languages you should be pretty save I think. Don't try to reinvent the wheel, just use existing libs that proved to be working over time.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  12. Michaela Joy

    Michaela Joy MDL Crazy Lady

    Jul 26, 2012
    3,648
    3,950
    120
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  13. Mutoid

    Mutoid MDL Member

    Sep 23, 2015
    182
    24
    10
    What's the issue with 2038 ?
    .... that one must have passed me by :)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  14. Stannieman

    Stannieman MDL Guru

    Sep 4, 2009
    2,232
    1,803
    90
    I think that's when 2 digit years change century. Right now 38 - 99 means 1938 - 1999, 00 - 37 means 2000-2037. At some point this should change, because when it's 2040 we want 38 to be 2038 and not 1938 anymore.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  15. sl1fka

    sl1fka MDL Novice

    Jan 1, 2016
    41
    29
    0
    The 2038 bug will only happen on systems that use UNIX Time. UNIX time is a 32-bit value that counts all of the seconds since January 1, 1970. Sometime in 2038, that 32-bit number will reach its maximum value.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  16. Mutoid

    Mutoid MDL Member

    Sep 23, 2015
    182
    24
    10
    Now that makes some sense to me .... I now know where to look for further info.

    Thanks for posting !
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  17. Michaela Joy

    Michaela Joy MDL Crazy Lady

    Jul 26, 2012
    3,648
    3,950
    120
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  18. QuantumBug

    QuantumBug MDL Developer

    Mar 7, 2012
    1,485
    1,322
    60
    #18 QuantumBug, Jun 22, 2016
    Last edited: Jun 22, 2016
    From Jan 1st 1970, the max count for a 32-bit signed integer is 2,147,483,647, this means that Unix Time is only applicable until Tuesday 19 Jan 2038 03:14:07 (GMT). Once this integer is exceeded, systems utilizing this time will roll over to 1970 or crash.

    So poor old Jim the janitor at the nuclear plant wont be able to watch his star trek porn whilst the reactors are in meltdown.