From 365e4d8f6a833fa5477d09e9e5f07db0b0edefcf Mon Sep 17 00:00:00 2001
From: Niko Ehrenfeuchter <nikolaus.ehrenfeuchter@unibas.ch>
Date: Fri, 19 Jan 2018 10:23:13 +0100
Subject: [PATCH] Move time-delta code to TimeUtils.

---
 ATXCommon/TimeUtils.cs | 18 ++++++++++++++++++
 AutoTx/AutoTx.cs       |  5 ++---
 AutoTx/Email.cs        |  8 +++++---
 3 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/ATXCommon/TimeUtils.cs b/ATXCommon/TimeUtils.cs
index 04cc8fa..8a27393 100644
--- a/ATXCommon/TimeUtils.cs
+++ b/ATXCommon/TimeUtils.cs
@@ -11,5 +11,23 @@ namespace ATXCommon
         public static string Timestamp() {
             return DateTime.Now.ToString("yyyy-MM-dd__HH-mm-ss");
         }
+
+        /// <summary>
+        /// Calculate the time delta since the given date in minutes.
+        /// </summary>
+        /// <param name="refDate">The reference DateTime to check.</param>
+        /// <returns>The number of minutes between the reference date and now.</returns>
+        public static int MinutesSince(DateTime refDate) {
+            return (int)(DateTime.Now - refDate).TotalMinutes;
+        }
+
+        /// <summary>
+        /// Calculate the time delta since the given date in seconds.
+        /// </summary>
+        /// <param name="refDate">The reference DateTime to check.</param>
+        /// <returns>The number of seconds between the reference date and now.</returns>
+        public static int SecondsSince(DateTime refDate) {
+            return (int)(DateTime.Now - refDate).TotalSeconds;
+        }
     }
 }
diff --git a/AutoTx/AutoTx.cs b/AutoTx/AutoTx.cs
index 2435484..5e816cd 100644
--- a/AutoTx/AutoTx.cs
+++ b/AutoTx/AutoTx.cs
@@ -489,8 +489,7 @@ namespace AutoTx
             SendLowSpaceMail(SystemChecks.CheckFreeDiskSpace(_config.SpaceMonitoring));
             UpdateServiceState();
 
-            var delta = (int)(DateTime.Now - _lastUserDirCheck).TotalSeconds;
-            if (delta >= 120)
+            if (TimeUtils.SecondsSince(_lastUserDirCheck) >= 120)
                 CreateIncomingDirectories();
 
             // tasks depending on the service state:
@@ -934,7 +933,7 @@ namespace AutoTx
             if (string.IsNullOrEmpty(report))
                 return "";
             report = "Expired folders in grace location:\n" + report;
-            var delta = (int)(DateTime.Now - _status.LastGraceNotification).TotalMinutes;
+            var delta = TimeUtils.MinutesSince(_status.LastGraceNotification);
             report += "\nTime since last grace notification: " + delta + "\n";
             if (delta >= _config.GraceNotificationDelta) {
                 SendAdminEmail(report, "Grace location cleanup required.");
diff --git a/AutoTx/Email.cs b/AutoTx/Email.cs
index 584d546..96d435a 100644
--- a/AutoTx/Email.cs
+++ b/AutoTx/Email.cs
@@ -80,7 +80,7 @@ namespace AutoTx
             if (_config.SendAdminNotification == false)
                 return;
 
-            var delta = (int)(DateTime.Now - _status.LastAdminNotification).TotalMinutes;
+            var delta = TimeUtils.MinutesSince(_status.LastAdminNotification);
             if (delta < _config.AdminNotificationDelta) {
                 Log.Debug("Suppressed admin email, interval too short ({0} vs. {1}):\n\n{2}\n{3}",
                     delta, _config.AdminNotificationDelta, subject, body);
@@ -109,9 +109,11 @@ namespace AutoTx
             if (string.IsNullOrWhiteSpace(spaceDetails))
                 return;
 
-            var delta = (int)(DateTime.Now - _status.LastStorageNotification).TotalMinutes;
-            if (delta < _config.StorageNotificationDelta)
+            var delta = TimeUtils.MinutesSince(_status.LastStorageNotification);
+            if (delta < _config.StorageNotificationDelta) {
+                Log.Trace("Only {0} minutes since last low-space-notification, skipping.", delta);
                 return;
+            }
 
             Log.Warn("WARNING: {0}", spaceDetails);
             _status.LastStorageNotification = DateTime.Now;
-- 
GitLab