-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoBackupIntervalHelper.cs
More file actions
76 lines (63 loc) · 2.1 KB
/
Copy pathAutoBackupIntervalHelper.cs
File metadata and controls
76 lines (63 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
namespace EasyVersionBackup
{
public static class AutoBackupIntervalHelper
{
public static string Format(int seconds)
{
int safeSeconds = Math.Max(0, seconds);
if (safeSeconds > 0 && safeSeconds % 3600 == 0)
{
return (safeSeconds / 3600).ToString() + "h";
}
if (safeSeconds > 0 && safeSeconds % 60 == 0)
{
return (safeSeconds / 60).ToString() + "m";
}
return safeSeconds.ToString() + "s";
}
public static bool TryParseSeconds(string value, out int seconds)
{
seconds = 0;
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
string normalizedValue = value.Trim().ToLowerInvariant();
long multiplier = 60;
string numberText = normalizedValue;
if (normalizedValue.EndsWith("s", StringComparison.Ordinal))
{
multiplier = 1;
numberText = normalizedValue[..^1];
}
else if (normalizedValue.EndsWith("m", StringComparison.Ordinal))
{
multiplier = 60;
numberText = normalizedValue[..^1];
}
else if (normalizedValue.EndsWith("h", StringComparison.Ordinal))
{
multiplier = 3600;
numberText = normalizedValue[..^1];
}
if (!long.TryParse(numberText, out long valueNumber) || valueNumber < 1)
{
return false;
}
long calculatedSeconds = valueNumber * multiplier;
if (calculatedSeconds > int.MaxValue)
{
return false;
}
seconds = (int)calculatedSeconds;
return true;
}
public static int ParseSecondsOrDefault(string value, int defaultSeconds)
{
return TryParseSeconds(value, out int seconds)
? seconds
: Math.Max(1, defaultSeconds);
}
}
}