Skip to content
This repository has been archived by the owner on Jan 23, 2025. It is now read-only.

Commit

Permalink
dynamically check the precision of the timestamp
Browse files Browse the repository at this point in the history
Signed-off-by: ianmuchyri <ianmuchiri8@gmail.com>
  • Loading branch information
ianmuchyri committed Mar 13, 2024
1 parent ae0fb85 commit 3815d6b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
38 changes: 18 additions & 20 deletions ui/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2656,11 +2656,27 @@ func parseTemplates(mfsdk sdk.SDK, prefix string) (tpl *template.Template, err e
}
return result
},
"unixTimeToHumanTime": func(t float64, precision int) string {
"unixTimeToHumanTime": func(t float64) string {
if t == 0 {
return ""
}
return time.Unix(adjustPrecision(int64(t), precision), 0).String()
currentPrecision := len(fmt.Sprintf("%d", int64(t)))
switch currentPrecision {
// Seconds precision
case len(fmt.Sprintf("%d", time.Now().Unix())):
return time.Unix(int64(t), 0).String()
// Milliseconds precision
case len(fmt.Sprintf("%d", time.Now().UnixMilli())):
return time.UnixMilli(int64(t)).String()
// Microseconds precision
case len(fmt.Sprintf("%d", time.Now().UnixMicro())):
return time.UnixMicro(int64(t)).String()
// Nanoseconds precision
case len(fmt.Sprintf("%d", time.Now().UnixNano())):
return time.Unix(0, int64(t)).String()
default:
return ""
}
},
"hasPermission": func(permissions []string, permission string) bool {
return slices.Contains(permissions, permission)
Expand Down Expand Up @@ -2704,21 +2720,3 @@ func parseTemplates(mfsdk sdk.SDK, prefix string) (tpl *template.Template, err e

return tpl.ParseFS(templatesFS, templates...)
}

// Function to adjust the precision of a timestamp
func adjustPrecision(timestamp int64, desiredPrecision int) int64 {
currentPrecision := len(fmt.Sprintf("%d", timestamp))
precisionDifference := desiredPrecision - currentPrecision
var adjustedTimestamp int64
switch {
case precisionDifference < 0:
adjustedTimestamp = timestamp / int64(math.Pow10(-precisionDifference))
case precisionDifference > 0:
adjustedTimestamp = timestamp * int64(math.Pow10(precisionDifference))
default:
adjustedTimestamp = timestamp

}

return adjustedTimestamp
}
2 changes: 1 addition & 1 deletion ui/web/templates/readmessages.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
{{ end }}
</td>
<td>{{ if $c.Sum }}{{ $c.Sum }}{{ end }}</td>
<td>{{ unixTimeToHumanTime $c.Time 10 }}</td>
<td>{{ unixTimeToHumanTime $c.Time }}</td>
</tr>
{{ end }}
</tbody>
Expand Down

0 comments on commit 3815d6b

Please sign in to comment.