From 785b46594804941fd65c84b39c6fe833fd7ef3ba Mon Sep 17 00:00:00 2001 From: Chris Frey Date: Thu, 24 May 2012 01:14:59 -0400 Subject: [PATCH] lib: added DaysInMonth() utility function --- src/time.cc | 28 ++++++++++++++++++++++++++++ src/time.h | 3 +++ 2 files changed, 31 insertions(+) diff --git a/src/time.cc b/src/time.cc index 6185e541..53990698 100644 --- a/src/time.cc +++ b/src/time.cc @@ -259,6 +259,34 @@ struct timespec* ThreadTimeout(int timeout_ms, struct timespec *spec) return spec; } +// +// DaysInMonth +// +/// Returns the number of days in the month, given the tm_mon and tm_year +/// as specified in the struct tm argument. It _does_ take leap year into +/// account. +/// +BXEXPORT int DaysInMonth(struct tm &t) +{ + int year = t.tm_year + 1900; + + switch( t.tm_mon ) + { + case 1: + if( year % 400 == 0 || (year % 100 != 0 && year % 4 == 0) ) + return 29; + else + return 28; + case 4: + case 6: + case 9: + case 11: + return 30; + default: + return 31; + } +} + } // namespace Barry diff --git a/src/time.h b/src/time.h index a1e9ef81..bf759f3e 100644 --- a/src/time.h +++ b/src/time.h @@ -86,6 +86,9 @@ BXEXPORT time_t Message2Time(uint16_t r_date, uint16_t r_time); // Thread timeout creation BXEXPORT struct timespec* ThreadTimeout(int timeout_ms, struct timespec *spec); +// Utility functions +BXEXPORT int DaysInMonth(struct tm &t); + } // namespace Barry #endif -- 2.11.4.GIT