SHDR: ensure UTC-safe timestamps via ToUnixUTCTime to fix #113 #115
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
[SHDR] Ensure UTC-safe timestamps (fixes #113)
Summary
SHDR timestamps were offset from local time (e.g., +5 hours) due to ambiguous
DateTime
kinds and conversions. This PR introduces a UTC-safe conversion method and updates SHDR code paths to use it, ensuring consistent UTC timestamps across environments.Fixes: #113
Problem
DateTime
withKind=Unspecified
orLocal
led to non-UTC epoch tick calculations.Example from the issue:
Using
LocalDateTime
(or anUnspecified
time) will shift the UTC reference.Changes
Added UTC-safe conversion helpers in libraries/MTConnect.NET-Common/UnixTime.cs:
Updated SHDR code to use UTC-safe conversions:
DateTime
with ToUnixUtcTime() to avoid local ambiguity.No changes were made to formatting of outbound SHDR strings, which continue to use ISO 8601 with a UTC basis via ToString("o") on UTC
DateTime
.Why this works
DateTime
instances to UTC before computing Unix ticks, the resulting timestamps are consistent regardless of host machine timezone orDateTime.Kind
.Backwards compatibility
Unspecified
is UTC. If a caller truly intends local time, they can call ToUnixUtcTime(DateTimeKind.Local).Testing
Utc
,Local
, andUnspecified
.Affected areas
Risks
Unspecified
as Local implicitly, behavior will now be UTC by default. This is mitigated by allowing the caller to specifyunspecifiedAssume
explicitly if needed.Checklist
Example usage