-
Notifications
You must be signed in to change notification settings - Fork 3
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
DateTime.UtcNow.TimeOfDay always returns zero #48
Comments
@mamen It's good that you found a solution. However, I would still have expected |
@mamen What is your target framework? |
I am on .NET Framework 4.7.2 |
@mamen I ran an experiment. Code follows below. var dateTimeShim = Shim.Replace(() => DateTime.UtcNow)
.With(() => new DateTime(2017, 10, 6, 15, 16, 17, 745, DateTimeKind.Utc));
PoseContext.Isolate(
() =>
{
Console.WriteLine("In the following:");
Console.WriteLine(" - [A] means the actual non-shimmed value");
Console.WriteLine(" - [S] means the shimmed value");
Console.WriteLine(" - [C] means the computed value in cases where that makes sense");
Console.WriteLine();
var shimmedNow = DateTime.UtcNow;
var actualNow = new DateTime(2017, 10, 6, 15, 16, 17, 745, DateTimeKind.Utc);
Console.WriteLine("We start by comparing the shimmed instance of DateTime.UtcNow with an actual instance.");
Console.WriteLine($"Now [A]: {actualNow}");
Console.WriteLine($"Now [S]: {shimmedNow}");
Console.WriteLine();
Console.WriteLine("Their TimeOfDays report the same (remarkably) empty TimeOfDay.");
Console.WriteLine($"TimeOfDay [A]: {actualNow.TimeOfDay}");
Console.WriteLine($"TimeOfDay [S]: {shimmedNow.TimeOfDay}");
Console.WriteLine();
Console.WriteLine("Let's look at the underlying values.");
Console.WriteLine("When constructing the TimeOfDay, the dateData field is used.");
Console.WriteLine("Computations follow below.");
var shimmedDateData = (ulong) typeof(DateTime).GetField("dateData", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(shimmedNow);
Console.WriteLine($"dateData [S]: {shimmedDateData}");
var actualDateData = (ulong) typeof(DateTime).GetField("dateData", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(actualNow);
Console.WriteLine($"dateData [A]: {actualDateData}");
Console.WriteLine();
Console.WriteLine("In the following sections we also use [C] to mean that we have calculated the value ourselves using the data from the shimmed instance.");
Console.WriteLine();
Console.WriteLine("This value is retrieved (and manipulated) by calling the InternalTicks property.");
Console.WriteLine("The values are as follows:");
var actualInternalTicks = typeof(DateTime).GetProperty("InternalTicks", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(actualNow);
Console.WriteLine($"InternalTicks [A]: {actualInternalTicks}");
var shimmedInternalTicks = typeof(DateTime).GetProperty("InternalTicks", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(shimmedNow);
Console.WriteLine($"InternalTicks [S]: {shimmedInternalTicks}");
var internalTicks = (long)shimmedDateData & 4611686018427387903L;
Console.WriteLine($"InternalTicks [C]: {internalTicks}");
Console.WriteLine();
Console.WriteLine($"TimeOfDay [A]: {actualNow.TimeOfDay}");
Console.WriteLine($"TimeOfDay [S]: {shimmedNow.TimeOfDay}");
var timeSpan = new TimeSpan(internalTicks % 864000000000L);
Console.WriteLine($"TimeOfDay [C]: {timeSpan}");
Console.WriteLine();
var actualResult = (long)actualNow.TimeOfDay.TotalMilliseconds / 100;
Console.WriteLine($"Result [A]: {actualResult}");
var shimmedResult = (long)shimmedNow.TimeOfDay.TotalMilliseconds / 100;
Console.WriteLine($"Result [S]: {shimmedResult}");
var calculatedMut = (long)timeSpan.TotalMilliseconds / 100;
Console.WriteLine($"Result [C]: {calculatedMut}");
Console.WriteLine();
Console.WriteLine("From this we can conclude that something goes wrong in the call to TimeOfDay.");
Console.WriteLine("As of now, I don't know what.");
}, dateTimeShim); The output (and the result) is the following:
Something goes wrong when invoking Next steps would be to verify whether this happens simply due to the rewrite process. We can verify this by passing in a shim which will never be applied. |
I am trying to move away from Fakes and stumbled upon this repository. I was able to successfully convert most of my tests which used Fakes with Pose(r). However, I was not able to convert some of the tests which use
DateTime
.In one of the tests, the
Shim
is set up like this:In the method under test,
DateTime
is accessed like this:If I run this test, the returned value for
TimeOfDay
is00:00:00
and thus,TotalMilliseconds
is also0
. However, when using Fakes,TimeOfDay
returns15:16:17.7450000
andTotalMilliseconds
returns54977745
.Am I doing something wrong or is this a (known) bug?
-- Edit --
I fixed it by introducing a second Shim for the
TimeOfDay
-property:The text was updated successfully, but these errors were encountered: