-
Notifications
You must be signed in to change notification settings - Fork 1
/
FormatHelper.cs
117 lines (98 loc) · 3.99 KB
/
FormatHelper.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
using System;
using System.ComponentModel;
using System.Globalization;
using System.Net;
using System.Reflection;
using PVOutput.Net.Enums;
namespace PVOutput.Net.Objects.Core
{
internal static class FormatHelper
{
internal static DateTime ParseDate(string dateString)
{
return DateTime.ParseExact(dateString, new string[] { "yyyyMMdd", "yyyyMM", "yyyy" }, CultureInfo.InvariantCulture.DateTimeFormat, DateTimeStyles.AssumeLocal);
}
internal static DateTimeOffset ParseTimeStamp(string timestamp)
{
return DateTimeOffset.Parse(timestamp, null, DateTimeStyles.RoundtripKind);
}
internal static DateTime? ParseOptionalDate(string dateString)
{
if (string.IsNullOrEmpty(dateString))
return null;
return ParseDate(dateString);
}
internal static TimeSpan ParseTime(string timeString)
{
return TimeSpan.ParseExact(timeString, "h\\:mm", CultureInfo.InvariantCulture, TimeSpanStyles.None);
}
internal static string GetDateAsString(DateTime date)
{
return date.ToString("yyyyMMdd", CultureInfo.InvariantCulture);
}
internal static string GetTimeAsString(DateTime date)
{
return date.ToString("HH:mm", CultureInfo.InvariantCulture.DateTimeFormat);
}
internal static string GetTimeAsString(TimeSpan time)
{
return time.ToString("h\\:mm", CultureInfo.InvariantCulture.DateTimeFormat);
}
internal static string GetValueAsString<TInputType>(TInputType? value) where TInputType : struct
{
if (value == null)
{
return null;
}
return (string)Convert.ChangeType(value, typeof(string), CultureInfo.CreateSpecificCulture("en-US"));
}
internal static TResultType? GetValue<TResultType>(string value) where TResultType : struct
{
if (string.IsNullOrEmpty(value) || value.Equals("NaN", StringComparison.OrdinalIgnoreCase))
{
return null;
}
return (TResultType)Convert.ChangeType(value, typeof(TResultType), CultureInfo.CreateSpecificCulture("en-US"));
}
internal static TResultType GetValueOrDefault<TResultType>(string value) where TResultType : struct
{
TResultType? result = GetValue<TResultType>(value);
return result ?? (default);
}
internal static string GetEnumerationDescription<TEnumType>(this TEnumType enumerationValue) where TEnumType : struct
{
Type type = enumerationValue.GetType();
if (!type.IsEnum)
{
throw new ArgumentException("EnumerationValue must be of Enum type", nameof(enumerationValue));
}
var name = Enum.GetName(type, enumerationValue);
if (name != null)
{
FieldInfo field = type.GetField(name);
if (field != null)
{
if (Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) is DescriptionAttribute attr)
{
return attr.Description;
}
}
}
return null;
}
public static TEnumType DescriptionToEnumValue<TEnumType>(this string enumerationDescription) where TEnumType : struct
{
Type type = typeof(TEnumType);
if (!type.IsEnum)
{
throw new ArgumentException("Type parameter must be of Enum type");
}
foreach (TEnumType val in Enum.GetValues(type))
{
if (val.GetEnumerationDescription() == enumerationDescription)
return val;
}
throw new ArgumentException($"Invalid description '{enumerationDescription}' for enum " + type.Name, nameof(enumerationDescription));
}
}
}