Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions libraries/MTConnect.NET-Common/UnixTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,39 @@ public static long ToUnixTime(this DateTime d)
}


/// <summary>
/// Convert a DateTime to Unix ticks (1/10,000 of a millisecond) ensuring the value is in UTC.
/// If the DateTime.Kind is Local, it will be converted to UTC. If Unspecified, the value will be
/// treated as UTC by default (for backwards compatibility) or as the specified kind, then converted to UTC.
/// </summary>
/// <param name="d">The DateTime to convert.</param>
/// <param name="unspecifiedAssume">The kind to assume when DateTime.Kind is Unspecified. Defaults to Utc.</param>
/// <returns>Unix ticks since epoch in UTC.</returns>
public static long ToUnixUtcTime(this DateTime d, DateTimeKind unspecifiedAssume = DateTimeKind.Utc)
{
var x = d;
if (x.Kind == DateTimeKind.Local)
{
x = x.ToUniversalTime();
}
else if (x.Kind == DateTimeKind.Unspecified)
{
// Specify the assumed kind, then convert to UTC if necessary
x = DateTime.SpecifyKind(x, unspecifiedAssume);
if (x.Kind == DateTimeKind.Local) x = x.ToUniversalTime();
}

var duration = x - EpochTime;
return duration.Ticks;
}

/// <summary>
/// Alias to <see cref="ToUnixUtcTime"/> to match requested API name.
/// </summary>
public static long ToUnixUTCTime(this DateTime d, DateTimeKind unspecifiedAssume = DateTimeKind.Utc)
=> ToUnixUtcTime(d, unspecifiedAssume);


public static DateTime ToDateTime(this long unixTicks)
{
return FromUnixTime(unixTicks);
Expand Down
12 changes: 6 additions & 6 deletions libraries/MTConnect.NET-SHDR/Adapters/ShdrAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ public void AddDataItem(string dataItemKey, object value)

public void AddDataItem(string dataItemKey, object value, DateTime timestamp)
{
AddDataItem(dataItemKey, value, timestamp.ToUnixTime());
AddDataItem(dataItemKey, value, timestamp.ToUnixUtcTime());
}

public void AddDataItem(string dataItemKey, object value, long timestamp)
Expand Down Expand Up @@ -735,7 +735,7 @@ public bool SendDataItem(string dataItemKey, object value)

public bool SendDataItem(string dataItemKey, object value, DateTime timestamp)
{
return SendDataItem(dataItemKey, value, timestamp.ToUnixTime());
return SendDataItem(dataItemKey, value, timestamp.ToUnixUtcTime());
}

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

public void AddMessage(string messageId, string value, DateTime timestamp)
{
AddMessage(messageId, value, timestamp.ToUnixTime());
AddMessage(messageId, value, timestamp.ToUnixUtcTime());
}

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

public void AddMessage(string messageId, string value, string nativeCode, DateTime timestamp)
{
AddMessage(messageId, value, nativeCode, timestamp.ToUnixTime());
AddMessage(messageId, value, nativeCode, timestamp.ToUnixUtcTime());
}

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

public bool SendMessage(string dataItemId, string value, DateTime timestamp)
{
return SendMessage(dataItemId, value, timestamp.ToUnixTime());
return SendMessage(dataItemId, value, timestamp.ToUnixUtcTime());
}

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

public bool SendMessage(string dataItemId, string value, string nativeCode, DateTime timestamp)
{
return SendMessage(dataItemId, value, nativeCode, timestamp.ToUnixTime());
return SendMessage(dataItemId, value, nativeCode, timestamp.ToUnixUtcTime());
}

public bool SendMessage(string dataItemId, string value, string nativeCode, long timestamp)
Expand Down
4 changes: 2 additions & 2 deletions libraries/MTConnect.NET-SHDR/Shdr/ShdrDataItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public ShdrDataItem(string dataItemKey, object value, DateTime timestamp)
{
new ObservationValue(ValueKeys.Result, value != null ? value.ToString() : string.Empty)
};
Timestamp = timestamp.ToUnixTime();
Timestamp = timestamp.ToUnixUtcTime();
}


Expand Down Expand Up @@ -350,7 +350,7 @@ public static IEnumerable<ShdrDataItem> FromString(string input, bool uppercaseV
if (timestamp.HasValue)
{
var y = ShdrLine.GetNextSegment(input);
return FromKeyValuePairs(y, timestamp.Value.ToUnixTime(), duration.HasValue ? duration.Value : 0, uppercaseValue);
return FromKeyValuePairs(y, timestamp.Value.ToUnixUtcTime(), duration.HasValue ? duration.Value : 0, uppercaseValue);
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions libraries/MTConnect.NET-SHDR/Shdr/ShdrMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public ShdrMessage(string dataItemKey, string value, DateTime timestamp)
{
new ObservationValue(ValueKeys.Result, value != null ? value.ToString() : string.Empty)
};
Timestamp = timestamp.ToUnixTime();
Timestamp = timestamp.ToUnixUtcTime();
}

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

public ShdrMessage(IObservationInput observation)
Expand Down