-
-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support for dateonly type convertors
- Loading branch information
Showing
5 changed files
with
85 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace AzureCostCli.Infrastructure.TypeConvertors; | ||
|
||
public class DateOnlyTypeConverter : StringTypeConverterBase<DateOnly> | ||
{ | ||
protected override DateOnly Parse(string s, IFormatProvider? provider) => DateOnly.Parse(s, provider); | ||
|
||
protected override string ToIsoString(DateOnly source, IFormatProvider? provider) => source.ToString("O", provider); | ||
} |
57 changes: 57 additions & 0 deletions
57
src/Infrastructure/TypeConvertors/StringTypeConverterBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System.ComponentModel; | ||
using System.Globalization; | ||
|
||
namespace AzureCostCli.Infrastructure.TypeConvertors; | ||
|
||
public abstract class StringTypeConverterBase<T> : TypeConverter | ||
{ | ||
protected abstract T Parse(string s, IFormatProvider? provider); | ||
|
||
protected abstract string ToIsoString(T source, IFormatProvider? provider); | ||
|
||
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) | ||
{ | ||
if (sourceType == typeof(string)) | ||
{ | ||
return true; | ||
} | ||
return base.CanConvertFrom(context, sourceType); | ||
} | ||
|
||
public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value) | ||
{ | ||
if (value is string str) | ||
{ | ||
return Parse(str, GetFormat(culture)); | ||
} | ||
return base.ConvertFrom(context, culture, value); | ||
} | ||
|
||
public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType) | ||
{ | ||
if (destinationType == typeof(string)) | ||
{ | ||
return true; | ||
} | ||
return base.CanConvertTo(context, destinationType); | ||
} | ||
public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType) | ||
{ | ||
if (destinationType == typeof(string) && value is T typedValue) | ||
{ | ||
return ToIsoString(typedValue, GetFormat(culture)); | ||
} | ||
return base.ConvertTo(context, culture, value, destinationType); | ||
} | ||
|
||
private static IFormatProvider? GetFormat(CultureInfo? culture) | ||
{ | ||
DateTimeFormatInfo? formatInfo = null; | ||
if (culture != null) | ||
{ | ||
formatInfo = (DateTimeFormatInfo?)culture.GetFormat(typeof(DateTimeFormatInfo)); | ||
} | ||
|
||
return (IFormatProvider?)formatInfo ?? culture; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters