Skip to content
Draft
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
17 changes: 17 additions & 0 deletions source/Octostache.Tests/FiltersFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,23 @@ public void NowDateCanBeFormatted()
result.Should().Be(DateTime.Now.Year.ToString());
}

[Fact]
public void NowDateAcceptsTimeZoneId()
{
var result = Evaluate("#{ | NowDate \\\"Eastern Standard Time\\\"}", new Dictionary<string, string>());
DateTime.Parse(result).Should().BeCloseTo(TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.Now, "Eastern Standard Time"), 60000);
}

[Fact]
public void NowDateAcceptsFormatAndTimeZoneId()
{
var result = Evaluate("#{ | NowDate \\\"Eastern Standard Time\\\" \\\"MM/dd/yy H:mm:ss zzz\\\"}", new Dictionary<string, string>());
DateTime.Parse(result).Should().BeCloseTo(TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.Now, "Eastern Standard Time"), 60000);

var result1 = Evaluate("#{ | NowDate \\\"MM/dd/yy H:mm:ss zzz\\\" \\\"Eastern Standard Time\\\"}", new Dictionary<string, string>());
DateTime.Parse(result1).Should().BeCloseTo(TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.Now, "Eastern Standard Time"), 60000);
}

[Fact]
public void NullJsonPropertyTreatedAsEmptyString()
{
Expand Down
20 changes: 18 additions & 2 deletions source/Octostache/Templates/Functions/DateFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,26 @@ static class DateFunction
{
public static string? NowDate(string? argument, string[] options)
{
if (argument != null || options.Length > 1)
if (argument != null || options.Length > 2)
return null;

return DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Unspecified).ToString(options.Any() ? options[0] : "O");
string? formatString = null;
TimeZoneInfo? tz = null;

foreach (var option in options)
{
try
{
tz = TimeZoneInfo.FindSystemTimeZoneById(option);
}
catch (TimeZoneNotFoundException)
{
formatString = option;
}
}

var dt = (tz == null) ? DateTime.Now : TimeZoneInfo.ConvertTime(DateTime.Now, tz);
return dt.ToString(formatString ?? "O");
}

public static string? NowDateUtc(string? argument, string[] options)
Expand Down