NUnit 测试没有 .Net Timespan 或 DateTime 函数的 DateTime

发布于 2024-07-09 21:36:21 字数 154 浏览 11 评论 0原文

以下的 C# 优化版本是什么,没有使用 .Net 的 Timespan 或 DateTime。 我将如何 NUnit 测试它?

TimeSpan ts = Date1 - Date2;
int numberOfDays = ts.Days;

What is the C# optimised version of the following, without using .Net's Timespan or DateTime. How would I NUnit test it?

TimeSpan ts = Date1 - Date2;
int numberOfDays = ts.Days;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼眸 2024-07-16 21:36:21

您可以将日期转换为刻度,减去,然后将刻度转换回天。 但到底为什么不能使用 TimeSpan 呢? 它很可能就是这么做的。

像这样的事情:

      DateTime a = DateTime.Now;
      DateTime b = a.AddDays(2);

      // ticks are in hns
      long ticks = b.Ticks - a.Ticks;
      long seconds = ticks / 10000000;
      long minutes = seconds / 60;
      long hours = minutes / 60;
      long days = hours / 24;

我几天后才回到“2”。

You could convert the dates to ticks, substract, then convert the ticks back to days. Though exactly why can't you use TimeSpan? It's likely doing just that.

Something like this:

      DateTime a = DateTime.Now;
      DateTime b = a.AddDays(2);

      // ticks are in hns
      long ticks = b.Ticks - a.Ticks;
      long seconds = ticks / 10000000;
      long minutes = seconds / 60;
      long hours = minutes / 60;
      long days = hours / 24;

I get back "2" for days.

蓝天 2024-07-16 21:36:21

Date1和Date2是什么类型? 在您的示例中,它看起来是日期时间。 如果日期不是日期时间,您希望将日期放在什么变量中? 您始终可以将 Date1 和 Date2 放在 String 中,并使用 SubString() 来获取年、月和日,但这确实是一个痛苦的工作。

解决您的问题的优化方法

TimeSpan ts = Date1 - Date2;
int numberOfDays = ts.Days;

是:

DateTime Date1 = new DateTime(2008,12,01);
DateTime Date2 = new DateTime(2008,12,25);
int numberOfDayBetweenDateThatShouldBe = 25;
Assert.IsTrue((Date2-Date1).TotalDays == numberOfDayBetweenDateThatShouldBe);

Date1 and Date2 are what type? In your exameple it looks to be DateTime. You want your Date in what variable if it's not DateTime? You can always have your Date1 and Date2 in String and play with SubString() to get Year, Month and days but that would be a real pain to work with.

The optimized way to do your problem of:

TimeSpan ts = Date1 - Date2;
int numberOfDays = ts.Days;

is

DateTime Date1 = new DateTime(2008,12,01);
DateTime Date2 = new DateTime(2008,12,25);
int numberOfDayBetweenDateThatShouldBe = 25;
Assert.IsTrue((Date2-Date1).TotalDays == numberOfDayBetweenDateThatShouldBe);
撞了怀 2024-07-16 21:36:21

谢谢,这是家庭作业 (asp.net)

任务是用 C# 编写一个 ASP.net 应用程序,该应用程序接受用户输入的 2 个日期,计算出它们之间的天数并将该数字显示给用户。 这不应使用任何内置的 .net 框架 DateTime 或 TimeSpan 类。

唉,我在生产强度上最优雅的实现

private int CalculateDays(DateTime start, DateTime end )
{
   DateTime origin = new DateTime(); 
   return (end - origin).Days - (start - origin).Days;
}

protected void Button1_Click(object sender, EventArgs e)
{
    // Parse dates for correctness and range errors, warn as necessary
    DateTime start;
    DateTime end;

    // rough error implement error handling implementation   
    string errors = string.Empty;

         if(!DateTime.TryParse(txt_start_date.Text.Trim(), out start))   errors+="Start date was incorrect";
    else if(!DateTime.TryParse(txt_end_date.Text.Trim(), out end))   errors+= (errors.Length>0? errors+= "\n":"") + "End date was incorrect" ; 
    else if ((end.Day - start.Day) <= 0) errors+= (errors.Length>0? errors+= "\n":"" ) + "End date must be greater than the Start date" ;         //CultureInfo.InvariantCulture
    else 
    {
        Response.Write(CalculateDays(start, end)); 
    }
    Debug.Assert(errors.Length <= 0, errors); 
}

原因是我无法看到如何在不接触 DateTime.Days 的情况下至少做到这一点。


