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
17 changes: 17 additions & 0 deletions Fluid.Tests/MiscFiltersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,23 @@ public async Task DateNumberIsParsedAsSeconds(long number, string expected)
Assert.Equal(expected, result.ToStringValue());
}

[Theory]
[InlineData("0", "00:00:00.000")]
[InlineData("1:2", "01:02:00.000")]
[InlineData("1:2:3.1", "01:02:03.100")]
public async Task DateTimeSpan(string timespan, string expected)
{
// Converting to Unix time should not vary by TimeSone

var input = FluidValue.Create(TimeSpan.Parse(timespan), new TemplateOptions());
var format = new FilterArguments(new StringValue("%H:%M:%S.%L"));
var context = new TemplateContext { TimeZone = Eastern };

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

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

[Fact]
public async Task NoTimeZoneIsParsedAsLocal()
{
Expand Down
6 changes: 3 additions & 3 deletions Fluid/MemberAccessStrategyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ internal static IMemberAccessor GetNamedAccessor(Type type, string name, MemberN
/// </summary>
/// <typeparam name="T">The type to register.</typeparam>
/// <param name="strategy">The <see cref="MemberAccessStrategy"/>.</param>
public static void Register<T>(this MemberAccessStrategy strategy)
public static void Register<T>(this MemberAccessStrategy strategy) where T : class
{
Register(strategy, typeof(T));
}
Expand All @@ -101,7 +101,7 @@ public static void Register(this MemberAccessStrategy strategy, Type type)
/// <typeparam name="T">The type to register.</typeparam>
/// <param name="strategy">The <see cref="MemberAccessStrategy"/>.</param>
/// <param name="names">The names of the properties in the type to register.</param>
public static void Register<T>(this MemberAccessStrategy strategy, params string[] names)
public static void Register<T>(this MemberAccessStrategy strategy, params string[] names) where T : class
{
strategy.Register(typeof(T), names);
}
Expand All @@ -112,7 +112,7 @@ public static void Register<T>(this MemberAccessStrategy strategy, params string
/// <typeparam name="T">The type to register.</typeparam>
/// <param name="strategy">The <see cref="MemberAccessStrategy"/>.</param>
/// <param name="names">The property's expressions in the type to register.</param>
public static void Register<T>(this MemberAccessStrategy strategy, params Expression<Func<T, object>>[] names)
public static void Register<T>(this MemberAccessStrategy strategy, params Expression<Func<T, object>>[] names) where T : class
{
strategy.Register<T>(names.Select(ExpressionHelper.GetPropertyName).ToArray());
}
Expand Down
3 changes: 3 additions & 0 deletions Fluid/Values/FluidValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ public static FluidValue Create(object value, TemplateOptions options)
case DateTimeOffset dateTimeOffset:
return new DateTimeValue(dateTimeOffset);

case TimeSpan timeSpan:
return new DateTimeValue(new DateTime(timeSpan.Ticks));

case IFormattable formattable:
return new StringValue(formattable.ToString(null, options.CultureInfo));

Expand Down