Skip to content

Commit

Permalink
returned DateOnlyTypeHandler and TimeOnlyTypeHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
govorovvs committed Apr 5, 2024
1 parent a064e5f commit fd919d4
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>0.13.1</Version>
<Version>0.13.2</Version>
<Authors>Byndyusoft</Authors>
<PackageTags>Byndyusoft;Data;Relational</PackageTags>
<RepositoryUrl>https://github.com/Byndyusoft/Byndyusoft.Data.Relational</RepositoryUrl>
Expand Down
29 changes: 29 additions & 0 deletions src/Byndyusoft.Data.Relational/TypeHandlers/DateOnlyTypeHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#if NET6_0_OR_GREATER

using System;
using System.Data;
using Dapper;

namespace Byndyusoft.Data.Relational.TypeHandlers
{
public class DateOnlyTypeHandler : SqlMapper.TypeHandler<DateOnly>
{
public override DateOnly Parse(object value)
{
return value switch
{
DateOnly dateOnly => dateOnly,
DateTime dateTime => DateOnly.FromDateTime(dateTime),
_ => default
};
}

public override void SetValue(IDbDataParameter parameter, DateOnly value)
{
parameter.DbType = DbType.Date;
parameter.Value = value.ToDateTime(TimeOnly.MinValue);
}
}
}

#endif
29 changes: 29 additions & 0 deletions src/Byndyusoft.Data.Relational/TypeHandlers/TimeOnlyTypeHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#if NET6_0_OR_GREATER

using System;
using System.Data;
using Dapper;

namespace Byndyusoft.Data.Relational.TypeHandlers
{
public class TimeOnlyTypeHandler : SqlMapper.TypeHandler<TimeOnly>
{
public override TimeOnly Parse(object value)
{
return value switch
{
TimeOnly time => time,
DateTime date => TimeOnly.FromDateTime(date),
TimeSpan span => TimeOnly.FromTimeSpan(span),
_ => default
};
}

public override void SetValue(IDbDataParameter parameter, TimeOnly value)
{
parameter.DbType = DbType.Time;
parameter.Value = value;
}
}
}
#endif

0 comments on commit fd919d4

Please sign in to comment.