Skip to content

Commit

Permalink
Fixing default timezone parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienros committed Feb 25, 2021
1 parent 0ec4966 commit 59d6784
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
25 changes: 17 additions & 8 deletions Fluid.Tests/MiscFiltersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,18 +334,13 @@ public void DateNumberIsParsedAsSeconds(long number, string expected)
[Fact]
public void NoTimeZoneIsParsedAsLocal()
{
// This test is issued from a template running in Ruby on a system with -5 TZ
// {{ '1970-01-01 00:00:00' | date: '%c' }}

// This test ensures that a parsed date without TZ uses the one from the settings

var input = StringValue.Create("1970-01-01 00:00:00");
var format = new FilterArguments(new StringValue("%+"));
var context = new TemplateContext { TimeZoneUtcOffset = TimeSpan.FromHours(-5) };
var format = new FilterArguments(new StringValue("%a %b %e %H:%M:%S %Y %z"));
var context = new TemplateContext { TimeZoneUtcOffset = TimeSpan.FromHours(-8) };

var result = MiscFilters.Date(input, format, context);

Assert.Equal("Wed Dec 31 19:00:00 -05:00 1969", result.Result.ToStringValue());
Assert.Equal("Thu Jan 1 00:00:00 1970 -0800", result.Result.ToStringValue());
}

[Fact]
Expand All @@ -362,6 +357,20 @@ public void TimeZoneIsParsed()
Assert.Equal("18000", result.Result.ToStringValue());
}

[Fact]
public void DefaultTimeZoneIsSetWhenNotParsed()
{
// This test ensures that when a TZ is specified it uses it instead of the settings one
var input = StringValue.Create("1970-01-01 00:00:00");

var format = new FilterArguments(new StringValue("%s"));
var context = new TemplateContext { TimeZoneUtcOffset = TimeSpan.FromHours(-5) };

var result = MiscFilters.Date(input, format, context);

Assert.Equal("18000", result.Result.ToStringValue());
}

[Fact]
public void DateNumberIsParsedInLocalTimeZone()
{
Expand Down
2 changes: 1 addition & 1 deletion Fluid/Filters/MiscFilters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ private static bool TryGetDateTimeInput(FluidValue input, TemplateContext contex
{
if (dateTime.Kind == DateTimeKind.Unspecified)
{
result = new DateTimeOffset(dateTime, context.TimeZoneUtcOffset).Add(context.TimeZoneUtcOffset);
result = new DateTimeOffset(dateTime, context.TimeZoneUtcOffset);
}
else
{
Expand Down
10 changes: 8 additions & 2 deletions Fluid/TemplateContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public TemplateContext(TemplateOptions options)
LocalScope.SetValue("blank", StringValue.Empty);
CultureInfo = options.CultureInfo;
TimeZoneUtcOffset = options.TimeZoneUtcOffset;
Now = options.Now;
}

/// <summary>
Expand All @@ -59,12 +60,17 @@ public TemplateContext(object model) : this()
/// <summary>
/// Gets or sets the <see cref="CultureInfo"/> instance used to render locale values like dates and numbers.
/// </summary>
public CultureInfo CultureInfo { get; set; }
public CultureInfo CultureInfo { get; set; } = TemplateOptions.Default.CultureInfo;

/// <summary>
/// Gets or sets the value to returned by the "now" keyword.
/// </summary>
public Func<DateTimeOffset> Now { get; set; } = TemplateOptions.Default.Now;

/// <summary>
/// Gets or sets the local Timezone offset used when parsing or creating dates without specific timezone.
/// </summary>
public TimeSpan TimeZoneUtcOffset { get; set; } = TimeZoneInfo.Local.BaseUtcOffset;
public TimeSpan TimeZoneUtcOffset { get; set; } = TemplateOptions.Default.TimeZoneUtcOffset;

internal void IncrementSteps()
{
Expand Down

0 comments on commit 59d6784

Please sign in to comment.