From 805251a6db499d38d101abdbce2a72aff8c59e75 Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Mon, 4 Jul 2016 16:23:35 +0300 Subject: [PATCH] Add utility to calculate date range in seconds on Chrono library. --- source/Library/Chrono.cpp | 34 ++++++++++++++++++++++++++++++++++ source/Library/Chrono.hpp | 5 +++++ 2 files changed, 39 insertions(+) diff --git a/source/Library/Chrono.cpp b/source/Library/Chrono.cpp index 3b603ea9..50448bea 100644 --- a/source/Library/Chrono.cpp +++ b/source/Library/Chrono.cpp @@ -219,6 +219,40 @@ Date Chrono::ReverseDayOfyear(Uint16 year, Uint16 doy) return Date(year, month, doy); } +// ------------------------------------------------------------------------------------------------ +Int64 Chrono::DateRangeToSeconds(Uint16 _year, Uint8 _month, Uint8 _day, Uint16 year_, Uint8 month_, Uint8 day_) +{ + // Are we within the same year? + if (_year == year_) + { + return (DayOfYear(_year, _month, _day) - DayOfYear(year_, month_, day_)) * 86400ULL; + } + // Should we negate the result when returning it? + bool neg = false; + // Is the start year greater than the end year? + if (_year > year_) + { + std::swap(_year, year_); + std::swap(_month, month_); + std::swap(_day, day_); + // Negate the result + neg = true; + } + // Calculate the remaining days from the first year + Int64 num = DaysInYear(_year) - DayOfYear(_year, _month, _day); + // Calculate the days withing the years range + while (++_year < year_) + { + num += DaysInYear(_year); + } + // Calculate the days up to the last day + num += DayOfYear(year_, month_, day_); + // Convert the obtained days in seconds + num *= 86400ULL; + // Return the result + return neg ? -num : num; +} + // ------------------------------------------------------------------------------------------------ static SLongInt SqGetEpochTimeMicro() { diff --git a/source/Library/Chrono.hpp b/source/Library/Chrono.hpp index 66009ec6..a60c28ee 100644 --- a/source/Library/Chrono.hpp +++ b/source/Library/Chrono.hpp @@ -104,6 +104,11 @@ public: * Convert just the year and day of year to full date. */ static Date ReverseDayOfyear(Uint16 year, Uint16 doy); + + /* -------------------------------------------------------------------------------------------- + * Calculate the number of days in the specified date range. + */ + static Int64 DateRangeToSeconds(Uint16 _year, Uint8 _month, Uint8 _day, Uint16 year_, Uint8 month_, Uint8 day_); }; } // Namespace:: SqMod