Skip to content

Commit 05aa0ef

Browse files
Move UnixTime into gnmiext as time.go to align with per-type file naming
1 parent 2943ae0 commit 05aa0ef

2 files changed

Lines changed: 35 additions & 27 deletions

File tree

internal/provider/cisco/nxos/system.go

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ package nxos
55

66
import (
77
"context"
8-
"encoding/json"
98
"fmt"
10-
"strconv"
11-
"time"
129

1310
"google.golang.org/grpc"
1411

@@ -69,36 +66,14 @@ func (*BootPOAP) XPath() string {
6966
return "/System/boot-items/poap"
7067
}
7168

72-
type BootTime UnixTime
69+
type BootTime gnmiext.UnixTime
7370

7471
func (*BootTime) XPath() string {
7572
return "System/procsys-items/bootTime"
7673
}
7774

7875
func (t *BootTime) UnmarshalJSON(b []byte) error {
79-
return (*UnixTime)(t).UnmarshalJSON(b)
80-
}
81-
82-
// UnixTime is a wrapper around time.Time that marshals/unmarshals to/from a Unix timestamp in seconds.
83-
type UnixTime struct {
84-
time.Time `json:"-"`
85-
}
86-
87-
// UnmarshalJSON implements json.Unmarshaler.
88-
func (t *UnixTime) UnmarshalJSON(b []byte) error {
89-
var unix int64
90-
if err := json.Unmarshal(b, &unix); err != nil {
91-
var str string
92-
if err := json.Unmarshal(b, &str); err != nil {
93-
return fmt.Errorf("failed to unmarshal UnixTime: %w", err)
94-
}
95-
unix, err = strconv.ParseInt(str, 10, 64)
96-
if err != nil {
97-
return fmt.Errorf("failed to parse UnixTime string: %w", err)
98-
}
99-
}
100-
t.Time = time.Unix(unix, 0)
101-
return nil
76+
return (*gnmiext.UnixTime)(t).UnmarshalJSON(b)
10277
}
10378

10479
func Reboot(ctx context.Context, conn *grpc.ClientConn) error {

internal/transport/gnmiext/time.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// SPDX-FileCopyrightText: 2025 SAP SE or an SAP affiliate company and IronCore contributors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package gnmiext
5+
6+
import (
7+
"encoding/json"
8+
"fmt"
9+
"strconv"
10+
"time"
11+
)
12+
13+
// UnixTime is a wrapper around time.Time that marshals/unmarshals to/from a Unix timestamp in seconds.
14+
type UnixTime struct {
15+
time.Time `json:"-"`
16+
}
17+
18+
// UnmarshalJSON implements json.Unmarshaler.
19+
func (t *UnixTime) UnmarshalJSON(b []byte) error {
20+
var unix int64
21+
if err := json.Unmarshal(b, &unix); err != nil {
22+
var str string
23+
if err := json.Unmarshal(b, &str); err != nil {
24+
return fmt.Errorf("failed to unmarshal UnixTime: %w", err)
25+
}
26+
unix, err = strconv.ParseInt(str, 10, 64)
27+
if err != nil {
28+
return fmt.Errorf("failed to parse UnixTime string: %w", err)
29+
}
30+
}
31+
t.Time = time.Unix(unix, 0)
32+
return nil
33+
}

0 commit comments

Comments
 (0)