glance icon indicating copy to clipboard operation
glance copied to clipboard

parseRelativeTime has an "in" for days only, e.g., "in 5d"

Open gehrleib opened this issue 8 months ago • 3 comments

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 */}}

Image

Image

gehrleib avatar Mar 27 '25 03:03 gehrleib

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.

svilenmarkov avatar Mar 28 '25 13:03 svilenmarkov

I think it's safe to assume midnight of the local time if a time is not provided since we're doing relative.

gehrleib avatar Mar 29 '25 20:03 gehrleib

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.

svilenmarkov avatar Mar 31 '25 00:03 svilenmarkov

I've added the parseLocalTime function in v0.7.13 which should address this issue.

svilenmarkov avatar Apr 22 '25 23:04 svilenmarkov