Skip to content

Commit

Permalink
Add Now() utility function
Browse files Browse the repository at this point in the history
Signed-off-by: Lorenzo <lorenzo.donini90@gmail.com>
  • Loading branch information
lorenzodonini committed Oct 31, 2023
1 parent bc0ceee commit d214d7c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ocpp1.6/types/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ func NewDateTime(time time.Time) *DateTime {
return &DateTime{Time: time}
}

// Creates a new DateTime struct, containing a time.Now() value.

This comment has been minimized.

Copy link
@andig

andig Dec 3, 2023

Contributor

@lorenzodonini maybe this would be an opportunity to declare common types and have 1.6/2.0 alias to those common types?

func Now() *DateTime {
return &DateTime{Time: time.Now()}
}

func null(b []byte) bool {
if len(b) != 4 {
return false
Expand Down
6 changes: 6 additions & 0 deletions ocpp1.6_test/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
5 changes: 5 additions & 0 deletions ocpp2.0.1/types/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

This comment has been minimized.

Copy link
@andig

andig Dec 3, 2023

Contributor

Out of curiosity: isn't this simply

return string(b) == "null"

?

if len(b) != 4 {
return false
Expand Down
6 changes: 6 additions & 0 deletions ocpp2.0.1_test/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit d214d7c

Please sign in to comment.