Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Common/3.3/TimeSpanHelper.cs @ 10355

Last change on this file since 10355 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 3.5 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2013 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.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;
53          case "min":
54          case "minute":
55          case "minutes": result = result.Add(TimeSpan.FromMinutes(value)); break;
56          case "s":
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.