Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Common/3.3/TimeSpanHelper.cs @ 17800

Last change on this file since 17800 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 3.4 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#endregion
23
24using System;
25using System.Text;
26using System.Text.RegularExpressions;
27
28namespace HeuristicLab.Common {
29  public static class TimeSpanHelper {
30    public static bool TryGetFromNaturalFormat(string text, out TimeSpan result) {
31      result = TimeSpan.Zero;
32
33      var matches = Regex.Matches(text, @"(\d+)[ \t]*([a-zA-Z]+)");
34      if (matches.Count == 0) return false;
35
36      foreach (Match match in matches) {
37        var number = match.Groups[1].Value;
38        var unit = match.Groups[2].Value;
39
40        double value;
41        if (!double.TryParse(number, out value))
42          return false;
43
44        switch (unit) {
45          case "d":
46          case "day":
47          case "days": result = result.Add(TimeSpan.FromDays(value)); break;
48          case "h":
49          case "hour":
50          case "hours": result = result.Add(TimeSpan.FromHours(value)); break;
51          case "m":
52          case "min":
53          case "minute":
54          case "minutes": result = result.Add(TimeSpan.FromMinutes(value)); break;
55          case "s":
56          case "sec":
57          case "second":
58          case "seconds": result = result.Add(TimeSpan.FromSeconds(value)); break;
59          default: return false;
60        }
61      }
62      return true;
63    }
64
65    public static string FormatNatural(TimeSpan ts, bool abbrevUnit = false, bool composite = false) {
66      if (!composite) {
67        if (ts.TotalSeconds < 180) return ts.TotalSeconds + (abbrevUnit ? "s" : " seconds");
68        if (ts.TotalMinutes < 180) return ts.TotalMinutes + (abbrevUnit ? "min" : " minutes");
69        if (ts.TotalHours < 96) return ts.TotalHours + (abbrevUnit ? "h" : " hours");
70        return ts.TotalDays + (abbrevUnit ? "d" : " days");
71      } else {
72        var sb = new StringBuilder();
73        if (ts.TotalDays > 0) {
74          var absDays = (int)Math.Floor(ts.TotalDays);
75          sb.Append(absDays);
76          sb.Append(abbrevUnit ? "d" : ((absDays > 1) ? " days" : " day"));
77        }
78        if (ts.Hours > 0) {
79          if (sb.Length > 0) sb.Append(" ");
80          sb.Append(ts.Hours);
81          sb.Append(abbrevUnit ? "h" : ((ts.Hours > 1) ? " hours" : " hour"));
82        }
83        if (ts.Minutes > 0) {
84          if (sb.Length > 0) sb.Append(" ");
85          sb.Append(ts.Minutes);
86          sb.Append(abbrevUnit ? "min" : ((ts.Minutes > 1) ? " minutes" : " minute"));
87        }
88        if (ts.Seconds > 0) {
89          if (sb.Length > 0) sb.Append(" ");
90          sb.Append(ts.Seconds);
91          sb.Append(abbrevUnit ? "s" : ((ts.Minutes > 1) ? " seconds" : " second"));
92        }
93        return sb.ToString();
94      }
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.