summaryrefslogtreecommitdiff
path: root/AK
diff options
context:
space:
mode:
authorNico Weber <thakis@chromium.org>2020-08-25 20:11:12 -0400
committerAndreas Kling <kling@serenityos.org>2020-08-26 08:52:07 +0200
commita7a18b478e9ebd1bf668e926423305f211fc8c50 (patch)
tree5ca65252071acb3aeb06ee9d3c8ff972c3809112 /AK
parentdcb81fc1999f468c723a62fb29696f908d859081 (diff)
downloadserenity-a7a18b478e9ebd1bf668e926423305f211fc8c50.zip
AK+LibC+LibCore: Have fewer implementations of days_in_month
Diffstat (limited to 'AK')
-rw-r--r--AK/Time.cpp9
-rw-r--r--AK/Time.h4
2 files changed, 13 insertions, 0 deletions
diff --git a/AK/Time.cpp b/AK/Time.cpp
index 63253f90b7..ea9e470dba 100644
--- a/AK/Time.cpp
+++ b/AK/Time.cpp
@@ -42,4 +42,13 @@ int day_of_year(int year, unsigned month, int day)
return day_of_year;
}
+int days_in_month(int year, unsigned month)
+{
+ ASSERT(month >= 1 && month <= 12);
+ if (month == 2)
+ return is_leap_year(year) ? 29 : 28;
+
+ bool is_long_month = (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12);
+ return is_long_month ? 31 : 30;
+}
}
diff --git a/AK/Time.h b/AK/Time.h
index 612b92f691..9a87f3c389 100644
--- a/AK/Time.h
+++ b/AK/Time.h
@@ -35,6 +35,9 @@ namespace AK {
// can be negative.
int day_of_year(int year, unsigned month, int day);
+// Month starts at 1. Month must be >= 1 and <= 12.
+int days_in_month(int year, unsigned month);
+
inline bool is_leap_year(int year)
{
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
@@ -169,6 +172,7 @@ inline bool operator!=(const TimespecType& a, const TimespecType& b)
}
using AK::day_of_year;
+using AK::days_in_month;
using AK::is_leap_year;
using AK::timespec_add;
using AK::timespec_add_timeval;