Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2522_RefactorPluginInfrastructure/HeuristicLab.Common/3.3/TimeSpanHelper.cs @ 16796

Last change on this file since 16796 was 15973, checked in by gkronber, 7 years ago

#2522: merged trunk changes from r13402:15972 to branch resolving conflicts where necessary

File size: 3.5 KB
RevLine 
[9041]1#region License Information
2
3/* HeuristicLab
[15973]4 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[9041]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.Collections.Generic;
26using System.Linq;
27using System.Text;
28using System.Text.RegularExpressions;
29
30namespace HeuristicLab.Common {
31  public static class TimeSpanHelper {
32    public static bool TryGetFromNaturalFormat(string text, out TimeSpan result) {
33      result = TimeSpan.Zero;
34
35      var matches = Regex.Matches(text, @"(\d+)[ \t]*([a-zA-Z]+)");
36      if (matches.Count == 0) return false;
37
38      foreach (Match match in matches) {
39        var number = match.Groups[1].Value;
40        var unit = match.Groups[2].Value;
41
42        double value;
43        if (!double.TryParse(number, out value))
44          return false;
45
46        switch (unit) {
47          case "d":
48          case "day":
49          case "days": result = result.Add(TimeSpan.FromDays(value)); break;
50          case "h":
51          case "hour":
52          case "hours": result = result.Add(TimeSpan.FromHours(value)); break;
[12626]53          case "m":
[9041]54          case "min":
55          case "minute":
56          case "minutes": result = result.Add(TimeSpan.FromMinutes(value)); break;
57          case "s":
[12626]58          case "sec":
[9041]59          case "second":
60          case "seconds": result = result.Add(TimeSpan.FromSeconds(value)); break;
61          default: return false;
62        }
63      }
64      return true;
65    }
66
67    public static string FormatNatural(TimeSpan ts, bool abbrevUnit = false, bool composite = false) {
68      if (!composite) {
69        if (ts.TotalSeconds < 180) return ts.TotalSeconds + (abbrevUnit ? "s" : " seconds");
70        if (ts.TotalMinutes < 180) return ts.TotalMinutes + (abbrevUnit ? "min" : " minutes");
71        if (ts.TotalHours < 96) return ts.TotalHours + (abbrevUnit ? "h" : " hours");
72        return ts.TotalDays + (abbrevUnit ? "d" : " days");
73      } else {
74        var sb = new StringBuilder();
75        if (ts.TotalDays > 0) {
76          var absDays = (int)Math.Floor(ts.TotalDays);
77          sb.Append(absDays);
78          sb.Append(abbrevUnit ? "d" : ((absDays > 1) ? " days" : " day"));
79        }
80        if (ts.Hours > 0) {
81          if (sb.Length > 0) sb.Append(" ");
82          sb.Append(ts.Hours);
83          sb.Append(abbrevUnit ? "h" : ((ts.Hours > 1) ? " hours" : " hour"));
84        }
85        if (ts.Minutes > 0) {
86          if (sb.Length > 0) sb.Append(" ");
87          sb.Append(ts.Minutes);
88          sb.Append(abbrevUnit ? "min" : ((ts.Minutes > 1) ? " minutes" : " minute"));
89        }
90        if (ts.Seconds > 0) {
91          if (sb.Length > 0) sb.Append(" ");
92          sb.Append(ts.Seconds);
93          sb.Append(abbrevUnit ? "s" : ((ts.Minutes > 1) ? " seconds" : " second"));
94        }
95        return sb.ToString();
96      }
97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.