-
Notifications
You must be signed in to change notification settings - Fork 0
/
Helpers.cs
191 lines (178 loc) · 5.12 KB
/
Helpers.cs
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using System;
using System.Collections.Generic;
using System.Text;
namespace TestConsole
{
class Helpers
{
public static int s = 1000;
public static int m = s * 60;
public static int h = m * 60;
public static int d = h * 24;
public static int w = d * 7;
public static double y = d * 365.25;
public static int undefined = int.MinValue;
/**
* Parse or format the given `val`.
*
* Options:
*
* - `long` verbose formatting [false]
*
* @param {String|Number} val
* @param {Object} [options]
* @throws {Error} throw an error if val is not a non-empty string or a number
* @return {String|Number}
* @api public
*/
//module.exports = public void(val, options)
//{
// options = options || { };
// var type = typeof val;
// if (type === 'string' && val.length > 0)
// {
// return parse(val);
// }
// else if (type === 'number' && isFinite(val))
// {
// return options.long? fmtLong(val) : fmtShort(val);
// }
// throw new Error(
// 'val is not a non-empty string or a valid number. val=' +
// JSON.stringify(val)
// );
//};
/**
* Parse the given `str` and return milliseconds.
*
* @param {String} str
* @return {Number}
* @api private
*/
public double parse(string str)
{
//str = String(str);
if (str.Length > 100)
{
return 0;
}
//var match = /^ (-? (?:\d +)?\.?\d +) *(milliseconds ?| msecs ?| ms | seconds ?| secs ?| s | minutes ?| mins ?| m | hours ?| hrs ?| h | days ?| d | weeks ?| w | years ?| yrs ?| y) ?$/ i.exec(
// str
// );
//if (!match)
//{
// return;
//}
//var n = parseFloat(match[1]);
double n = 0;
var type = "";// (match[2] || "ms").toLowerCase();
switch (type)
{
case "years":
case "year":
case "yrs":
case "yr":
case "y":
return n * y;
case "weeks":
case "week":
case "w":
return n * w;
case "days":
case "day":
case "d":
return n * d;
case "hours":
case "hour":
case "hrs":
case "hr":
case "h":
return n * h;
case "minutes":
case "minute":
case "mins":
case "min":
case "m":
return n * m;
case "seconds":
case "second":
case "secs":
case "sec":
case "s":
return n * s;
case "milliseconds":
case "millisecond":
case "msecs":
case "msec":
case "ms":
return n;
default:
return undefined;
}
}
/**
* Short format for `ms`.
*
* @param {Number} ms
* @return {String}
* @api private
*/
public string fmtShort(decimal ms)
{
var msAbs = Math.Abs(ms);
if (msAbs >= d)
{
return Math.Round(ms / d) + "d";
}
if (msAbs >= h)
{
return Math.Round(ms / h) + "h";
}
if (msAbs >= m)
{
return Math.Round(ms / m) + "m";
}
if (msAbs >= s)
{
return Math.Round(ms / s) + "s";
}
return ms + "ms";
}
/**
* Long format for `ms`.
*
* @param {Number} ms
* @return {String}
* @api private
*/
public string fmtLong(int ms)
{
var msAbs = Math.Abs(ms);
if (msAbs >= d)
{
return plural(ms, msAbs, d, "day");
}
if (msAbs >= h)
{
return plural(ms, msAbs, h, "hour");
}
if (msAbs >= m)
{
return plural(ms, msAbs, m, "minute");
}
if (msAbs >= s)
{
return plural(ms, msAbs, s, "second");
}
return ms + " ms";
}
/**
* Pluralization helper.
*/
public string plural(decimal ms, int msAbs,int n, string name)
{
var isPlural = msAbs >= n * 1.5;
return Math.Round(ms / n) + " " + name + (isPlural ? "s" : "");
}
}
}