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

Implement missing strftime formats #543

Merged
merged 2 commits into from
Dec 20, 2022
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
43 changes: 34 additions & 9 deletions Fluid.Tests/MiscFiltersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ public async Task EscapeOnce()

[Theory]
[InlineData("%a", "Tue")]
[InlineData("%a", "Sun", "2022-06-26 00:00:00 -0500")]
[InlineData("%^a", "TUE")]
[InlineData("%A", "Tuesday")]
[InlineData("%^A", "TUESDAY")]
Expand All @@ -213,12 +214,17 @@ public async Task EscapeOnce()
[InlineData("%c", "Tuesday, August 1, 2017 5:04:36 PM")]
[InlineData("%^c", "TUESDAY, AUGUST 1, 2017 5:04:36 PM")]
[InlineData("%C", "20")]
[InlineData("%C", "02", "0217-01-01")]
[InlineData("%d", "01")]
[InlineData("%_d", " 1")]
[InlineData("%-d", "1")]
[InlineData("%D", "08/01/17")]
[InlineData("%e", " 1")]
[InlineData("%F", "2017-08-01")]
[InlineData("%G", "2022", "2023-01-01 12:00:00")]
[InlineData("%G", "2024", "2024-01-01 12:00:00")]
[InlineData("%g", "22", "2023-01-01 12:00:00")]
[InlineData("%g", "24", "2024-01-01 12:00:00")]
[InlineData("%H", "17")]
[InlineData("%I", "05")]
[InlineData("%j", "213")]
Expand All @@ -229,21 +235,38 @@ public async Task EscapeOnce()
[InlineData("%_m", " 8")]
[InlineData("%-m", "8")]
[InlineData("%M", "04")]
[InlineData("%n", "\n")]
[InlineData("%N", "123456800")] // nanoseconds are parsed to the 7th digit
[InlineData("%3N", "123")]
[InlineData("%1N", "1")]
[InlineData("%p", "PM")]
[InlineData("%P", "pm")]
[InlineData("%r", "05:04:36 PM")]
[InlineData("%R", "17:04")]
[InlineData("%s", "1501578276")]
[InlineData("%20s", "00000000001501578276")]
[InlineData("%_20s", " 1501578276")]
[InlineData("%S", "36")]
[InlineData("%t", "\t")]
[InlineData("%T", "17:04:36")]
[InlineData("%u", "2")]
[InlineData("%U", "31")]
[InlineData("%u", "7", "2022-06-26 00:00:00")]
[InlineData("%U", "52", "2016-12-31T12:00:00")] // Saturday 12/31
[InlineData("%U", "01", "2017-01-01T12:00:00")] // Sunday 01/01 - week begins on a Sunday (for %U)
[InlineData("%U", "01", "2017-01-02T12:00:00")] // Monday 01/02 - week begins on a Sunday (for %U)
[InlineData("%U", "26", "2022-06-26T00:00:00")]
[InlineData("%v", " 1-Aug-2017")]
[InlineData("%^v", " 1-AUG-2017")]
[InlineData("%V", "31")]
[InlineData("%W", "32")]
[InlineData("%W", "52", "2016-12-31T12:00:00")] // Saturday 12/31
[InlineData("%W", "00", "2017-01-01T12:00:00")] // Sunday 01/01 - still not first week of the year
[InlineData("%W", "01", "2017-01-02T12:00:00")] // Monday 01/02 - week begins on a Monday (for %W)
[InlineData("%W", "25", "2022-06-26T00:00:00")]
[InlineData("%y", "17")]
[InlineData("%Y", "2017")]
[InlineData("%Y", "0217", "0217-01-01")]
[InlineData("%y", "17", "0217-01-01")]
[InlineData("%y", "07", "0207-01-01")]
[InlineData("%z", "+0800")]
[InlineData("%Z", "+08:00")]
[InlineData("%:z", "+08:00")]
Expand All @@ -252,24 +275,26 @@ public async Task EscapeOnce()
[InlineData("It is %r", "It is 05:04:36 PM")]
[InlineData("Chained %z%:z%a%a%^a", "Chained +0800+08:00TueTueTUE")]
[InlineData("%Y-%m-%dT%H:%M:%S.%L", "2017-08-01T17:04:36.123")]
public async Task Date(string format, string expected)
public async Task Date(string format, string expected, string dateTime = "2017-08-01T17:04:36.123456789+08:00")
{
var input = new DateTimeValue(new DateTimeOffset(
new DateTime(2017, 8, 1, 17, 4, 36, 123), TimeSpan.FromHours(8)));

var arguments = new FilterArguments(new StringValue(format));
var options = new TemplateOptions() { CultureInfo = new CultureInfo("en-US", useUserOverride: false), TimeZone = TimeZoneInfo.Utc };
var context = new TemplateContext(options);

new StringValue(dateTime).TryGetDateTimeInput(new TemplateContext(), out var customDateTime);
var input = new DateTimeValue(customDateTime);

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

Assert.Equal(expected, result.ToStringValue());
}

[Theory]
[InlineData("2020-05-18T12:00:00+01:00", "%l:%M%P", "12:00pm")]
[InlineData("2020-05-18T08:00:00+01:00", "%l:%M%P", "8:00am")]
[InlineData("2020-05-18T20:00:00+01:00", "%l:%M%P", "8:00pm")]
[InlineData("2020-05-18T08:00:00+01:00", "%l:%M%P", " 8:00am")]
[InlineData("2020-05-18T20:00:00+01:00", "%l:%M%P", " 8:00pm")]
[InlineData("2020-05-18T20:00:00+01:00", "%-l:%M%P", "8:00pm")]
[InlineData("2020-05-18T20:00:00+01:00", "%I:%M%P", "08:00pm")]
[InlineData("2020-05-18T23:59:00+01:00", "%l:%M%P", "11:59pm")]
[InlineData("2020-05-18T00:00:00+01:00", "%l:%M%P", "12:00am")]
[InlineData("2020-05-18T11:59:00+01:00", "%l:%M%P", "11:59am")]
Expand All @@ -283,7 +308,7 @@ public async Task Time12hFormatFormDateTimeOffset(string dateTimeOffset, string

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

Assert.Equal(expected, result.ToStringValue().Trim());
Assert.Equal(expected, result.ToStringValue());
}

[Theory]
Expand Down
Loading