2023-08-11 15:51:20 +01:00
|
|
|
using System.Globalization;
|
2023-08-08 21:03:41 +01:00
|
|
|
using SmartFormat.Core.Extensions;
|
|
|
|
|
2024-05-05 02:26:50 +01:00
|
|
|
namespace OliverBooth.Extensions.SmartFormat;
|
2023-08-08 21:03:41 +01:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Represents a SmartFormat formatter that formats a date.
|
|
|
|
/// </summary>
|
2023-08-13 13:27:44 +01:00
|
|
|
public sealed class DateFormatter : IFormatter
|
2023-08-08 21:03:41 +01:00
|
|
|
{
|
|
|
|
/// <inheritdoc />
|
|
|
|
public bool CanAutoDetect { get; set; } = true;
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public string Name { get; set; } = "date";
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public bool TryEvaluateFormat(IFormattingInfo formattingInfo)
|
|
|
|
{
|
|
|
|
if (formattingInfo.CurrentValue is not string value)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!DateTime.TryParseExact(value, "yyyy-MM-dd",
|
|
|
|
CultureInfo.InvariantCulture,
|
|
|
|
DateTimeStyles.None,
|
|
|
|
out DateTime date))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
2024-02-18 18:01:16 +00:00
|
|
|
formattingInfo.Write(date.ToString(formattingInfo.Format?.ToString(), CultureInfo.InvariantCulture));
|
2023-08-08 21:03:41 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|