aboutsummaryrefslogtreecommitdiff
path: root/src/feature/nodelist/networkstatus.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2020-02-24 10:04:01 -0500
committerNick Mathewson <nickm@torproject.org>2020-02-24 10:04:01 -0500
commitb7ba558f56da643857884761f6a52262c7aa51b8 (patch)
treee6e09605b3c98de2228ea0e49076f399257e717e /src/feature/nodelist/networkstatus.c
parente1cf10ceb7b742b5028ff4f5cfbf2b2e67572c06 (diff)
downloadtor-b7ba558f56da643857884761f6a52262c7aa51b8.tar.gz
tor-b7ba558f56da643857884761f6a52262c7aa51b8.zip
Move one voting schedule fn into networkstatus.c
The 'voting_schdule_get_start_of_next_interval' function isn't actually dirauth-specific.
Diffstat (limited to 'src/feature/nodelist/networkstatus.c')
-rw-r--r--src/feature/nodelist/networkstatus.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/feature/nodelist/networkstatus.c b/src/feature/nodelist/networkstatus.c
index d7864fa38a..6755de1a81 100644
--- a/src/feature/nodelist/networkstatus.c
+++ b/src/feature/nodelist/networkstatus.c
@@ -2765,3 +2765,47 @@ networkstatus_free_all(void)
}
}
}
+
+/** Return the start of the next interval of size <b>interval</b> (in
+ * seconds) after <b>now</b>, plus <b>offset</b>. Midnight always
+ * starts a fresh interval, and if the last interval of a day would be
+ * truncated to less than half its size, it is rolled into the
+ * previous interval. */
+time_t
+voting_schedule_get_start_of_next_interval(time_t now, int interval,
+ int offset)
+{
+ struct tm tm;
+ time_t midnight_today=0;
+ time_t midnight_tomorrow;
+ time_t next;
+
+ tor_gmtime_r(&now, &tm);
+ tm.tm_hour = 0;
+ tm.tm_min = 0;
+ tm.tm_sec = 0;
+
+ if (tor_timegm(&tm, &midnight_today) < 0) {
+ // LCOV_EXCL_START
+ log_warn(LD_BUG, "Ran into an invalid time when trying to find midnight.");
+ // LCOV_EXCL_STOP
+ }
+ midnight_tomorrow = midnight_today + (24*60*60);
+
+ next = midnight_today + ((now-midnight_today)/interval + 1)*interval;
+
+ /* Intervals never cross midnight. */
+ if (next > midnight_tomorrow)
+ next = midnight_tomorrow;
+
+ /* If the interval would only last half as long as it's supposed to, then
+ * skip over to the next day. */
+ if (next + interval/2 > midnight_tomorrow)
+ next = midnight_tomorrow;
+
+ next += offset;
+ if (next - interval > now)
+ next -= interval;
+
+ return next;
+}