Skip to content

Commit

Permalink
Allows serialization of time without zone (#97)
Browse files Browse the repository at this point in the history
* Allows serialization of time without zone

* Fixes test

* Adds test with timezone
  • Loading branch information
rkodev committed Jul 14, 2023
1 parent 4524db0 commit 0c3dfe5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

## [1.0.4] - 2023-07-12

### Changed

- Fixes parsing time parsing without timezone information.

## [1.0.3] - 2023-06-28

### Changed
Expand Down
5 changes: 5 additions & 0 deletions json_parse_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,11 @@ func (n *JsonParseNode) GetTimeValue() (*time.Time, error) {
if v == nil {
return nil, nil
}

// if string does not have timezone information, add local timezone
if len(*v) == 19 {
*v = *v + time.Now().Format("-07:00")
}
parsed, err := time.Parse(time.RFC3339, *v)
return &parsed, err
}
Expand Down
25 changes: 25 additions & 0 deletions json_parse_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,31 @@ func TestFunctional(t *testing.T) {
}
}

func TestParsingTime(t *testing.T) {
source := `{
"noZone": "2023-07-12T08:54:24",
"withZone": "2023-07-12T09:54:24+03:00"
}`

sourceArray := []byte(source)
parseNode, err := NewJsonParseNode(sourceArray)
if err != nil {
t.Errorf("Error creating parse node: %s", err.Error())
}

someProp, err := parseNode.GetChildNode("noZone")
assert.Nil(t, err)
time1, err := someProp.GetTimeValue()
assert.Nil(t, err)
assert.Contains(t, time1.String(), "2023-07-12 08:54:24 +")

someProp2, err := parseNode.GetChildNode("withZone")
assert.Nil(t, err)
time2, err := someProp2.GetTimeValue()
assert.Nil(t, err)
assert.Contains(t, time2.String(), "2023-07-12 09:54:24 +")
}

func TestThrowErrorOfPrimitiveType(t *testing.T) {
source := `{
"id": "2",
Expand Down

0 comments on commit 0c3dfe5

Please sign in to comment.