glance
glance copied to clipboard
parseRelativeTime has an "in" for days only, e.g., "in 5d"
Description
parseRelativeTime is giving me "in 5d" instead of "5d". However, "3h" is "3h", same with "5y".
{{/* --- Upcoming Payment Section --- */}}
<div class="flex items-center justify-center margin-top-15 size-h5 ">
{{ $subsArray := .JSON.Array "subscriptions" }}
{{ if gt (len $subsArray) 0 }}
{{ $upcoming := index $subsArray 0 }}
{{ $upcomingName := $upcoming.String "name" }}
{{ $upcomingTimeStr := $upcoming.String "next_payment" }}
{{ $upcomingPrice := $upcoming.Float "price" }}
{{ $relativeAttr := $upcomingTimeStr | parseRelativeTime "dateonly" }}
<span>
{{ $upcomingName }}
<span {{ $relativeAttr }}>{{ $upcomingTimeStr }}</span>
{{ if $upcomingPrice }}
{{/* Keep upcoming price with 2 decimals */}}
for {{ $currency }}{{ printf "%.2f" $upcomingPrice }}
{{ end }}
</span>
{{ else }}
<span>No active subscriptions</span>
{{ end }}
</div> {{/* End upcoming payment section */}}
Hey, based on the information you've provided, I don't think that this is a bug. When you see "in x" that means a future time, when it's without the "in" that means a past time.
{{ $relativeAttr := $upcomingTimeStr | parseRelativeTime "dateonly" }}
Since you're only parsing the date without the time, there's no way to show the relative time with sub-day accuracy.
I think it's safe to assume midnight of the local time if a time is not provided since we're doing relative.
That is already the case, parsing a date without time will set the time to midnight. I believe the issue is that the time is by default parsed in UTC and that may be many hours off from your timezone. I'll need to add something like parseTimeInCurrentTimezone that would use the timezone of the server Glance is running on, which should fix this.
I've added the parseLocalTime function in v0.7.13 which should address this issue.