summaryrefslogtreecommitdiffstats
path: root/src/util.rs
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2017-09-16 10:47:03 +0200
committerMatthias Beyer <mail@beyermatthias.de>2017-09-16 10:47:11 +0200
commit3660f0608e25cf0c48561a5c89f4fa080a0eb564 (patch)
treeb090a8f2b36f5de42cd40fe3a07684d5d9143b1d /src/util.rs
parent98bab48b8c900e32b0b8d6108262ffe6606b51aa (diff)
Add end-of-month type/calc
Adds util module
Diffstat (limited to 'src/util.rs')
-rw-r--r--src/util.rs36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/util.rs b/src/util.rs
new file mode 100644
index 0000000..3062c84
--- /dev/null
+++ b/src/util.rs
@@ -0,0 +1,36 @@
+
+#[inline]
+pub fn get_num_of_days_in_month(y: i32, m: u32) -> u32 {
+ if m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12 {
+ 31
+ } else if m == 2 {
+ if is_leap_year(y) {
+ 29
+ } else {
+ 28
+ }
+ } else {
+ 30
+ }
+}
+
+#[inline]
+pub fn is_leap_year(y: i32) -> bool {
+ (y % 4 == 0) && (y % 100 != 0 || y % 400 == 0)
+}
+
+#[test]
+fn test_is_leap_year() {
+ let leaps = [ 1880, 1884, 1888, 1892, 1896, 1904, 1908, 1912, 1916, 1920, 1924, 1928, 1932,
+ 1936, 1940, 1944, 1948, 1952, 1956, 1960, 1964, 1968, 1972, 1976, 1980, 1984, 1988, 1992,
+ 1996, 2000, 2004, 2008, 2012, 2020, 2024, 2028, 2032, 2036, 2040, 2044, 2048, 2052, 2056,
+ 2060, 2064, 2068, 2072, 2076, 2080, 2084, 2088, 2092, 2096, 2104, 2108, 2112, 2116, 2120,
+ 2124, 2128, 2132, 2136, 2140, 2144, 2148, 2152, 2156, 2160, 2164, 2168, 2172, 2176, 2180,
+ 2184, 2188, 2192, 2196, 2204, 2208 ];
+
+ for i in leaps.iter() {
+ assert!(is_leap_year(*i), "Is no leap year: {}", i);
+ assert!(!is_leap_year(*i + 1), "Seems to be leap year: {}, but should not be", i + 1);
+ }
+
+}