Skip to content

Commit 78bc351

Browse files
Merge pull request #115 from ojusms/fix/shdr-utc-time-113
SHDR: ensure UTC-safe timestamps via ToUnixUTCTime to fix #113
2 parents 67d2be0 + 73c430a commit 78bc351

File tree

4 files changed

+43
-10
lines changed

4 files changed

+43
-10
lines changed

libraries/MTConnect.NET-Common/UnixTime.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,39 @@ public static long ToUnixTime(this DateTime d)
3535
}
3636

3737

38+
/// <summary>
39+
/// Convert a DateTime to Unix ticks (1/10,000 of a millisecond) ensuring the value is in UTC.
40+
/// If the DateTime.Kind is Local, it will be converted to UTC. If Unspecified, the value will be
41+
/// treated as UTC by default (for backwards compatibility) or as the specified kind, then converted to UTC.
42+
/// </summary>
43+
/// <param name="d">The DateTime to convert.</param>
44+
/// <param name="unspecifiedAssume">The kind to assume when DateTime.Kind is Unspecified. Defaults to Utc.</param>
45+
/// <returns>Unix ticks since epoch in UTC.</returns>
46+
public static long ToUnixUtcTime(this DateTime d, DateTimeKind unspecifiedAssume = DateTimeKind.Utc)
47+
{
48+
var x = d;
49+
if (x.Kind == DateTimeKind.Local)
50+
{
51+
x = x.ToUniversalTime();
52+
}
53+
else if (x.Kind == DateTimeKind.Unspecified)
54+
{
55+
// Specify the assumed kind, then convert to UTC if necessary
56+
x = DateTime.SpecifyKind(x, unspecifiedAssume);
57+
if (x.Kind == DateTimeKind.Local) x = x.ToUniversalTime();
58+
}
59+
60+
var duration = x - EpochTime;
61+
return duration.Ticks;
62+
}
63+
64+
/// <summary>
65+
/// Alias to <see cref="ToUnixUtcTime"/> to match requested API name.
66+
/// </summary>
67+
public static long ToUnixUTCTime(this DateTime d, DateTimeKind unspecifiedAssume = DateTimeKind.Utc)
68+
=> ToUnixUtcTime(d, unspecifiedAssume);
69+
70+
3871
public static DateTime ToDateTime(this long unixTicks)
3972
{
4073
return FromUnixTime(unixTicks);

libraries/MTConnect.NET-SHDR/Adapters/ShdrAdapter.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ public void AddDataItem(string dataItemKey, object value)
698698

699699
public void AddDataItem(string dataItemKey, object value, DateTime timestamp)
700700
{
701-
AddDataItem(dataItemKey, value, timestamp.ToUnixTime());
701+
AddDataItem(dataItemKey, value, timestamp.ToUnixUtcTime());
702702
}
703703

704704
public void AddDataItem(string dataItemKey, object value, long timestamp)
@@ -735,7 +735,7 @@ public bool SendDataItem(string dataItemKey, object value)
735735

736736
public bool SendDataItem(string dataItemKey, object value, DateTime timestamp)
737737
{
738-
return SendDataItem(dataItemKey, value, timestamp.ToUnixTime());
738+
return SendDataItem(dataItemKey, value, timestamp.ToUnixUtcTime());
739739
}
740740

741741
public bool SendDataItem(string dataItemKey, object value, long timestamp)
@@ -784,7 +784,7 @@ public void AddMessage(string messageId, string value)
784784

785785
public void AddMessage(string messageId, string value, DateTime timestamp)
786786
{
787-
AddMessage(messageId, value, timestamp.ToUnixTime());
787+
AddMessage(messageId, value, timestamp.ToUnixUtcTime());
788788
}
789789

790790
public void AddMessage(string messageId, string value, long timestamp)
@@ -799,7 +799,7 @@ public void AddMessage(string messageId, string value, string nativeCode)
799799

800800
public void AddMessage(string messageId, string value, string nativeCode, DateTime timestamp)
801801
{
802-
AddMessage(messageId, value, nativeCode, timestamp.ToUnixTime());
802+
AddMessage(messageId, value, nativeCode, timestamp.ToUnixUtcTime());
803803
}
804804

805805
public void AddMessage(string messageId, string value, string nativeCode, long timestamp)
@@ -831,7 +831,7 @@ public bool SendMessage(string dataItemId, string value)
831831

832832
public bool SendMessage(string dataItemId, string value, DateTime timestamp)
833833
{
834-
return SendMessage(dataItemId, value, timestamp.ToUnixTime());
834+
return SendMessage(dataItemId, value, timestamp.ToUnixUtcTime());
835835
}
836836

837837
public bool SendMessage(string dataItemId, string value, long timestamp)
@@ -846,7 +846,7 @@ public bool SendMessage(string dataItemId, string value, string nativeCode)
846846

847847
public bool SendMessage(string dataItemId, string value, string nativeCode, DateTime timestamp)
848848
{
849-
return SendMessage(dataItemId, value, nativeCode, timestamp.ToUnixTime());
849+
return SendMessage(dataItemId, value, nativeCode, timestamp.ToUnixUtcTime());
850850
}
851851

852852
public bool SendMessage(string dataItemId, string value, string nativeCode, long timestamp)

libraries/MTConnect.NET-SHDR/Shdr/ShdrDataItem.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public ShdrDataItem(string dataItemKey, object value, DateTime timestamp)
6666
{
6767
new ObservationValue(ValueKeys.Result, value != null ? value.ToString() : string.Empty)
6868
};
69-
Timestamp = timestamp.ToUnixTime();
69+
Timestamp = timestamp.ToUnixUtcTime();
7070
}
7171

7272

@@ -350,7 +350,7 @@ public static IEnumerable<ShdrDataItem> FromString(string input, bool uppercaseV
350350
if (timestamp.HasValue)
351351
{
352352
var y = ShdrLine.GetNextSegment(input);
353-
return FromKeyValuePairs(y, timestamp.Value.ToUnixTime(), duration.HasValue ? duration.Value : 0, uppercaseValue);
353+
return FromKeyValuePairs(y, timestamp.Value.ToUnixUtcTime(), duration.HasValue ? duration.Value : 0, uppercaseValue);
354354
}
355355
else
356356
{

libraries/MTConnect.NET-SHDR/Shdr/ShdrMessage.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public ShdrMessage(string dataItemKey, string value, DateTime timestamp)
8282
{
8383
new ObservationValue(ValueKeys.Result, value != null ? value.ToString() : string.Empty)
8484
};
85-
Timestamp = timestamp.ToUnixTime();
85+
Timestamp = timestamp.ToUnixUtcTime();
8686
}
8787

8888
public ShdrMessage(string dataItemKey, string value, string nativeCode, DateTime timestamp)
@@ -92,7 +92,7 @@ public ShdrMessage(string dataItemKey, string value, string nativeCode, DateTime
9292
values.Add(new ObservationValue(ValueKeys.Result, value != null ? value.ToString() : string.Empty));
9393
if (!string.IsNullOrEmpty(nativeCode)) values.Add(new ObservationValue(ValueKeys.NativeCode, nativeCode));
9494
Values = values;
95-
Timestamp = timestamp.ToUnixTime();
95+
Timestamp = timestamp.ToUnixUtcTime();
9696
}
9797

9898
public ShdrMessage(IObservationInput observation)

0 commit comments

Comments
 (0)