编辑
好吧,再想一想,有点麻烦,但是,在任何地方都没有使用 DateTime 或 Timespan

public static class PrimitiveCal
{
    // incremental total days a normal year 
    private static int[] DaysInYr = new int[] {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };

    // incremental total days in a leap year 
    private static int[] DaysInLeapYr = new int[] { 0,31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };


    public static long GetDays(int[] dt_start, int[] dt_end)
    {

        int day_diff = dt_end[0] - dt_start[0];
        int s_mth = dt_start[1];
        int e_mth = dt_end[1];
        int s_year = dt_start[2];
        int e_year = dt_end[2];
        int yr_diff = e_year - s_year;

        return day_diff + (yr_diff > 0 ?
                         // use months as boundaries, cater for leap years
                         (YrType(e_year)[e_mth - 1] +
                         (YrType(s_year)[YrType(s_year).Length - 1] - YrType(s_year)[s_mth - 1])) +
                         (yr_diff == 1 ? 0 : AddMiddleYears(s_year, e_year))

                         // get month sums in same year 
                         : YrType(e_year)[e_mth - 1] - YrType(e_year)[s_mth - 1]);
    }

    private static int AddMiddleYears(int s_year, int e_year)
    {
        int total_days = 0;
        for (int i = s_year + 1; i <= e_year - 1; i++) total_days += YrType(i)[YrType(i).Length - 1];
        return total_days;
    }

    private static int[] YrType(int year)
    {
        return (year % 4  == 0 &&  year%100!=0) || year % 400 ==0 ? DaysInLeapYr : DaysInYr;
    }
}

thanks, this is homework (asp.net)

The task is to write an asp.net application in c# that takes 2 dates as input from the user, works out the number of days between them and displays that number back to the user. This should not use any of the in-built .net framework DateTime or TimeSpan classes.

and alas my most elegant implementation at production strength

private int CalculateDays(DateTime start, DateTime end )
{
   DateTime origin = new DateTime(); 
   return (end - origin).Days - (start - origin).Days;
}

protected void Button1_Click(object sender, EventArgs e)
{
    // Parse dates for correctness and range errors, warn as necessary
    DateTime start;
    DateTime end;

    // rough error implement error handling implementation   
    string errors = string.Empty;

         if(!DateTime.TryParse(txt_start_date.Text.Trim(), out start))   errors+="Start date was incorrect";
    else if(!DateTime.TryParse(txt_end_date.Text.Trim(), out end))   errors+= (errors.Length>0? errors+= "\n":"") + "End date was incorrect" ; 
    else if ((end.Day - start.Day) <= 0) errors+= (errors.Length>0? errors+= "\n":"" ) + "End date must be greater than the Start date" ;         //CultureInfo.InvariantCulture
    else 
    {
        Response.Write(CalculateDays(start, end)); 
    }
    Debug.Assert(errors.Length <= 0, errors); 
}

Reason being am unable to see how to do this without touching DateTime.Days at least.


edit
okay thought over again and a bit of monkey work, however, no use of DateTime or Timespan anywhere

public static class PrimitiveCal
{
    // incremental total days a normal year 
    private static int[] DaysInYr = new int[] {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };

    // incremental total days in a leap year 
    private static int[] DaysInLeapYr = new int[] { 0,31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 };


    public static long GetDays(int[] dt_start, int[] dt_end)
    {

        int day_diff = dt_end[0] - dt_start[0];
        int s_mth = dt_start[1];
        int e_mth = dt_end[1];
        int s_year = dt_start[2];
        int e_year = dt_end[2];
        int yr_diff = e_year - s_year;

        return day_diff + (yr_diff > 0 ?
                         // use months as boundaries, cater for leap years
                         (YrType(e_year)[e_mth - 1] +
                         (YrType(s_year)[YrType(s_year).Length - 1] - YrType(s_year)[s_mth - 1])) +
                         (yr_diff == 1 ? 0 : AddMiddleYears(s_year, e_year))

                         // get month sums in same year 
                         : YrType(e_year)[e_mth - 1] - YrType(e_year)[s_mth - 1]);
    }

    private static int AddMiddleYears(int s_year, int e_year)
    {
        int total_days = 0;
        for (int i = s_year + 1; i <= e_year - 1; i++) total_days += YrType(i)[YrType(i).Length - 1];
        return total_days;
    }

    private static int[] YrType(int year)
    {
        return (year % 4  == 0 &&  year%100!=0) || year % 400 ==0 ? DaysInLeapYr : DaysInYr;
    }
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文