-
Notifications
You must be signed in to change notification settings - Fork 1
/
Adaptors.cs
82 lines (73 loc) · 2.89 KB
/
Adaptors.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
using Microsoft.SqlServer.Server;
using System;
using System.Data.SqlTypes;
using System.Globalization;
namespace MySQLCLRFunctions
{
public static class Adaptors
{
// Converts a hex string to a VARBINARY string, I think.
[SqlFunction(DataAccess = DataAccessKind.None, IsDeterministic = true, IsPrecise = true)]
public static string VarBin2Hex(SqlBytes InputAsHex)
{
if (InputAsHex == null) return null;
return BitConverter.ToString(InputAsHex.Buffer);
}
[SqlFunction(DataAccess = DataAccessKind.None, IsDeterministic = true, IsPrecise = true)]
public static DateTime? ADDateTimeString2DateTime(string InputAsStringDateTime)
{
if (InputAsStringDateTime == null) return null;
if (string.IsNullOrWhiteSpace(InputAsStringDateTime)) return null;
// 20021111182004.0Z
// 20021031003422
try
{
return DateTime.ParseExact(InputAsStringDateTime, "yyyyMMddHHmmss", CultureInfo.InvariantCulture);
}
catch (FormatException)
{
return null;
}
}
private static string[][] formats = new string[][]
{
new string [] {"yyyyMMddHHmmssfffffff", "21" }
, new string [] { "yyyyMMddHHmmssfff", "17" }
, new string [] {"yyyyMMddHHmmss", "14" }
, new string [] {"yyyyMMdd", "8" }
, new string [] { "yyMMdd", "6" }
, new string [] { "MMddyyyy", "8" }
, new string [] { "MMddyy", "6" }
, new string [] {"MM/dd/yy", "8" }
, new string [] {"MM/dd/yyyy", "10" }
, new string [] {"ddd dd MMM yyyy h:mm tt zzz", "0" }
, new string [] {"MMddyyyyHHmmss", "14" }
, new string [] {"dd/MM/yyyy HH:mm:ss.ffffff", "0"}
, new string [] {"d", "0" }
};
[SqlFunction(DataAccess = DataAccessKind.None, IsDeterministic = true, IsPrecise = true)]
public static DateTime? ToDate(string InputAsStringDateTime)
{
if (InputAsStringDateTime == null) return null;
if (string.IsNullOrWhiteSpace(InputAsStringDateTime)) return null;
// 20021111182004.0Z
// 20021031003422
foreach (string[] _format in formats)
{
try
{
if (_format[1] == "0")
return DateTime.ParseExact(InputAsStringDateTime, _format[0], CultureInfo.InvariantCulture);
else
{
return DateTime.ParseExact(InputAsStringDateTime, _format[0], CultureInfo.InvariantCulture);
}
}
catch (FormatException)
{
}
}
return null;
}
}
}