diff --git a/ATXSerializables/FsUtils.cs b/ATXSerializables/FsUtils.cs index c6dadb1103b511605dce2bc7fd5673d085aa7c2f..8dadf4d898fba827a749f48267098a1fdbb19ce9 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 510548bea40541cd4aeae51e154f271cd73315e2..0167d2c168737d7fbcc4f15a4d13006b2740918c 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 }