-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #333 from UiPath/feat/add-dateonly-converter
feat: add dateonly converter
- Loading branch information
Showing
4 changed files
with
240 additions
and
0 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
98 changes: 98 additions & 0 deletions
98
src/System.Xaml/System/Xaml/Replacements/DateOnlyConverter2.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,98 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.ComponentModel; | ||
using System.ComponentModel.Design.Serialization; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Globalization; | ||
|
||
namespace System.Xaml.Replacements | ||
{ | ||
/// <summary> | ||
/// Provides a type converter to convert <see cref='System.DateOnly'/> objects to and from various other representations. | ||
/// </summary> | ||
internal class DateOnlyConverter2 : TypeConverter | ||
{ | ||
/// <summary> | ||
/// Gets a value indicating whether this converter can convert an object in the given source type to a <see cref='System.DateOnly'/> | ||
/// object using the specified context. | ||
/// </summary> | ||
/// <inheritdoc /> | ||
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) | ||
{ | ||
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType) | ||
{ | ||
return destinationType == typeof(InstanceDescriptor) || base.CanConvertTo(context, destinationType); | ||
} | ||
|
||
/// <summary> | ||
/// Converts the given value object to a <see cref='System.DateOnly'/> object. | ||
/// </summary> | ||
/// <inheritdoc /> | ||
public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value) | ||
{ | ||
if (value is string text) | ||
{ | ||
text = text.Trim(); | ||
if (text.Length == 0) | ||
{ | ||
return DateOnly.MinValue; | ||
} | ||
|
||
// See if we have a culture info to parse with. If so, then use it. | ||
DateTimeFormatInfo? formatInfo = null; | ||
|
||
if (culture != null) | ||
{ | ||
formatInfo = (DateTimeFormatInfo?)culture.GetFormat(typeof(DateTimeFormatInfo)); | ||
} | ||
|
||
if (formatInfo != null) | ||
{ | ||
return DateOnly.Parse(text, formatInfo); | ||
} | ||
else | ||
{ | ||
return DateOnly.Parse(text, culture); | ||
} | ||
} | ||
|
||
return base.ConvertFrom(context, culture, value); | ||
} | ||
|
||
/// <summary> | ||
/// Converts the given value object from a <see cref='System.DateOnly'/> object using the arguments. | ||
/// </summary> | ||
/// <inheritdoc /> | ||
public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType) | ||
{ | ||
if (destinationType == typeof(string) && value is DateOnly dateOnly) | ||
{ | ||
if (dateOnly == DateOnly.MinValue) | ||
{ | ||
return string.Empty; | ||
} | ||
|
||
culture ??= CultureInfo.CurrentCulture; | ||
|
||
if (culture == CultureInfo.InvariantCulture) | ||
{ | ||
return dateOnly.ToString("yyyy-MM-dd", culture); | ||
} | ||
|
||
return dateOnly.ToString(culture.DateTimeFormat.ShortDatePattern, culture); | ||
} | ||
|
||
if (destinationType == typeof(InstanceDescriptor) && value is DateOnly date) | ||
{ | ||
return new InstanceDescriptor(typeof(DateOnly).GetConstructor(new Type[] { typeof(int), typeof(int), typeof(int) }), new object[] { date.Year, date.Month, date.Day }); | ||
} | ||
|
||
return base.ConvertTo(context, culture, value, destinationType); | ||
} | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
src/System.Xaml/System/Xaml/Replacements/TimeOnlyConverter2.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,100 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.ComponentModel; | ||
using System.ComponentModel.Design.Serialization; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Globalization; | ||
|
||
namespace System.Xaml.Replacements | ||
{ | ||
/// <summary> | ||
/// Provides a type converter to convert <see cref='System.TimeOnly'/> objects to and from various other representations. | ||
/// </summary> | ||
internal class TimeOnlyConverter2 : TypeConverter | ||
{ | ||
/// <summary> | ||
/// Gets a value indicating whether this converter can convert an object in the given source type to a <see cref='System.TimeOnly'/> | ||
/// object using the specified context. | ||
/// </summary> | ||
/// <inheritdoc /> | ||
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) | ||
{ | ||
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType) | ||
{ | ||
return destinationType == typeof(InstanceDescriptor) || base.CanConvertTo(context, destinationType); | ||
} | ||
|
||
/// <summary> | ||
/// Converts the given value object to a <see cref='System.TimeOnly'/> object. | ||
/// </summary> | ||
/// <inheritdoc /> | ||
public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value) | ||
{ | ||
if (value is string text) | ||
{ | ||
text = text.Trim(); | ||
if (text.Length == 0) | ||
{ | ||
return TimeOnly.MinValue; | ||
} | ||
|
||
|
||
// See if we have a culture info to parse with. If so, then use it. | ||
DateTimeFormatInfo? formatInfo = null; | ||
|
||
if (culture != null) | ||
{ | ||
formatInfo = (DateTimeFormatInfo?)culture.GetFormat(typeof(DateTimeFormatInfo)); | ||
} | ||
|
||
if (formatInfo != null) | ||
{ | ||
return TimeOnly.Parse(text, formatInfo); | ||
} | ||
else | ||
{ | ||
return TimeOnly.Parse(text, culture); | ||
} | ||
} | ||
|
||
return base.ConvertFrom(context, culture, value); | ||
} | ||
|
||
/// <summary> | ||
/// Converts the given value object from a <see cref='System.TimeOnly'/> object using the arguments. | ||
/// </summary> | ||
/// <inheritdoc /> | ||
public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType) | ||
{ | ||
if (destinationType == typeof(string) && value is TimeOnly timeOnly) | ||
{ | ||
if (timeOnly == TimeOnly.MinValue) | ||
{ | ||
return string.Empty; | ||
} | ||
|
||
culture ??= CultureInfo.CurrentCulture; | ||
|
||
return timeOnly.ToString(culture.DateTimeFormat.ShortTimePattern, culture); | ||
} | ||
|
||
if (destinationType == typeof(InstanceDescriptor) && value is TimeOnly time) | ||
{ | ||
if (time.Ticks == 0) | ||
{ | ||
return new InstanceDescriptor(typeof(TimeOnly).GetConstructor(new Type[] { typeof(long) }), new object[] { time.Ticks }); | ||
} | ||
|
||
return new InstanceDescriptor(typeof(TimeOnly).GetConstructor(new Type[] { typeof(int), typeof(int), typeof(int), typeof(int), typeof(int) }), | ||
new object[] { time.Hour, time.Minute, time.Second, time.Millisecond}); | ||
} | ||
|
||
return base.ConvertTo(context, culture, value, destinationType); | ||
} | ||
} | ||
} |
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