Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perf: Improve perf of SqlDateTimeToDateTime #912

Merged
merged 6 commits into from
Apr 9, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,30 @@ internal static DateTime SqlDateTimeToDateTime(int daypart, int timepart)
const int SQLTicksPerMinute = SQLTicksPerSecond * 60;
const int SQLTicksPerHour = SQLTicksPerMinute * 60;
const int SQLTicksPerDay = SQLTicksPerHour * 24;
const int MinDay = -53690; // Jan 1 1753
const int MaxDay = 2958463; // Dec 31 9999 is this many days from Jan 1 1900
const int MinTime = 0; // 00:00:0:000PM
const int MaxTime = SQLTicksPerDay - 1; // = 25919999, 11:59:59:997PM

if (daypart < MinDay || daypart > MaxDay || timepart < MinTime || timepart > MaxTime)
//const int MinDay = -53690; // Jan 1 1753
const uint MinDayOffset = 53690; // postive value of MinDay used to pull negative values up to 0 so a single check can be used
const uint MaxDay = 2958463; // Dec 31 9999 is this many days from Jan 1 1900
const uint MaxTime = SQLTicksPerDay - 1; // = 25919999, 11:59:59:997PM
const long BaseDateTicks = 599266080000000000L;//new DateTime(1900, 1, 1).Ticks;
Wraith2 marked this conversation as resolved.
Show resolved Hide resolved

// casting to uint wraps negative values to large positive ones above the valid
// ranges so the lower bound doesn't need to be checked
if ((uint)(daypart + MinDayOffset) > (MaxDay + MinDayOffset) || (uint)timepart > MaxTime)
{
throw new OverflowException(SQLResource.DateTimeOverflowMessage);
ThrowOverflowException();
}

long baseDateTicks = new DateTime(1900, 1, 1).Ticks;
long dayticks = daypart * TimeSpan.TicksPerDay;
long timeticks = ((long)(timepart / SQLTicksPerMillisecond + 0.5)) * TimeSpan.TicksPerMillisecond;
double timePartPerMs = timepart / SQLTicksPerMillisecond;
timePartPerMs += 0.5;
long timeTicks = ((long)timePartPerMs) * TimeSpan.TicksPerMillisecond;
long totalTicks = BaseDateTicks + dayticks + timeTicks;
return new DateTime(totalTicks);
}

return new DateTime(baseDateTicks + dayticks + timeticks);
private static void ThrowOverflowException()
{
throw new OverflowException(SQLResource.DateTimeOverflowMessage);
}
cheenamalhotra marked this conversation as resolved.
Show resolved Hide resolved
#endregion

Expand Down