-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
DateField.cs
60 lines (47 loc) · 2.11 KB
/
DateField.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
using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using SapNwRfc.Internal.Interop;
namespace SapNwRfc.Internal.Fields
{
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global", Justification = "Reflection use")]
internal sealed class DateField : Field<DateTime?>
{
private static readonly string ZeroRfcDateString = new string('0', 8);
private static readonly string EmptyRfcDateString = new string(' ', 8);
private static readonly string RfcDateFormat = "yyyyMMdd";
public DateField(string name, DateTime? value)
: base(name, value)
{
}
public override void Apply(RfcInterop interop, IntPtr dataHandle)
{
char[] buffer = (Value?.ToString(RfcDateFormat, CultureInfo.InvariantCulture) ?? ZeroRfcDateString).ToCharArray();
RfcResultCode resultCode = interop.SetDate(
dataHandle: dataHandle,
name: Name,
date: buffer,
errorInfo: out RfcErrorInfo errorInfo);
resultCode.ThrowOnError(errorInfo);
}
public static DateField Extract(RfcInterop interop, IntPtr dataHandle, string name)
{
char[] buffer = EmptyRfcDateString.ToCharArray();
RfcResultCode resultCode = interop.GetDate(
dataHandle: dataHandle,
name: name,
emptyDate: buffer,
errorInfo: out RfcErrorInfo errorInfo);
resultCode.ThrowOnError(errorInfo);
string dateString = new string(buffer);
if (dateString == EmptyRfcDateString || dateString == ZeroRfcDateString)
return new DateField(name, null);
if (!DateTime.TryParseExact(dateString, RfcDateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime date))
return new DateField(name, null);
return new DateField(name, date);
}
[ExcludeFromCodeCoverage]
public override string ToString()
=> Value.HasValue ? $"{Name} = {Value:yyyy-MM-dd}" : $"{Name} = No date";
}
}