fix(mqtt): #1044 plain DateTime writes publish ISO-8601 UTC, not the invariant MM/dd/yyyy form
This commit is contained in:
@@ -560,7 +560,7 @@ every MQTT node materialized read-only regardless of what the operator authored.
|
||||
|
||||
| Mode | Destination | Payload |
|
||||
|---|---|---|
|
||||
| **Plain** | the tag's `writeTopic` | the value as UTF-8 text (`Raw`/`Scalar`; a Boolean is `true`/`false`, lower-case — #988 M-15, it was .NET's `True`/`False`) or a bare JSON literal (`Json`) |
|
||||
| **Plain** | the tag's `writeTopic` | the value as UTF-8 text (`Raw`/`Scalar`; a Boolean is `true`/`false`, lower-case — #988 M-15, it was .NET's `True`/`False`; a DateTime/DateTimeOffset is ISO-8601 UTC round-trip form, `2026-08-10T12:30:45.1230000Z`, an Unspecified kind read as UTC — #1044, it was `08/10/2026 12:30:45`) or a bare JSON literal (`Json`) |
|
||||
| **Sparkplug B** | `spBv1.0/{group}/DCMD/{node}/{device}` for a device metric, `spBv1.0/{group}/NCMD/{node}` for an edge-node metric | a Sparkplug `Payload` carrying the metric **by name**, with an explicit `Datatype` and no `seq` |
|
||||
|
||||
Both publish with `retain: false`. A **Plain** write goes at **QoS 1** (STAB-26): the publish completes only
|
||||
|
||||
@@ -192,6 +192,14 @@ public static class MqttWriteEncoder
|
||||
text = value switch
|
||||
{
|
||||
bool b => b ? "true" : "false",
|
||||
// #1044: a DateTime through IFormattable published "08/10/2026 12:30:45" (month-first, no
|
||||
// zone) — ambiguous and unparseable by most devices. ISO-8601 UTC round-trip form, with the
|
||||
// same Unspecified-is-UTC rule the Sparkplug DateTime encode uses so two nodes of a
|
||||
// redundant pair publish the same text for the same authored value.
|
||||
DateTime dt => (dt.Kind == DateTimeKind.Unspecified
|
||||
? DateTime.SpecifyKind(dt, DateTimeKind.Utc)
|
||||
: dt.ToUniversalTime()).ToString("O", CultureInfo.InvariantCulture),
|
||||
DateTimeOffset dto => dto.UtcDateTime.ToString("O", CultureInfo.InvariantCulture),
|
||||
IFormattable f => f.ToString(null, CultureInfo.InvariantCulture),
|
||||
_ => value.ToString() ?? "",
|
||||
};
|
||||
|
||||
@@ -422,6 +422,46 @@ public sealed class MqttReview20261202Tests
|
||||
Encoding.UTF8.GetString(encoded.Payload).ShouldBe(expected);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------
|
||||
// #1044 — Plain Scalar/Raw DateTime writes publish ISO-8601 UTC, not the culture-style "MM/dd/yyyy HH:mm:ss"
|
||||
// ---------------------------------------------------------------------------------
|
||||
|
||||
[Theory]
|
||||
[InlineData("Scalar", DateTimeKind.Utc)]
|
||||
[InlineData("Raw", DateTimeKind.Utc)]
|
||||
[InlineData("Scalar", DateTimeKind.Unspecified)]
|
||||
public void DateTime_writes_publish_iso8601_utc(string format, DateTimeKind kind)
|
||||
{
|
||||
MqttTagDefinitionFactory.FromTagConfig(
|
||||
$$"""{"topic":"t/d","writeTopic":"t/d/set","payloadFormat":"{{format}}","dataType":"DateTime"}""",
|
||||
"P/d",
|
||||
out var def).ShouldBeTrue();
|
||||
|
||||
var value = new DateTime(2026, 8, 10, 12, 30, 45, 123, kind);
|
||||
MqttWriteEncoder.TryEncode(MqttMode.Plain, def, value, out var encoded).ShouldBe(MqttWriteEncodeError.None);
|
||||
|
||||
var text = Encoding.UTF8.GetString(encoded.Payload);
|
||||
text.ShouldBe("2026-08-10T12:30:45.1230000Z");
|
||||
DateTime.Parse(text, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.RoundtripKind)
|
||||
.ShouldBe(DateTime.SpecifyKind(value, DateTimeKind.Utc));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DateTimeOffset_and_local_datetime_writes_publish_the_same_utc_instant()
|
||||
{
|
||||
MqttTagDefinitionFactory.FromTagConfig(
|
||||
"""{"topic":"t/d","writeTopic":"t/d/set","payloadFormat":"Scalar","dataType":"DateTime"}""",
|
||||
"P/d",
|
||||
out var def).ShouldBeTrue();
|
||||
|
||||
var offset = new DateTimeOffset(2026, 8, 10, 14, 30, 45, TimeSpan.FromHours(2));
|
||||
MqttWriteEncoder.TryEncode(MqttMode.Plain, def, offset, out var a).ShouldBe(MqttWriteEncodeError.None);
|
||||
Encoding.UTF8.GetString(a.Payload).ShouldBe("2026-08-10T12:30:45.0000000Z");
|
||||
|
||||
MqttWriteEncoder.TryEncode(MqttMode.Plain, def, offset.LocalDateTime, out var b).ShouldBe(MqttWriteEncodeError.None);
|
||||
Encoding.UTF8.GetString(b.Payload).ShouldBe("2026-08-10T12:30:45.0000000Z");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A fake SUBSCRIBE transport that throws a client-side protocol violation for any batch carrying
|
||||
/// <c>poison</c> — the shape MQTTnet produces for a malformed filter — or a session failure.
|
||||
|
||||
Reference in New Issue
Block a user