-
Notifications
You must be signed in to change notification settings - Fork 75
/
DateTimeHF.cs
230 lines (203 loc) · 5.37 KB
/
DateTimeHF.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
using System;
using System.Collections.Generic;
using System.Text;
using Waher.Content.Xml;
namespace Waher.Networking.XMPP.Synchronization
{
/// <summary>
/// Date and Time value based on the intenal high-frequency timer.
/// </summary>
public class DateTimeHF
{
private readonly int year;
private readonly int month;
private readonly int day;
private readonly int hour;
private readonly int minute;
private readonly int second;
private readonly int millisecond;
private readonly int microsecond;
private readonly int nanosecond100;
private readonly DateTimeKind kind;
private long? ticks;
/// <summary>
/// Date and Time value based on the intenal high-frequency timer.
/// </summary>
/// <param name="TP">DateTime-value</param>
/// <param name="Microsecond">Microsecond</param>
/// <param name="Nanosecond100">100-nanosecond</param>
/// <param name="Ticks">High-resolution timer ticks.</param>
public DateTimeHF(DateTime TP, int Microsecond, int Nanosecond100, long? Ticks)
: this(TP.Year, TP.Month, TP.Day, TP.Hour, TP.Minute, TP.Second, TP.Millisecond,
Microsecond, Nanosecond100, TP.Kind, Ticks)
{
}
/// <summary>
/// Date and Time value based on the intenal high-frequency timer.
/// </summary>
/// <param name="Year">Year</param>
/// <param name="Month">Month</param>
/// <param name="Day">Day</param>
/// <param name="Hour">Hour</param>
/// <param name="Minute">Minute</param>
/// <param name="Second">Second</param>
/// <param name="Millisecond">Millisecond</param>
/// <param name="Microsecond">Microsecond</param>
/// <param name="Nanosecond100">100-nanosecond</param>
/// <param name="Kind">Kind</param>
/// <param name="Ticks">High-resolution timer ticks.</param>
public DateTimeHF(int Year, int Month, int Day, int Hour, int Minute, int Second,
int Millisecond, int Microsecond, int Nanosecond100, DateTimeKind Kind, long? Ticks)
{
this.year = Year;
this.month = Month;
this.day = Day;
this.hour = Hour;
this.minute = Minute;
this.second = Second;
this.millisecond = Millisecond;
this.microsecond = Microsecond;
this.nanosecond100 = Nanosecond100;
this.kind = Kind;
this.ticks = Ticks;
}
/// <summary>
/// Year
/// </summary>
public int Year => this.year;
/// <summary>
/// Month
/// </summary>
public int Month => this.month;
/// <summary>
/// Day
/// </summary>
public int Day => this.day;
/// <summary>
/// Hour
/// </summary>
public int Hour => this.hour;
/// <summary>
/// Minute
/// </summary>
public int Minute => this.minute;
/// <summary>
/// Second
/// </summary>
public int Second => this.second;
/// <summary>
/// Millisecond
/// </summary>
public int Millisecond => this.millisecond;
/// <summary>
/// Microsecond
/// </summary>
public int Microsecond => this.microsecond;
/// <summary>
/// 100-nanosecond
/// </summary>
public int Nanosecond100 => this.nanosecond100;
/// <summary>
/// High-resolution timer ticks.
/// </summary>
public long? Ticks
{
get => this.ticks;
internal set => this.ticks = value;
}
/// <summary>
/// Tries to parse a high-frequency date and time value.
/// </summary>
/// <param name="s">String-representation</param>
/// <param name="Value">Parsed value, if successful.</param>
/// <returns>If value could be parsed.</returns>
public static bool TryParse(string s, out DateTimeHF Value)
{
int i = s.IndexOf('.');
if (i < 0)
{
if (XML.TryParse(s, out DateTime TP))
{
Value = new DateTimeHF(TP, 0, 0, null);
return true;
}
else
{
Value = null;
return false;
}
}
else
{
int j = i + 1;
int c = s.Length;
char ch;
while (j < c && (ch = s[j]) >= '0' && ch <= '9')
j++;
j -= i;
if (j <= 3)
{
if (XML.TryParse(s, out DateTime TP))
{
Value = new DateTimeHF(TP, 0, 0, null);
return true;
}
else
{
Value = null;
return false;
}
}
else
{
string s2;
j -= 3;
if (j < 4)
s2 = s.Substring(i + 4, j) + new string('0', 4 - j);
else if (j > 4)
s2 = s.Substring(i + 4, 4);
else
s2 = s.Substring(i + 4, j);
s = s.Remove(i + 4, j);
if (XML.TryParse(s, out DateTime TP) && int.TryParse(s2, out j))
{
Value = new DateTimeHF(TP, j / 10, j % 10, null);
return true;
}
else
{
Value = null;
return false;
}
}
}
}
/// <summary>
/// Normal Date and Time representation.
/// </summary>
public DateTime DateTime
{
get
{
return new DateTime(this.year, this.month, this.day,
this.hour, this.minute, this.second, this.millisecond, this.kind);
}
}
/// <summary>
/// Calculates the differente, in units of 100 ns, between two high-frequency
/// based date and time values.
/// </summary>
/// <param name="Timestamp1">Timestamp 1</param>
/// <param name="Timestamp2">Timestamp 2</param>
/// <returns>Difference, in units of 100 ns.</returns>
public static long operator -(DateTimeHF Timestamp1, DateTimeHF Timestamp2)
{
long Diff = (long)((Timestamp1.DateTime - Timestamp2.DateTime).TotalMilliseconds + 0.5);
Diff *= 1000;
Diff += (Timestamp1.microsecond - Timestamp2.microsecond);
Diff *= 10;
Diff += (Timestamp1.nanosecond100 - Timestamp2.nanosecond100);
return Diff;
}
}
}