小编Fab*_*ian的帖子

将UTC DateTime转换为DateTimeOffset

我需要将UTC日期字符串转换为DateTimeOffsets.

这必须使用与计算机时区不同的时区. 例如,当前计算机时区是+02:00,但我想创建一个偏移-4:00的DateTimeOffset.

我已经在stackoverflow上阅读了很多问题,但没有一个能解决我的问题.

这就是我需要做的事情:

输入: "2012-11-20T00:00:00Z"

输出: DateTimeOffset:

  • UtcDateTime 2012-11-20 00:00
  • 定义的时区的正确Utc偏移量(本例中为01:00)
  • LocalDateTime:2012-11-20 01:00 (= UtcDateTime + Offset)

当然,必须考虑夏令时.

编辑:为了使事情更清楚,请尝试完成以下代码段:

DateTimeOffset result;
const string dateString = "2012-11-20T00:00:00Z";
var timezone = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time"); //this timezone has an offset of +01:00:00 on this date

//do conversion here

Assert.AreEqual(result.Offset, new TimeSpan(1, 0, 0));  //the correct utc offset, in this case +01:00:00
Assert.AreEqual(result.UtcDateTime, new DateTime(2012, 11, 20, 0, 0, 0)); //equals the original date
Assert.AreEqual(result.LocalDateTime, …
Run Code Online (Sandbox Code Playgroud)

.net c# datetime datetimeoffset

24
推荐指数
2
解决办法
3万
查看次数

用Moq对类进行部分嘲弄

我想GetValue使用Moq 仅模拟以下类的方法:

public class MyClass
{
    public virtual void MyMethod()
    {
        int value = GetValue();
        Console.WriteLine("ORIGINAL MyMethod: " + value);
    }

    internal virtual int GetValue()
    {
        Console.WriteLine("ORIGINAL GetValue");
        return 10;
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经阅读了一下这应该如何与Moq一起使用.我在网上找到的解决方案是使用该CallBase属性,但这对我不起作用.

这是我的测试:

[Test]
public void TestMyClass()
{
     var my = new Mock<MyClass> { CallBase = true };
     my.Setup(mock => mock.GetValue()).Callback(() => Console.WriteLine("MOCKED GetValue")).Returns(999);
     my.Object.MyMethod();
     my.VerifyAll();
 }
Run Code Online (Sandbox Code Playgroud)

我希望Moq使用现有的实现MyMethod并调用mocked方法,从而产生以下输出:

ORIGINAL MyMethod: 999
MOCKED GetValue
Run Code Online (Sandbox Code Playgroud)

但这就是我得到的:

ORIGINAL GetValue
ORIGINAL MyMethod: 10
Run Code Online (Sandbox Code Playgroud)

然后

Moq.MockVerificationException : The …
Run Code Online (Sandbox Code Playgroud)

.net c# moq mocking

11
推荐指数
1
解决办法
1万
查看次数

标签 统计

.net ×2

c# ×2

datetime ×1

datetimeoffset ×1

mocking ×1

moq ×1