-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
TimeField.cs
60 lines (47 loc) · 2.03 KB
/
TimeField.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 TimeField : Field<TimeSpan?>
{
private static readonly string ZeroRfcTimeString = new string('0', 6);
private static readonly string EmptyRfcTimeString = new string(' ', 6);
private static readonly string RfcTimeFormat = "hhmmss";
public TimeField(string name, TimeSpan? value)
: base(name, value)
{
}
public override void Apply(RfcInterop interop, IntPtr dataHandle)
{
char[] buffer = (Value?.ToString(RfcTimeFormat, CultureInfo.InvariantCulture) ?? ZeroRfcTimeString).ToCharArray();
RfcResultCode resultCode = interop.SetTime(
dataHandle: dataHandle,
name: Name,
time: buffer,
out RfcErrorInfo errorInfo);
resultCode.ThrowOnError(errorInfo);
}
public static TimeField Extract(RfcInterop interop, IntPtr dataHandle, string name)
{
char[] buffer = EmptyRfcTimeString.ToCharArray();
RfcResultCode resultCode = interop.GetTime(
dataHandle: dataHandle,
name: name,
emptyTime: buffer,
out RfcErrorInfo errorInfo);
resultCode.ThrowOnError(errorInfo);
string timeString = new string(buffer);
if (timeString == EmptyRfcTimeString || timeString == ZeroRfcTimeString)
return new TimeField(name, null);
if (!TimeSpan.TryParseExact(timeString, RfcTimeFormat, CultureInfo.InvariantCulture, out TimeSpan time))
return new TimeField(name, null);
return new TimeField(name, time);
}
[ExcludeFromCodeCoverage]
public override string ToString()
=> $"{Name} = {Value:hh:mm:ss}";
}
}