Skip to content
Open
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
8 changes: 8 additions & 0 deletions charger/ocpp/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,18 @@ import (
"github.com/lorenzodonini/ocpp-go/ocpp1.6/remotetrigger"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/security"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/smartcharging"
"github.com/lorenzodonini/ocpp-go/ocpp1.6/types"
"github.com/lorenzodonini/ocpp-go/ocppj"
"github.com/lorenzodonini/ocpp-go/ws"
)

// dateTimeFormat sets the OCPP dateTime serialization to RFC3339 with fractional seconds
const dateTimeFormat = "2006-01-02T15:04:05.000Z07:00"

func init() {
types.DateTimeFormat = dateTimeFormat
}

type Config struct {
Port int `json:"port"`
}
Expand Down
19 changes: 19 additions & 0 deletions charger/ocpp/instance_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package ocpp

import (
"encoding/json"
"os"
"testing"
"time"

"github.com/lorenzodonini/ocpp-go/ocpp1.6/types"
"github.com/stretchr/testify/require"
)

func TestMain(m *testing.M) {
Expand All @@ -28,3 +33,17 @@ func TestExternalUrl(t *testing.T) {
}
}
}

func TestDateTimeFormatHasFractionalSeconds(t *testing.T) {
require.Equal(t, dateTimeFormat, types.DateTimeFormat)

ts := types.NewDateTime(time.Date(2026, 6, 23, 12, 45, 9, 404_000_000, time.UTC))
b, err := json.Marshal(ts)
require.NoError(t, err)
require.JSONEq(t, `"2026-06-23T12:45:09.404Z"`, string(b))
Comment on lines +41 to +43

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (testing): Add a round-trip (marshal + unmarshal) test to ensure deserialization remains compatible

Right now we only assert the marshalled JSON string. Please also unmarshal that JSON back into types.DateTime (or the relevant type) and assert it matches the original time (accounting for location if needed) so we verify decode as well as encode with fractional seconds.

Suggested implementation:

func TestMain(m *testing.M) {
		}
	}
}

func TestDateTimeFormatHasFractionalSeconds(t *testing.T) {
	require.Equal(t, dateTimeFormat, types.DateTimeFormat)

	original := types.NewDateTime(time.Date(2026, 6, 23, 12, 45, 9, 404_000_000, time.UTC))

	// Marshal to JSON and verify the wire format (including fractional seconds)
	b, err := json.Marshal(original)
	require.NoError(t, err)
	require.JSONEq(t, `"2026-06-23T12:45:09.404Z"`, string(b))

	// Unmarshal back and ensure we get the same instant in time
	var roundTripped types.DateTime
	err = json.Unmarshal(b, &roundTripped)
	require.NoError(t, err)

	// Compare underlying time values to verify round-trip correctness
	require.True(t, time.Time(original).Equal(time.Time(roundTripped)))
}

The assertion time.Time(original).Equal(time.Time(roundTripped)) assumes that types.DateTime is defined as type DateTime time.Time. If the actual definition differs (for example, if it is a struct wrapper or exposes a method to retrieve the underlying time.Time), adjust the comparison accordingly, e.g. original.Time().Equal(roundTripped.Time()) or another equivalent accessor.


// the emitted format must round-trip back to the same instant
var got types.DateTime
require.NoError(t, json.Unmarshal(b, &got))
require.True(t, ts.Equal(got.Time), "round-trip mismatch: %s != %s", ts, got)
}
Loading