You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.4 KiB
55 lines
1.4 KiB
using Connected.Extensions;
|
|
using System.Globalization;
|
|
|
|
namespace Connected
|
|
{
|
|
public static class Converters
|
|
{
|
|
public static CultureInfo DefaultCulture = CultureInfo.CurrentUICulture;
|
|
|
|
#region --> Date converters
|
|
public static LambdaConverter<DateTime, string> IsoDate
|
|
=> new(SetIsoDate, GetIsoDate);
|
|
|
|
private static DateTime GetIsoDate(string value)
|
|
{
|
|
if (DateTime.TryParse(value, out var dateTime))
|
|
return dateTime;
|
|
return DateTime.MinValue;
|
|
}
|
|
|
|
private static string SetIsoDate(DateTime value)
|
|
{
|
|
return value.ToIsoDateString();
|
|
}
|
|
|
|
public static LambdaConverter<DateTime?, string?> NullableIsoDate
|
|
=> new(SetNullableIsoDate, GetNullableIsoDate);
|
|
|
|
private static DateTime? GetNullableIsoDate(string? value)
|
|
{
|
|
if (DateTime.TryParse(value, out var dateTime))
|
|
return dateTime;
|
|
return null;
|
|
}
|
|
|
|
private static string SetNullableIsoDate(DateTime? value)
|
|
{
|
|
return value.ToIsoDateString();
|
|
}
|
|
|
|
public static DateConverter DateFormat(string format)
|
|
{
|
|
format ??= "yyyy-MM-dd";
|
|
return new DateConverter(format);
|
|
}
|
|
|
|
public static DateConverter DateFormat(string format, CultureInfo culture)
|
|
{
|
|
return new DateConverter(format) { Culture = culture };
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|