Presentation is loading. Please wait.

Presentation is loading. Please wait.

Time is not on my side.

Similar presentations


Presentation on theme: "Time is not on my side."— Presentation transcript:

1 Time is not on my side

2 Guess the Output – Part 1 try it: https://ideone.com/f9Qj4C
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); dateFormat.setTimeZone(TimeZone.getTimeZone("America/Chicago")); String dateString1 = " :50:00"; // May 26th 2017, 10:50:00 AM String dateString2 = " :50:01"; // May 26th 2017, 10:50:01 AM Date date1 = dateFormat.parse(dateString1); // Parse string to date Date date2 = dateFormat.parse(dateString2); // Parse string to date long milliSecondDifference = date2.getTime() - date1.getTime(); // Subtract dates long secondsDifference = milliSecondDifference / 1000; // Milliseconds to Seconds System.out.println(secondsDifference); Correct output is 1 second Try it yourself try it:

3 Guess the Output – Part 2 try it: https://ideone.com/KZ9DXg
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); String dateString1 = " :54:16"; // December 31st :54:16 PM String dateString2 = " :54:17"; // December 31st :54:17 PM Date date1 = dateFormat.parse(dateString1); // Parse string to date Date date2 = dateFormat.parse(dateString2); // Parse string to date long milliSecondDifference = date2.getTime() - date1.getTime(); // Subtract dates long secondsDifference = milliSecondDifference / 1000; // Milliseconds to Seconds System.out.println(secondsDifference); Expected output is 1 second Correct output is 344 second Try it yourself try it:

4 Why? Backup link: Start 18s End 7:36

5 Stack Overflow - Question

6 Stack Overflow - Answer

7 2012 Microsoft Azure Bug Occurred at 4:00 PM on February 28th, 2012 (PST) Code calculated a certificate expiration date, 1 year from the current issuing date. February 29th, 2013 is not a valid date, caused a cascading failure Caused service disruptions to Azure for 12 hours SYSTEMTIME st; // declare a SYSTEMTIME variable GetSystemTime(&st); // set it to the current date and time st.wYear++; // increment it by one year February 28th, 2012 (PST) is 00:00 February 29th (UST)

8 isLeapYear(int year) { return year % 4 == 0 }
Fixing the bug isLeapYear(int year) { return year % 4 == 0 } What about the year 2100 (not a leap year) They skip centuries

9 Moral of the Story Don’t reinvent the wheel Know your edge cases
DateTime.AddYears will take into account leap years Know your edge cases Test your edge cases


Download ppt "Time is not on my side."

Similar presentations


Ads by Google