From 0eb87c150a0bd2bc110665efc26c32c87ffe5289 Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Tue, 31 Oct 2023 08:35:55 +0100 Subject: [PATCH] Add Now() utility function Signed-off-by: Lorenzo --- ocpp1.6/types/datetime.go | 5 +++++ ocpp1.6_test/common_test.go | 6 ++++++ ocpp2.0.1/types/datetime.go | 5 +++++ ocpp2.0.1_test/common_test.go | 6 ++++++ 4 files changed, 22 insertions(+) diff --git a/ocpp1.6/types/datetime.go b/ocpp1.6/types/datetime.go index 498ae265..c451b0a5 100644 --- a/ocpp1.6/types/datetime.go +++ b/ocpp1.6/types/datetime.go @@ -24,6 +24,11 @@ func NewDateTime(time time.Time) *DateTime { return &DateTime{Time: time} } +// Creates a new DateTime struct, containing a time.Now() value. +func Now() *DateTime { + return &DateTime{Time: time.Now()} +} + func null(b []byte) bool { if len(b) != 4 { return false diff --git a/ocpp1.6_test/common_test.go b/ocpp1.6_test/common_test.go index dba35aa0..e7c342e2 100644 --- a/ocpp1.6_test/common_test.go +++ b/ocpp1.6_test/common_test.go @@ -176,3 +176,9 @@ func (suite *OcppV16TestSuite) TestMarshalDateTime() { suite.Equal(dt.ExpectedFormattedString, formatted) } } + +func (suite *OcppV16TestSuite) TestNowDateTime() { + now := types.Now() + suite.NotNil(now) + suite.True(time.Now().Sub(now.Time) < 1*time.Second) +} diff --git a/ocpp2.0.1/types/datetime.go b/ocpp2.0.1/types/datetime.go index 498ae265..c451b0a5 100644 --- a/ocpp2.0.1/types/datetime.go +++ b/ocpp2.0.1/types/datetime.go @@ -24,6 +24,11 @@ func NewDateTime(time time.Time) *DateTime { return &DateTime{Time: time} } +// Creates a new DateTime struct, containing a time.Now() value. +func Now() *DateTime { + return &DateTime{Time: time.Now()} +} + func null(b []byte) bool { if len(b) != 4 { return false diff --git a/ocpp2.0.1_test/common_test.go b/ocpp2.0.1_test/common_test.go index b3e0fe64..d3adf79f 100644 --- a/ocpp2.0.1_test/common_test.go +++ b/ocpp2.0.1_test/common_test.go @@ -338,3 +338,9 @@ func (suite *OcppV2TestSuite) TestMarshalDateTime() { suite.Equal(dt.ExpectedFormattedString, formatted) } } + +func (suite *OcppV2TestSuite) TestNowDateTime() { + now := types.Now() + suite.NotNil(now) + suite.True(time.Now().Sub(now.Time) < 1*time.Second) +}