Skip to content

Commit

Permalink
Added timeSpan overloads to SQL Server DateDiff functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Muppets committed May 29, 2019
1 parent 4f43be9 commit 89576fc
Show file tree
Hide file tree
Showing 3 changed files with 432 additions and 0 deletions.
210 changes: 210 additions & 0 deletions src/EFCore.SqlServer/Extensions/SqlServerDbFunctionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,41 @@ public static int DateDiffHour(
? (int?)DateDiffHour(_, startDate.Value, endDate.Value)
: null;

/// <summary>
/// Counts the number of hour boundaries crossed between the startTimeSpan and endTimeSpan.
/// Corresponds to SQL Server's DATEDIFF(HOUR,startDate,endDate).
/// </summary>
/// <param name="_">The DbFunctions instance.</param>
/// <param name="startTimeSpan">Starting timespan for the calculation.</param>
/// <param name="endTimeSpan">Ending timespan for the calculation.</param>
/// <returns>Number of hour boundaries crossed between the timespans.</returns>
public static int DateDiffHour(
[CanBeNull] this DbFunctions _,
TimeSpan startTimeSpan,
TimeSpan endTimeSpan)
{
checked
{
return endTimeSpan.Hours - startTimeSpan.Hours;
}
}

/// <summary>
/// Counts the number of hour boundaries crossed between the startTimeSpan and endTimeSpan.
/// Corresponds to SQL Server's DATEDIFF(HOUR,startDate,endDate).
/// </summary>
/// <param name="_">The DbFunctions instance.</param>
/// <param name="startTimeSpan">Starting timespan for the calculation.</param>
/// <param name="endTimeSpan">Ending timespan for the calculation.</param>
/// <returns>Number of hour boundaries crossed between the timespans.</returns>
public static int? DateDiffHour(
[CanBeNull] this DbFunctions _,
TimeSpan? startTimeSpan,
TimeSpan? endTimeSpan)
=> (startTimeSpan.HasValue && endTimeSpan.HasValue)
? (int?)DateDiffHour(_, startTimeSpan.Value, endTimeSpan.Value)
: null;

/// <summary>
/// Counts the number of minute boundaries crossed between the startDate and endDate.
/// Corresponds to SQL Server's DATEDIFF(MINUTE,startDate,endDate).
Expand Down Expand Up @@ -410,6 +445,41 @@ public static int DateDiffMinute(
? (int?)DateDiffMinute(_, startDate.Value, endDate.Value)
: null;

/// <summary>
/// Counts the number of minute boundaries crossed between the startTimeSpan and endTimeSpan.
/// Corresponds to SQL Server's DATEDIFF(MINUTE,startDate,endDate).
/// </summary>
/// <param name="_">The DbFunctions instance.</param>
/// <param name="startTimeSpan">Starting timespan for the calculation.</param>
/// <param name="endTimeSpan">Ending timespan for the calculation.</param>
/// <returns>Number of minute boundaries crossed between the timepsans.</returns>
public static int DateDiffMinute(
[CanBeNull] this DbFunctions _,
TimeSpan startTimeSpan,
TimeSpan endTimeSpan)
{
checked
{
return DateDiffHour(_, startTimeSpan, endTimeSpan) * 60 + endTimeSpan.Minutes - startTimeSpan.Minutes;
}
}

/// <summary>
/// Counts the number of minute boundaries crossed between the startTimeSpan and endTimeSpan.
/// Corresponds to SQL Server's DATEDIFF(MINUTE,startDate,endDate).
/// </summary>
/// <param name="_">The DbFunctions instance.</param>
/// <param name="startTimeSpan">Starting timespan for the calculation.</param>
/// <param name="endTimeSpan">Ending timespan for the calculation.</param>
/// <returns>Number of minute boundaries crossed between the timespans.</returns>
public static int? DateDiffMinute(
[CanBeNull] this DbFunctions _,
TimeSpan? startTimeSpan,
TimeSpan? endTimeSpan)
=> (startTimeSpan.HasValue && endTimeSpan.HasValue)
? (int?)DateDiffMinute(_, startTimeSpan.Value, endTimeSpan.Value)
: null;

/// <summary>
/// Counts the number of second boundaries crossed between the startDate and endDate.
/// Corresponds to SQL Server's DATEDIFF(SECOND,startDate,endDate).
Expand Down Expand Up @@ -475,6 +545,41 @@ public static int DateDiffSecond(
? (int?)DateDiffSecond(_, startDate.Value, endDate.Value)
: null;

/// <summary>
/// Counts the number of second boundaries crossed between the startTimeSpan and endTimeSpan.
/// Corresponds to SQL Server's DATEDIFF(SECOND,startDate,endDate).
/// </summary>
/// <param name="_">The DbFunctions instance.</param>
/// <param name="startTimeSpan">Starting timespan for the calculation.</param>
/// <param name="endTimeSpan">Ending timespan for the calculation.</param>
/// <returns>Number of second boundaries crossed between the timespans.</returns>
public static int DateDiffSecond(
[CanBeNull] this DbFunctions _,
TimeSpan startTimeSpan,
TimeSpan endTimeSpan)
{
checked
{
return DateDiffMinute(_, startTimeSpan, endTimeSpan) * 60 + endTimeSpan.Seconds - startTimeSpan.Seconds;
}
}

/// <summary>
/// Counts the number of second boundaries crossed between the startTimeSpan and endTimeSpan.
/// Corresponds to SQL Server's DATEDIFF(SECOND,startDate,endDate).
/// </summary>
/// <param name="_">The DbFunctions instance.</param>
/// <param name="startTimeSpan">Starting timespan for the calculation.</param>
/// <param name="endTimeSpan">Ending timespan for the calculation.</param>
/// <returns>Number of second boundaries crossed between the timespans.</returns>
public static int? DateDiffSecond(
[CanBeNull] this DbFunctions _,
TimeSpan? startTimeSpan,
TimeSpan? endTimeSpan)
=> (startTimeSpan.HasValue && endTimeSpan.HasValue)
? (int?)DateDiffSecond(_, startTimeSpan.Value, endTimeSpan.Value)
: null;

/// <summary>
/// Counts the number of millisecond boundaries crossed between the startDate and endDate.
/// Corresponds to SQL Server's DATEDIFF(MILLISECOND,startDate,endDate).
Expand Down Expand Up @@ -540,6 +645,41 @@ public static int DateDiffMillisecond(
? (int?)DateDiffMillisecond(_, startDate.Value, endDate.Value)
: null;

/// <summary>
/// Counts the number of millisecond boundaries crossed between the startTimeSpan and endTimeSpan.
/// Corresponds to SQL Server's DATEDIFF(MILLISECOND,startDate,endDate).
/// </summary>
/// <param name="_">The DbFunctions instance.</param>
/// <param name="startTimeSpan">Starting timespan for the calculation.</param>
/// <param name="endTimeSpan">Ending timespan for the calculation.</param>
/// <returns>Number of millisecond boundaries crossed between the timespans.</returns>
public static int DateDiffMillisecond(
[CanBeNull] this DbFunctions _,
TimeSpan startTimeSpan,
TimeSpan endTimeSpan)
{
checked
{
return DateDiffSecond(_, startTimeSpan, endTimeSpan) * 1000 + endTimeSpan.Milliseconds - startTimeSpan.Milliseconds;
}
}

/// <summary>
/// Counts the number of millisecond boundaries crossed between the startTimeSpan and endTimeSpan.
/// Corresponds to SQL Server's DATEDIFF(MILLISECOND,startDate,endDate).
/// </summary>
/// <param name="_">The DbFunctions instance.</param>
/// <param name="startTimeSpan">Starting timespan for the calculation.</param>
/// <param name="endTimeSpan">Ending timespan for the calculation.</param>
/// <returns>Number of millisecond boundaries crossed between the timespans.</returns>
public static int? DateDiffMillisecond(
[CanBeNull] this DbFunctions _,
TimeSpan? startTimeSpan,
TimeSpan? endTimeSpan)
=> (startTimeSpan.HasValue && endTimeSpan.HasValue)
? (int?)DateDiffMillisecond(_, startTimeSpan.Value, endTimeSpan.Value)
: null;

/// <summary>
/// Counts the number of microsecond boundaries crossed between the startDate and endDate.
/// Corresponds to SQL Server's DATEDIFF(MICROSECOND,startDate,endDate).
Expand Down Expand Up @@ -605,6 +745,41 @@ public static int DateDiffMicrosecond(
? (int?)DateDiffMicrosecond(_, startDate.Value, endDate.Value)
: null;

/// <summary>
/// Counts the number of microsecond boundaries crossed between the startTimeSpan and endTimeSpan.
/// Corresponds to SQL Server's DATEDIFF(MICROSECOND,startDate,endDate).
/// </summary>
/// <param name="_">The DbFunctions instance.</param>
/// <param name="startTimeSpan">Starting timespan for the calculation.</param>
/// <param name="endTimeSpan">Ending timespan for the calculation.</param>
/// <returns>Number of microsecond boundaries crossed between the timespans.</returns>
public static int DateDiffMicrosecond(
[CanBeNull] this DbFunctions _,
TimeSpan startTimeSpan,
TimeSpan endTimeSpan)
{
checked
{
return (int)((endTimeSpan.Ticks - startTimeSpan.Ticks) / 10);
}
}

/// <summary>
/// Counts the number of microsecond boundaries crossed between the startTimeSpan and endTimeSpan.
/// Corresponds to SQL Server's DATEDIFF(MICROSECOND,startDate,endDate).
/// </summary>
/// <param name="_">The DbFunctions instance.</param>
/// <param name="startTimeSpan">Starting timespan for the calculation.</param>
/// <param name="endTimeSpan">Ending timespan for the calculation.</param>
/// <returns>Number of microsecond boundaries crossed between the timespans.</returns>
public static int? DateDiffMicrosecond(
[CanBeNull] this DbFunctions _,
TimeSpan? startTimeSpan,
TimeSpan? endTimeSpan)
=> (startTimeSpan.HasValue && endTimeSpan.HasValue)
? (int?)DateDiffMicrosecond(_, startTimeSpan.Value, endTimeSpan.Value)
: null;

/// <summary>
/// Counts the number of nanosecond boundaries crossed between the startDate and endDate.
/// Corresponds to SQL Server's DATEDIFF(NANOSECOND,startDate,endDate).
Expand Down Expand Up @@ -669,5 +844,40 @@ public static int DateDiffNanosecond(
=> (startDate.HasValue && endDate.HasValue)
? (int?)DateDiffNanosecond(_, startDate.Value, endDate.Value)
: null;

/// <summary>
/// Counts the number of nanosecond boundaries crossed between the startTimeSpan and endTimeSpan.
/// Corresponds to SQL Server's DATEDIFF(NANOSECOND,startDate,endDate).
/// </summary>
/// <param name="_">The DbFunctions instance.</param>
/// <param name="startTimeSpan">Starting timespan for the calculation.</param>
/// <param name="endTimeSpan">Ending timespan for the calculation.</param>
/// <returns>Number of nanosecond boundaries crossed between the dates.</returns>
public static int DateDiffNanosecond(
[CanBeNull] this DbFunctions _,
TimeSpan startTimeSpan,
TimeSpan endTimeSpan)
{
checked
{
return (int)((endTimeSpan.Ticks - startTimeSpan.Ticks) * 100);
}
}

/// <summary>
/// Counts the number of nanosecond boundaries crossed between the startTimeSpan and endTimeSpan.
/// Corresponds to SQL Server's DATEDIFF(NANOSECOND,startDate,endDate).
/// </summary>
/// <param name="_">The DbFunctions instance.</param>
/// <param name="startTimeSpan">Starting timespan for the calculation.</param>
/// <param name="endTimeSpan">Ending timespan for the calculation.</param>
/// <returns>Number of nanosecond boundaries crossed between the dates.</returns>
public static int? DateDiffNanosecond(
[CanBeNull] this DbFunctions _,
TimeSpan? startTimeSpan,
TimeSpan? endTimeSpan)
=> (startTimeSpan.HasValue && endTimeSpan.HasValue)
? (int?)DateDiffNanosecond(_, startTimeSpan.Value, endTimeSpan.Value)
: null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ private readonly Dictionary<MethodInfo, string> _methodInfoDateDiffMapping
new[] { typeof(DbFunctions), typeof(DateTimeOffset?), typeof(DateTimeOffset?) }),
"HOUR"
},
{
typeof(SqlServerDbFunctionsExtensions).GetRuntimeMethod(
nameof(SqlServerDbFunctionsExtensions.DateDiffHour),
new[] { typeof(DbFunctions), typeof(TimeSpan), typeof(TimeSpan) }),
"HOUR"
},
{
typeof(SqlServerDbFunctionsExtensions).GetRuntimeMethod(
nameof(SqlServerDbFunctionsExtensions.DateDiffHour),
new[] { typeof(DbFunctions), typeof(TimeSpan?), typeof(TimeSpan?) }),
"HOUR"
},
{
typeof(SqlServerDbFunctionsExtensions).GetRuntimeMethod(
nameof(SqlServerDbFunctionsExtensions.DateDiffMinute),
Expand All @@ -134,6 +146,18 @@ private readonly Dictionary<MethodInfo, string> _methodInfoDateDiffMapping
new[] { typeof(DbFunctions), typeof(DateTimeOffset?), typeof(DateTimeOffset?) }),
"MINUTE"
},
{
typeof(SqlServerDbFunctionsExtensions).GetRuntimeMethod(
nameof(SqlServerDbFunctionsExtensions.DateDiffMinute),
new[] { typeof(DbFunctions), typeof(TimeSpan), typeof(TimeSpan) }),
"MINUTE"
},
{
typeof(SqlServerDbFunctionsExtensions).GetRuntimeMethod(
nameof(SqlServerDbFunctionsExtensions.DateDiffMinute),
new[] { typeof(DbFunctions), typeof(TimeSpan?), typeof(TimeSpan?) }),
"MINUTE"
},
{
typeof(SqlServerDbFunctionsExtensions).GetRuntimeMethod(
nameof(SqlServerDbFunctionsExtensions.DateDiffSecond),
Expand All @@ -158,6 +182,18 @@ private readonly Dictionary<MethodInfo, string> _methodInfoDateDiffMapping
new[] { typeof(DbFunctions), typeof(DateTimeOffset?), typeof(DateTimeOffset?) }),
"SECOND"
},
{
typeof(SqlServerDbFunctionsExtensions).GetRuntimeMethod(
nameof(SqlServerDbFunctionsExtensions.DateDiffSecond),
new[] { typeof(DbFunctions), typeof(TimeSpan), typeof(TimeSpan) }),
"SECOND"
},
{
typeof(SqlServerDbFunctionsExtensions).GetRuntimeMethod(
nameof(SqlServerDbFunctionsExtensions.DateDiffSecond),
new[] { typeof(DbFunctions), typeof(TimeSpan?), typeof(TimeSpan?) }),
"SECOND"
},
{
typeof(SqlServerDbFunctionsExtensions).GetRuntimeMethod(
nameof(SqlServerDbFunctionsExtensions.DateDiffMillisecond),
Expand All @@ -182,6 +218,18 @@ private readonly Dictionary<MethodInfo, string> _methodInfoDateDiffMapping
new[] { typeof(DbFunctions), typeof(DateTimeOffset?), typeof(DateTimeOffset?) }),
"MILLISECOND"
},
{
typeof(SqlServerDbFunctionsExtensions).GetRuntimeMethod(
nameof(SqlServerDbFunctionsExtensions.DateDiffMillisecond),
new[] { typeof(DbFunctions), typeof(TimeSpan), typeof(TimeSpan) }),
"MILLISECOND"
},
{
typeof(SqlServerDbFunctionsExtensions).GetRuntimeMethod(
nameof(SqlServerDbFunctionsExtensions.DateDiffMillisecond),
new[] { typeof(DbFunctions), typeof(TimeSpan?), typeof(TimeSpan?) }),
"MILLISECOND"
},
{
typeof(SqlServerDbFunctionsExtensions).GetRuntimeMethod(
nameof(SqlServerDbFunctionsExtensions.DateDiffMicrosecond),
Expand All @@ -206,6 +254,18 @@ private readonly Dictionary<MethodInfo, string> _methodInfoDateDiffMapping
new[] { typeof(DbFunctions), typeof(DateTimeOffset?), typeof(DateTimeOffset?) }),
"MICROSECOND"
},
{
typeof(SqlServerDbFunctionsExtensions).GetRuntimeMethod(
nameof(SqlServerDbFunctionsExtensions.DateDiffMicrosecond),
new[] { typeof(DbFunctions), typeof(TimeSpan), typeof(TimeSpan) }),
"MICROSECOND"
},
{
typeof(SqlServerDbFunctionsExtensions).GetRuntimeMethod(
nameof(SqlServerDbFunctionsExtensions.DateDiffMicrosecond),
new[] { typeof(DbFunctions), typeof(TimeSpan?), typeof(TimeSpan?) }),
"MICROSECOND"
},
{
typeof(SqlServerDbFunctionsExtensions).GetRuntimeMethod(
nameof(SqlServerDbFunctionsExtensions.DateDiffNanosecond),
Expand All @@ -229,6 +289,18 @@ private readonly Dictionary<MethodInfo, string> _methodInfoDateDiffMapping
nameof(SqlServerDbFunctionsExtensions.DateDiffNanosecond),
new[] { typeof(DbFunctions), typeof(DateTimeOffset?), typeof(DateTimeOffset?) }),
"NANOSECOND"
},
{
typeof(SqlServerDbFunctionsExtensions).GetRuntimeMethod(
nameof(SqlServerDbFunctionsExtensions.DateDiffNanosecond),
new[] { typeof(DbFunctions), typeof(TimeSpan), typeof(TimeSpan) }),
"NANOSECOND"
},
{
typeof(SqlServerDbFunctionsExtensions).GetRuntimeMethod(
nameof(SqlServerDbFunctionsExtensions.DateDiffNanosecond),
new[] { typeof(DbFunctions), typeof(TimeSpan?), typeof(TimeSpan?) }),
"NANOSECOND"
}
};
private readonly ISqlExpressionFactory _sqlExpressionFactory;
Expand Down
Loading

0 comments on commit 89576fc

Please sign in to comment.