From 0788bb49e5f8362adaf8368f698a5bcc5a1a197e Mon Sep 17 00:00:00 2001 From: Niko Ehrenfeuchter <nikolaus.ehrenfeuchter@unibas.ch> Date: Wed, 17 Jan 2018 16:10:06 +0100 Subject: [PATCH] Move DirNameToAge to FsUtils. --- ATXSerializables/FsUtils.cs | 27 ++++++++++++++++++++++++++- AutoTx/AutoTx.cs | 22 +--------------------- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/ATXSerializables/FsUtils.cs b/ATXSerializables/FsUtils.cs index c6dadb1..8dadf4d 100644 --- a/ATXSerializables/FsUtils.cs +++ b/ATXSerializables/FsUtils.cs @@ -1,10 +1,15 @@ -using System.IO; +using System; +using System.Globalization; +using System.IO; using System.Linq; +using NLog; namespace ATXCommon { public static class FsUtils { + private static readonly Logger Log = LogManager.GetCurrentClassLogger(); + /// <summary> /// Recursively sum up size of all files under a given path. /// </summary> @@ -15,5 +20,25 @@ namespace ATXCommon .GetFiles("*", SearchOption.AllDirectories) .Sum(file => file.Length); } + + /// <summary> + /// Convert the timestamp given by the NAME of a directory into the age in days. + /// </summary> + /// <param name="dir">The DirectoryInfo object to check for its name-age.</param> + /// <param name="baseTime">The DateTime object to compare to.</param> + /// <returns>The age in days, or -1 in case of an error.</returns> + public static int DirNameToAge(DirectoryInfo dir, DateTime baseTime) { + DateTime dirTimestamp; + try { + dirTimestamp = DateTime.ParseExact(dir.Name, "yyyy-MM-dd__HH-mm-ss", + CultureInfo.InvariantCulture); + } + catch (Exception ex) { + Log.Warn("Unable to parse time from name [{0}], skipping: {1}", + dir.Name, ex.Message); + return -1; + } + return (baseTime - dirTimestamp).Days; + } } } diff --git a/AutoTx/AutoTx.cs b/AutoTx/AutoTx.cs index 510548b..0167d2c 100644 --- a/AutoTx/AutoTx.cs +++ b/AutoTx/AutoTx.cs @@ -1069,7 +1069,7 @@ namespace AutoTx foreach (var userdir in graceDir.GetDirectories()) { var expired = new List<Tuple<DirectoryInfo, long, int>>(); foreach (var subdir in userdir.GetDirectories()) { - var age = DirNameToAge(subdir, now); + var age = FsUtils.DirNameToAge(subdir, now); if (age < thresh) continue; long size = -1; @@ -1088,26 +1088,6 @@ namespace AutoTx return collection; } - /// <summary> - /// Convert the timestamp given by the NAME of a directory into the age in days. - /// </summary> - /// <param name="dir">The DirectoryInfo object to check for its name-age.</param> - /// <param name="baseTime">The DateTime object to compare to.</param> - /// <returns>The age in days, or -1 in case of an error.</returns> - private int DirNameToAge(DirectoryInfo dir, DateTime baseTime) { - DateTime dirTimestamp; - try { - dirTimestamp = DateTime.ParseExact(dir.Name, "yyyy-MM-dd__HH-mm-ss", - CultureInfo.InvariantCulture); - } - catch (Exception ex) { - Log.Warn("ERROR parsing timestamp from directory name [{0}], skipping: {1}", - dir.Name, ex.Message); - return -1; - } - return (baseTime - dirTimestamp).Days; - } - #endregion } -- GitLab