Skip to content

Commit 99fa75b

Browse files
committed
Leap Year Examples
1 parent 0bb0031 commit 99fa75b

File tree

5 files changed

+51
-0
lines changed

5 files changed

+51
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Checking for leap year
2+
bool isLeapYear = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
3+
if (isLeapYear)
4+
{
5+
// untested path
6+
}
7+
else
8+
{
9+
// tested path
10+
}
11+
12+
13+
// Checking specifically for the leap day
14+
if (month == 2 && day == 29) // (or 1 with a tm_mon value)
15+
{
16+
// untested path
17+
}
18+
else
19+
{
20+
// tested path
21+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
SYSTEMTIME st;
2+
FILETIME ft;
3+
GetSystemTime(&st);
4+
5+
// Flawed logic may result in invalid date
6+
st.wYear++;
7+
8+
// The following code may fail
9+
SystemTimeToFileTime(&st, &ft);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
SYSTEMTIME st;
2+
FILETIME ft;
3+
GetSystemTime(&st);
4+
5+
// Flawed logic may result in invalid date
6+
st.wYear++;
7+
8+
// Check for leap year, and adjust the date accordingly
9+
bool isLeapYear = st.wYear % 4 == 0 && (st.wYear % 100 != 0 || st.wYear % 400 == 0);
10+
st.wDay = st.wMonth == 2 && st.wDay == 29 && !isLeapYear ? 28 : st.wDay;
11+
12+
if (!SystemTimeToFileTime(&st, &ft))
13+
{
14+
// handle error
15+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
int items[365];
2+
items[dayOfYear - 1] = x; // buffer overflow on December 31st of any leap year
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
bool isLeapYear = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
2+
int *items = new int[isLeapYear ? 366 : 365];
3+
// ...
4+
delete[] items;

0 commit comments

Comments
 (0)