3

我想返回一个字符串,它在语法上是正确的,可以显示当前的正常运行时间。例如,3 年 7 个月 11 天 1 小时 16 分钟和零秒,表示单数单位不应为复数,零单位应为复数,但如果尚未出现则不应显示零(例如,不要显示年、月等,如果它还没有发生)

由于 ticks 方法不会超过一个月,我正在使用 ManagementObject,但我对如何进行日期时间计算和分解感到困惑(我对 C# 非常陌生,所以我正在制作一个执行各种功能的实用程序所以我可以学习各种各样的东西。

这就是我现在所拥有的,而且不是很多......

任何帮助是极大的赞赏。

        public string getUptime()
    {
        // this should be grammatically correct, meaning, 0 and greater than 1 should be plural, one should be singular
        // if it can count in realtime, all the better
        // in rare case, clipping can occur if the uptime is really, really huge

        // you need a function that stores the boot time in a global variable so it's executed once so repainting this won't slow things down with quer
        SelectQuery query = new SelectQuery("SELECT LastBootUpTime FROM Win32_OperatingSystem WHERE Primary='true'");
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
        foreach (ManagementObject mo in searcher.Get())
        {
            // this is the start time, do math to figure it out now, hoss
            DateTime boot = ManagementDateTimeConverter.ToDateTime(mo.Properties["LastBootUpTime"].Value.ToString());
            // or is it better to break up everything into days/hours/etc, i think its better to do that at the end before it gets returned
        }

        string now = DateTime.Now.ToShortDateString();







        return "3 years, 7 months, 11 days, 1 hour, 16 minutes and zero seconds";  // long string for testing label width
    }
4

2 回答 2

5

不要'转换为字符串 - 使用DateTime您已经拥有的上次重启时间并将其与当前时间 ( DateTime.Now) 进行比较。减去两者,你得到TimeSpan

TimeSpan upDuration = DateTime.Now - boot;

TimeSpan具有 Days、Hours、Minutes 属性,您现在可以使用它们来构建字符串。

于 2011-03-02T16:34:16.663 回答
0

搜索互联网我发现了一些导致我进入这些伪类的东西。可能工作...

class Utils
{

    public DateTime GetLastDateTime()
    {
        // Insert code here to return the last DateTime from your server.
        // Maybe from a database or file?

        // For now I'll just use the current DateTime:
        return DateTime.Now;
    }

    public CustomTimeSpan GetCurrentTimeSpan()
    {
        // You don't actually need this method. Just to expalin better...
        // Here you can get the diference (timespan) from one datetime to another:

        return new CustomTimeSpan(GetLastDateTime(), DateTime.Now);
    }

    public string FormatTimeSpan(CustomTimeSpan span)
    {
        // Now you can format your string to what you need, like:

        String.Format("{0} year{1}, {2} month{3}, {4} day{5}, {6} hour{7}, {8} minute{9} and {10} second{11}",
            span.Years, span.Years > 1 ? "s" : "",
            span.Months, span.Monts > 1 ? "s" : "",
            span.Days, span.Days > 1 ? "s" : "",
            span.Hours, span.Hours > 1 : "s" : "",
            span.Minutes, span.Minutes > 1 : "s" : "",
            span.Seconds, span.Seconds > 1 : "s" : "");
    }

}

class CustomTimeSpan : TimeSpan
{

    public int Years { get; private set; }
    public int Months { get; private set; }
    public int Days { get; private set; }
    public int Hours { get; private set; }
    public int Minutes { get; private set; }
    public int Seconds { get; private set; }

    public CustomTimeSpan ( DateTime originalDateTime, DateTime actualDateTime )
    {
        var span = actualDateTime - originalDateTime;

        this.Seconds = span.Seconds;
        this.Minutes = span.Minutes;
        this.Hours = span.Hours;

        // Now comes the tricky part: how to get the day, month and year part...
        var months = 12 * (actualDateTime.Year - originalDateTime.Year) + (actualDateTime.Month - originalDateTime.Month);
        int days = 0;

        if (actualDateTime.Day < originalDateTime.Day)
        {
            months--;
            days = GetDaysInMonth(originalDateTime.Year, originalDateTime.Month) - originalDateTime.Day + actualDateTime.Day;
        }
        else
        {
            days = actualDateTime.Day - originalDateTime.Day;
        }

        this.Years = months / 12;
        months -= years * 12;
        this.Months = months;
        this.Days = days;
    }


}

从基本代码到 Bob Scola 的学分。 如何使用带 .NET CF 的 TimeSpan 计算年龄?

于 2011-03-02T17:22:24.567 回答