Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions charger/ocpp/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,21 @@ 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 fixes the OCPP dateTime serialization to RFC3339 with a
// fractional-second component for all chargers. Sub-second precision is valid
// RFC3339/ISO8601 and is required by some chargers (e.g. Webasto/Ampure NEXT)
// that reject timestamps without it, while remaining compatible with all others.
const dateTimeFormat = "2006-01-02T15:04:05.000Z07:00"

func init() {
types.DateTimeFormat = dateTimeFormat
}

type Config struct {
Port int `json:"port"`
}
Expand Down
14 changes: 14 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,12 @@ 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.

}
Loading