aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/tests/daysinmonth.cpp16
-rw-r--r--src/util.cpp40
-rw-r--r--src/util.h6
3 files changed, 62 insertions, 0 deletions
diff --git a/src/tests/daysinmonth.cpp b/src/tests/daysinmonth.cpp
new file mode 100644
index 0000000..9888844
--- /dev/null
+++ b/src/tests/daysinmonth.cpp
@@ -0,0 +1,16 @@
1#include "bu/sio.h"
2#include "bu/util.h"
3
4using namespace Bu;
5
6int main()
7{
8 for( int j = 0; j < 12; j++ )
9 {
10 sio << "2012-" << j << " = "
11 << getDaysInMonth( j, 2012 ) << sio.nl;
12 }
13
14 return 0;
15}
16
diff --git a/src/util.cpp b/src/util.cpp
index 111574c..3ca711d 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -1 +1,41 @@
1#include "bu/util.h" 1#include "bu/util.h"
2
3int Bu::getDaysInMonth( int iMonth, int iYear )
4{
5 if( iMonth > 11 )
6 {
7 iYear += iMonth/12;
8 iMonth = iMonth%12;
9 }
10 switch( iMonth )
11 {
12 case 0:
13 case 2:
14 case 4:
15 case 6:
16 case 7:
17 case 9:
18 case 11:
19 return 31;
20 break;
21
22 case 3:
23 case 5:
24 case 8:
25 case 10:
26 return 30;
27 break;
28
29 case 1:
30 if( iYear%400 == 0 )
31 return 29;
32 if( iYear%100 == 0 )
33 return 28;
34 if( iYear%4 == 0 )
35 return 29;
36 return 28;
37 break;
38 }
39
40}
41
diff --git a/src/util.h b/src/util.h
index 0c2f0eb..c284880 100644
--- a/src/util.h
+++ b/src/util.h
@@ -167,6 +167,12 @@ namespace Bu
167 return *a > *b; 167 return *a > *b;
168 } 168 }
169 }; 169 };
170
171 /**
172 * Get the number of days in the month in the gregorian calendar, taking
173 * leap years into account.
174 */
175 int getDaysInMonth( int iMonth, int iYear );
170}; 176};
171 177
172#endif 178#endif