Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

namespace OrchardCore.Liquid.Filters
{
public class TimeZoneFilter : ILiquidFilter
public class LocalTimeZoneFilter : ILiquidFilter
{
private readonly ILocalClock _localClock;

public TimeZoneFilter(ILocalClock localClock)
public LocalTimeZoneFilter(ILocalClock localClock)
{
_localClock = localClock;
}
Expand Down Expand Up @@ -43,11 +43,9 @@ public async ValueTask<FluidValue> ProcessAsync(FluidValue input, FilterArgument
case DateTime dateTime:
value = dateTime;
break;

case DateTimeOffset dateTimeOffset:
value = dateTimeOffset;
break;

default:
return NilValue.Instance;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Globalization;
using System.Threading.Tasks;
using Fluid;
using Fluid.Values;
using OrchardCore.Modules;

namespace OrchardCore.Liquid.Filters
{
public class UtcTimeZoneFilter : ILiquidFilter
{
private readonly ILocalClock _localClock;

public UtcTimeZoneFilter(ILocalClock localClock)
{
_localClock = localClock;
}

public async ValueTask<FluidValue> ProcessAsync(FluidValue input, FilterArguments arguments, LiquidTemplateContext ctx)
{
DateTimeOffset value;

if (input.Type == FluidValues.String)
{
var stringValue = input.ToStringValue();

if (stringValue == "now" || stringValue == "today")
{
value = await _localClock.LocalNowAsync;
}
else
{
if (!DateTimeOffset.TryParse(stringValue, ctx.Options.CultureInfo, DateTimeStyles.AssumeUniversal, out value))
{
return NilValue.Instance;
}
}
}
else
{
switch (input.ToObjectValue())
{
case DateTime dateTime:
value = dateTime;
break;
case DateTimeOffset dateTimeOffset:
value = dateTimeOffset;
break;
default:
return NilValue.Instance;
}
}

return new ObjectValue(await _localClock.ConvertToUtcAsync(value.DateTime));
}
}
}
3 changes: 2 additions & 1 deletion src/OrchardCore.Modules/OrchardCore.Liquid/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ public override void ConfigureServices(IServiceCollection services)
options.Filters.AddFilter("json", JsonFilter.Json);
options.Filters.AddFilter("jsonparse", JsonParseFilter.JsonParse);
})
.AddLiquidFilter<TimeZoneFilter>("local")
.AddLiquidFilter<LocalTimeZoneFilter>("local")
.AddLiquidFilter<UtcTimeZoneFilter>("utc")
.AddLiquidFilter<SlugifyFilter>("slugify")
.AddLiquidFilter<LiquidFilter>("liquid")
.AddLiquidFilter<ContentUrlFilter>("href")
Expand Down
16 changes: 16 additions & 0 deletions src/docs/reference/modules/Liquid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,22 @@ Output
Wednesday, 02 August 2017 11:54:48
```

### `utc`

Converts a local date and time to the UTC date and time based on the site settings.

Input

```liquid
{{ "now" | utc | date: "%c" }}
```

Output

```text
Wednesday, 02 August 2017 11:54:48
```

### `t`

Localizes a string using the current culture.
Expand Down