-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParseHelper_ConfigHelper.cs
139 lines (109 loc) · 3.55 KB
/
ParseHelper_ConfigHelper.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
using System;
using System.Configuration;
using System.Linq;
namespace Clock
{
// grabbed these from NITE
public static class ConfigHelper
{
public static bool KeyExists(string key)
{
return ConfigurationManager.AppSettings.AllKeys.Contains(key);
}
public static string GetValue(string key)
{
return ConfigurationManager.AppSettings.Get(key);
}
public static bool GetValueBool(string key, bool defaultTo = false)
{
return GetValue(key).ToBool(defaultTo);
}
public static DateTime? GetValueDate(string key)
{
return GetValue(key).ToDate();
}
public static int? GetValueInt(string key)
{
return GetValue(key).ToInt();
}
}
public static class ParseHelper
{
#region Numeric and Bool
public static int? ToInt(this string input)
{
return int.TryParse(input, out var output)
? output
: (int?)null;
}
public static long? ToLong(this string input)
{
return long.TryParse(input, out var output)
? output
: (long?)null;
}
public static double? ToDouble(this string input)
{
return double.TryParse(input, out var output)
? output
: (double?)null;
}
public static decimal? ToDecimal(this string input)
{
return decimal.TryParse(input, out var output)
? output
: (decimal?)null;
}
//public static bool ToBool(this object input, bool @default = false)
//{
// if (input != null)
// {
// return ToBool(input.ToString(), @default);
// }
// return @default;
//}
public static bool ToBool(this string input, bool @default = false)
{
if (!bool.TryParse(input, out var output))
{
output = @default;
}
return output;
}
#endregion
#region Date
public static DateTime? ToDate(this string input)
{
return DateTime.TryParse(input, out var output)
? (DateTime?)output
: null;
}
public static DateTime? ToDate(this string input, string format)
{
return DateTime.TryParseExact(input, format, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AssumeLocal, out var output)
? (DateTime?)output
: null;
}
// https://stackoverflow.com/a/250400/
public static DateTime? UnixTimestampToDateTime(long? timestamp)
{
if (timestamp.HasValue)
{
var unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
return unixEpoch.AddSeconds(timestamp.Value / 1000).ToLocalTime();
}
return null;
}
#endregion
#region String
public static string NullIfEmpty(this string s)
{
return string.IsNullOrEmpty(s) ? null : s;
}
public static string NullIfWhiteSpace(this string s)
{
return string.IsNullOrWhiteSpace(s) ? null : s;
}
#endregion
}
}