Skip to content

Commit 4425e00

Browse files
committed
Formatted TS
1 parent d472177 commit 4425e00

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

internal/ota-api/dto.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717

1818
package otaapi
1919

20-
import "fmt"
20+
import (
21+
"fmt"
22+
"time"
23+
)
2124

2225
type (
2326
OtaStatusResponse struct {
@@ -49,28 +52,39 @@ type (
4952
OtaStatusPrint struct {
5053
Ota Ota `json:"ota"`
5154
Details []State `json:"details,omitempty"`
52-
}
55+
}
5356
)
5457

5558
func (o Ota) ToLine() string {
5659
if o.ErrorReason != "" {
57-
return fmt.Sprintf("ota_id: %s, started_at: %s, ended_at: %s, status: %s, error_reason: %s", o.ID, o.StartedAt, o.EndedAt, o.Status, o.ErrorReason)
60+
return fmt.Sprintf("ota_id: %s, started_at: %s, ended_at: %s, status: %s, error_reason: %s", o.ID, formatHumanReadableTs(o.StartedAt), formatHumanReadableTs(o.EndedAt), o.Status, o.ErrorReason)
5861
}
59-
return fmt.Sprintf("ota_id: %s, started_at: %s, ended_at: %s, status: %s", o.ID, o.StartedAt, o.EndedAt, o.Status)
62+
return fmt.Sprintf("ota_id: %s, started_at: %s, ended_at: %s, status: %s", o.ID, formatHumanReadableTs(o.StartedAt), formatHumanReadableTs(o.EndedAt), o.Status)
6063
}
6164

6265
func (o Ota) ToText() string {
63-
out := fmt.Sprintf("ota_id: %s\nstatus: %s\nstarted_at: %s\nended_at: %s\ndevice_id: %s", o.ID, o.Status, o.StartedAt, o.EndedAt, o.DeviceID)
66+
out := fmt.Sprintf("ota_id: %s\nstatus: %s\nstarted_at: %s\nended_at: %s\ndevice_id: %s", o.ID, o.Status, formatHumanReadableTs(o.StartedAt), formatHumanReadableTs(o.EndedAt), o.DeviceID)
6467
if o.ErrorReason != "" {
6568
out += fmt.Sprintf("\nerror_reason: %s", o.ErrorReason)
6669
}
6770
return out
6871
}
6972

7073
func (s State) ToText() string {
71-
rep := fmt.Sprintf("time: %s, status: %s", s.Timestamp, s.State)
74+
rep := fmt.Sprintf("time: %s, status: %s", formatHumanReadableTs(s.Timestamp), s.State)
7275
if s.StateData != "" {
7376
rep += fmt.Sprintf(", detail: %s", s.StateData)
7477
}
7578
return rep
7679
}
80+
81+
func formatHumanReadableTs(ts string) string {
82+
if ts == "" {
83+
return ""
84+
}
85+
parsed, err := time.Parse(time.RFC3339Nano, ts)
86+
if err != nil {
87+
return ts
88+
}
89+
return parsed.Format(time.RFC3339)
90+
}

0 commit comments

Comments
 (0)