diff options
| author | Jake Mannens <jake@asger.xyz> | 2023-09-27 03:38:58 +1000 |
|---|---|---|
| committer | Jake Mannens <jake@asger.xyz> | 2023-09-27 03:38:58 +1000 |
| commit | bc82b2dc2f7405c0fd4d179830412ea8209137b1 (patch) | |
| tree | 26fdaa8635a1487e3ae9fd112336be8df0f723ec /Util/Extensions.cs | |
| parent | 39eead0052215d7be4f49906e987fef7fb0c700b (diff) | |
Added MemoryCache class and implemented principal/ACL cache in the security service
Diffstat (limited to 'Util/Extensions.cs')
| -rw-r--r-- | Util/Extensions.cs | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/Util/Extensions.cs b/Util/Extensions.cs new file mode 100644 index 0000000..bbf9a12 --- /dev/null +++ b/Util/Extensions.cs @@ -0,0 +1,33 @@ +namespace HyperBooru.Util; + +public static class Extensions { + public static readonly string[] MagnitudeOrders = new[] { + "K", "M", "G", "T", "P", "E", "Z", "Y", "R", "Q" + }; + + public static DateTime? TryParseDateTimeUtc(this string s) { + bool success = DateTime.TryParse(s, out var dateTime); + return success ? DateTime.SpecifyKind(dateTime, DateTimeKind.Utc) : null; + } + + public static string ToBytesSI(this long x) { + var exp = (int) Math.Log10(x); + var suffix = MagnitudeOrders.ElementAtOrDefault(exp / 3 - 1); + if(suffix is null) + return x.ToString(); + double n = x / Math.Pow(10, exp / 3 * 3); + return $"{Math.Round(n, 2 - (exp % 3))} {suffix}B"; + } + + public static string ToStringHumanReadable(this TimeSpan t) { + if(t.TotalMilliseconds < 1000) + return string.Format("{0:0}ms", t.TotalMilliseconds); + if(t.TotalSeconds < 60) + return string.Format("{0:0.00}s", t.TotalSeconds); + if(t.TotalMinutes < 60) + return string.Format("{0:0}m{0:0}s", t.TotalMinutes, t.Seconds); + if(t.TotalHours < 24) + return string.Format("{0:0}h{0:0}m", t.TotalHours, t.Minutes); + return string.Format("{0:0.00}d", t.TotalDays); + } +} |
