Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Liquid filter to convert to Organization Timezone #68

Closed
apexdodge opened this issue Feb 14, 2023 · 1 comment
Closed

Liquid filter to convert to Organization Timezone #68

apexdodge opened this issue Feb 14, 2023 · 1 comment
Labels
enhancement New feature or request
Milestone

Comments

@apexdodge
Copy link
Contributor

By default, the liquid template outputs UTC format, but we want to give the Template builder the ability to conveniently convert the time to the same time specified in the system configuration settings.

Example:

<span class="ud-blog-date">{{ item.CreationTime | organization_time: '%b %e %Y, %l:%M:%S %P' }}</span>

The following code needs to be added to https://github.com/RaythaHQ/raytha/blob/main/src/Raytha.Web/Services/RenderEngine.cs

    public static ValueTask<FluidValue> LocalDateFilter(FluidValue input, FilterArguments arguments, TemplateContext context)
    {
        var value = TimeZoneConverter(input, context);
        return ReferenceEquals(value, NilValue.Instance) ? value : MiscFilters.Date(value, arguments, context);
    }

    private static FluidValue TimeZoneConverter(FluidValue input, TemplateContext context)
    {
        if (!input.TryGetDateTimeInput(context, out var value))
        {
            return NilValue.Instance;
        }

        var utc = DateTime.SpecifyKind(value.DateTime, DateTimeKind.Utc);

        // Create new offset for UTC
        var localOffset = new DateTimeOffset(utc, TimeSpan.Zero);

        var result = TimeZoneInfo.ConvertTime(localOffset, context.TimeZone);
        return new DateTimeValue(result);
    }

And:

options.Filters.AddFilter("organization_time", LocalDateFilter);

And we should update the default template too so this filter is in use for fresh installations.

@apexdodge apexdodge added the enhancement New feature or request label Feb 14, 2023
@apexdodge apexdodge added this to the v1.0.1 milestone Feb 14, 2023
@apexdodge
Copy link
Contributor Author

Implemented for v1.0.1

apexdodge added a commit that referenced this issue Feb 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant