Commit 166d2cfd authored by LEFEBVREJP email's avatar LEFEBVREJP email
Browse files

Merge branch 'hysplit-time-diff' into 'master'

Added HysplitTime.diff

See merge request !115
parents 9b00f634 5604d400
Pipeline #156883 passed with stages
in 17 minutes and 21 seconds
#ifndef RADIX_RADIXIO_HYSPLITCDUMP_HH_
#define RADIX_RADIXIO_HYSPLITCDUMP_HH_
#include <array>
#include <memory>
#include <sstream>
#include <string>
......@@ -31,6 +32,57 @@ struct RADIX_PUBLIC HysplitTime
ss << month << "/" << day << "/" << year << " " << hour << " " << minutes;
return ss.str();
}
/**
* @brief diff returns difference between two times in minutes
* @param end
* @return minutes between this time and end
*/
size_t diff(const HysplitTime &end) const
{
// To store number of days in all months from January to December.
const std::array<int, 12> month_days{31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31};
auto count_leap_years = [](const radix::HysplitTime &date) {
int years = date.year;
// Check if the current year needs to be considered for the count of leap
// years or not
if (date.month <= 2) years--;
// a year is a leap year if it is a multiple of 4, multiple of 400 and not
// a multiple of 100.
return years / 4 - years / 100 + years / 400;
};
// COUNT TOTAL NUMBER OF DAYS
// BEFORE FIRST DATE 'start'
// initialize count using years and day
size_t days1 = this->year * 365 + this->day;
// Add days for months in given date
for (int i = 0; i < this->month - 1; ++i) days1 += month_days[i];
// Since every leap year is of 366 days,
// Add a day for every leap year
days1 += count_leap_years(*this);
// SIMILARLY, COUNT TOTAL NUMBER OF
// DAYS BEFORE 'end'
size_t days2 = end.year * 365 + end.day;
for (int i = 0; i < end.month - 1; ++i) days2 += month_days[i];
days2 += count_leap_years(end);
// return difference between two counts
size_t minutes1 = days1 * 1440 + this->hour * 60 +
this->minutes; // 24 * 60 = 1440 minutes in the day
size_t minutes2 = days2 * 1440 + end.hour * 60 + end.minutes;
size_t minutes =
std::max(minutes2, minutes1) - std::min(minutes2, minutes1);
return minutes;
}
}; // struct Time
enum class HysplitUnit
......
......@@ -114,6 +114,35 @@ TEST(RadixIO, HysplitCDumpEncoding)
EXPECT_EQ("ACi_", HysplitCDump::encode_activity_name(HysplitUnit::CI));
EXPECT_EQ("AuCi", HysplitCDump::encode_activity_name(HysplitUnit::MICROCI));
}
TEST(RadixIO, HysplitTime)
{
HysplitTime t1, t2;
t1 = {1951, 12, 31, 12, 0, 0};
t2 = {1951, 12, 31, 13, 0, 0};
EXPECT_EQ(60, t1.diff(t2));
t2.minutes = 1;
EXPECT_EQ(61, t1.diff(t2));
t2.year = 1952;
t2.month = 1;
t2.day = 1;
t2.hour = 0;
// 12 hours 1 minute and wraps the year
EXPECT_EQ(721, t1.diff(t2));
// update century
t1.year = 2020;
t2.year = 2021;
EXPECT_EQ(721, t1.diff(t2));
// check leap year
t1.month = 2;
t1.day = 28;
t2.year = 2020;
t2.month = 3;
t2.day = 1;
t2.hour = 12;
t2.minutes = 0;
// 2 days
EXPECT_EQ(2880, t1.diff(t2));
}
TEST(RadixIO, HysplitCDump)
{
std::string file("cdump.bin");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment