* @return Next point in time the item is scheduled for
* returns constant #StNimmerleinsDay if it is scheduled never
*/
-time_t WeekCron::get_next_run(time_t calc_from)
+time_t WeekCron::get_next_run(time_t calc_from) const
{
if (!calc_from)
calc_from=time(NULL);
If yes, we will advance to the next day.
* @return Next point in time
*/
-time_t WeekCron::get_next_point(const time_t calc_from, const int daysec, const bool todaycheck)
+time_t WeekCron::get_next_point(const time_t calc_from, const int daysec, const bool todaycheck) const
{
struct tm ft;
- localtime_r(&calc_from,&ft);
+ if (localtime_r(&calc_from,&ft) == NULL)
+ return calc_from;
// take care of the weekday
- ft.tm_mday+=Week.days_till_set(static_cast<Week::WeekDay>(ft.tm_wday));
+ ft.tm_mday+=Week.days_till_set(static_cast<Week::WeekDay>(ft.tm_wday)); //lint !e737 !e713
ft.tm_hour=0;
ft.tm_min=0;
// get unixtime of start of the day and add daysec
time_t target=mktime(&ft)+daysec;
+ if (target == (time_t)-1)
+ return calc_from;
// todays schedule could already been through or now
if (todaycheck && target <= calc_from)
If yes, we will go back to the previos day
* @return Previous point in time
*/
-time_t WeekCron::get_previousnow_point(const time_t calc_from, const int daysec, const bool todaycheck)
+time_t WeekCron::get_previousnow_point(const time_t calc_from, const int daysec, const bool todaycheck) const
{
struct tm ft;
- localtime_r(&calc_from,&ft);
+ if (localtime_r(&calc_from,&ft) == NULL)
+ return calc_from;
// take care of the weekday
- ft.tm_mday-=Week.days_since_set(static_cast<Week::WeekDay>(ft.tm_wday));
+ ft.tm_mday-=Week.days_since_set(static_cast<Week::WeekDay>(ft.tm_wday)); //lint !e737 !e713
ft.tm_hour=0;
ft.tm_min=0;
// get unixtime of start of the day and add daysec
time_t target=mktime(&ft)+daysec;
+ if (target == (time_t)-1)
+ return calc_from;
// target later than we are looking for
// target==calc_from is ok (that's why it is called lastnow...)
/// Stores the active days this WeekCron is valid for
I2n::Time::Week Week;
- time_t get_next_point(const time_t calc_from, const int daysec, const bool todaycheck);
- time_t get_previousnow_point(const time_t calc_from, const int daysec, const bool todaycheck);
+ time_t get_next_point(const time_t calc_from, const int daysec, const bool todaycheck) const;
+ time_t get_previousnow_point(const time_t calc_from, const int daysec, const bool todaycheck) const;
public:
WeekCron(const std::string& daystring, const int begin);
bool is_sane() const;
- time_t get_next_run(time_t calc_from=0);
+ time_t get_next_run(time_t calc_from=0) const;
};
}
CPPUNIT_TEST(IntervalOnceShort);
CPPUNIT_TEST(IntervalTooShort);
-// CPPUNIT_TEST(StartHourStaysTheSameTill2038);
+ CPPUNIT_TEST(StartHourStaysTheSameTill2038);
CPPUNIT_TEST_SUITE_END();
void StartHourStaysTheSameTill2038()
{
- time_t result = 0;
- struct tm result_localtime;
+ int daysec = 79200;
- // Schedule daily at 22h from 1970 till 01.01.2038. Check every 30 minutes.
- WeekCron cron("0123456",79200);
- for (time_t now = 86400*15; now < 2145916800; now += 30*60)
+ // Schedule daily at 22h from 1970 till 01.01.2038. Check every 90 minutes.
+ WeekCron cron("0123456", daysec);
+ for (time_t now = 86400*15; now < 2145916800; now += 90*60)
{
- result = cron.get_next_run(now);
+ time_t result = cron.get_next_run(now);
- bool conversion_ok = (localtime_r(&result, &result_localtime) != NULL);
+ // Calculate unix time for the begin of the day
+ struct tm calc_daybegin;
+ bool conversion_ok = (localtime_r(&result, &calc_daybegin) != NULL);
CPPUNIT_ASSERT_EQUAL(true, conversion_ok);
- if (result_localtime.tm_hour != 22)
- {
- struct tm debug_now;
+ calc_daybegin.tm_hour=0;
+ calc_daybegin.tm_min=0;
+ calc_daybegin.tm_sec=0;
+ // tm_isdst means to use the dst in use at the given time
+ calc_daybegin.tm_isdst=-1;
- conversion_ok = (localtime_r(&now, &debug_now) != NULL);
- CPPUNIT_ASSERT_EQUAL(true, conversion_ok);
+ time_t daybegin = mktime(&calc_daybegin);
- char buf[50];
- strftime(buf, 50, "%Y-%m-%d %H:%M:%S", &debug_now);
-
- cout << "ERROR: Failed for " << now << " (" << buf << "): Resulting hour is " << result_localtime.tm_hour << ". Ignoring." << endl;
-
- // TODO: Remove this line once the test is working
- result_localtime.tm_hour = 22;
- }
-
- CPPUNIT_ASSERT_EQUAL(22, result_localtime.tm_hour);
+ CPPUNIT_ASSERT_EQUAL(daybegin + daysec, result);
}
}
};