Skip to content

Commit 3dd23b3

Browse files
committed
[kate.shared] Created FormatHelper
1 parent 3ebe09f commit 3dd23b3

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace kate.shared.Helpers
5+
{
6+
/// <summary>
7+
/// Helper class for formatting things
8+
/// </summary>
9+
public static class FormatHelper
10+
{
11+
public const string SqlServerDateTimeFormat = "yyyy-MM-dd HH:mm:ss.fff";
12+
13+
/// <summary>
14+
/// Format a TimeSpan to either one of the following;
15+
/// <list type="bullet">
16+
/// <item><c>0d 0h 0m 0s</c></item>
17+
/// <item><c>0h 0m 0s</c></item>
18+
/// <item><c>0m 0s</c></item>
19+
/// <item><c>0s</c></item>
20+
/// <item><c>0d 0h 0m 0.000s</c></item>
21+
/// <item><c>0h 0m 0.000s</c></item>
22+
/// <item><c>0m 0.000s</c></item>
23+
/// <item><c>0.000s</c></item>
24+
/// <item><c>0ms</c> (when <see cref="TimeSpan.TotalMilliseconds"/> is less than <c>2000</c>)</item>
25+
/// </list>
26+
/// </summary>
27+
public static string Duration(TimeSpan span)
28+
{
29+
if (span.TotalMilliseconds < 2000)
30+
{
31+
return span.TotalMilliseconds.ToString() + "ms";
32+
}
33+
var result = new List<string>();
34+
35+
if (span.Milliseconds > 10)
36+
{
37+
result.Add(string.Format("{0}.{1}",
38+
span.Seconds,
39+
span.Milliseconds.ToString().PadLeft(3, '0')));
40+
}
41+
else
42+
{
43+
result.Add(span.Seconds.ToString() + 's');
44+
}
45+
46+
if (span.Minutes > 0)
47+
{
48+
result.Insert(0,
49+
string.Format("{0}m", span.Minutes));
50+
}
51+
if (span.Hours > 0)
52+
{
53+
result.Insert(0,
54+
string.Format("{0}h", span.Hours));
55+
}
56+
if (span.Days > 0)
57+
{
58+
result.Insert(0,
59+
string.Format("{0}d", span.Days));
60+
}
61+
return string.Join(" ", result);
62+
}
63+
64+
/// <summary>
65+
/// Format the duration between <paramref name="start"/> and the current date (using <see cref="DateTimeOffset.UtcNow"/>)
66+
/// with <see cref="Duration(TimeSpan)"/>
67+
/// </summary>
68+
public static string DurationToUtcNow(DateTimeOffset start)
69+
{
70+
var end = DateTimeOffset.UtcNow;
71+
var diff = end - start;
72+
73+
return Duration(diff);
74+
}
75+
76+
/// <summary>
77+
/// Format the duration between <paramref name="start"/> and the current date (using <see cref="DateTime.UtcNow"/>)
78+
/// with <see cref="Duration(TimeSpan)"/>
79+
/// </summary>
80+
public static string DurationToUtcNow(DateTime start)
81+
{
82+
var end = DateTime.UtcNow;
83+
var diff = end - start;
84+
85+
return Duration(diff);
86+
}
87+
88+
/// <summary>
89+
/// Format the duration between <paramref name="start"/> and the current date (using <see cref="DateTimeOffset.Now"/>)
90+
/// with <see cref="Duration(TimeSpan)"/>
91+
/// </summary>
92+
public static string DurationToNow(DateTimeOffset start)
93+
{
94+
var end = DateTimeOffset.Now;
95+
var diff = end - start;
96+
97+
return Duration(diff);
98+
}
99+
100+
/// <summary>
101+
/// Format the duration between <paramref name="start"/> and the current date (using <see cref="DateTime.Now"/>)
102+
/// with <see cref="Duration(TimeSpan)"/>
103+
/// </summary>
104+
public static string DurationToNow(DateTime start)
105+
{
106+
var end = DateTime.Now;
107+
var diff = end - start;
108+
109+
return Duration(diff);
110+
}
111+
public static string DurationMilliseconds(long value)
112+
{
113+
return Duration(TimeSpan.FromMilliseconds(value));
114+
}
115+
#if NET8_0_OR_GREATER
116+
public static string DurationMicroseconds(long value)
117+
{
118+
return Duration(TimeSpan.FromMicroseconds(value));
119+
}
120+
#endif
121+
}
122+
}

kate.shared/Helpers/GeneralHelper.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public static class GeneralHelper
1818
/// Format the duration between <paramref name="start"/> and the current date (using <see cref="DateTimeOffset.UtcNow"/>
1919
/// </summary>
2020
/// <returns><see cref="FormatDuration(TimeSpan)"/></returns>
21+
[Obsolete("Use kate.shared.Helpers.FormatHelper.DurationToUtcNow(DateTimeOffset)")]
2122
public static string GenerateTaskDuration(DateTimeOffset start)
2223
{
2324
var end = DateTimeOffset.UtcNow;
@@ -28,6 +29,7 @@ public static string GenerateTaskDuration(DateTimeOffset start)
2829
/// <summary>
2930
/// Will format a timespan to `HH:MM:ss.sss` or `ss.sss seconds`.
3031
/// </summary>
32+
[Obsolete("Use kate.shared.Helpers.FormatHelper.Duration(TimeSpan)")]
3133
public static string FormatDuration(TimeSpan duration)
3234
{
3335
var st = new List<string>();

0 commit comments

Comments
 (0)