3

現在の稼働時間を表示するのに文法的に正しい文字列を返したいです。たとえば、3 年 7 か月 11 日 1 時間 16 分 0 秒は、単数単位は複数形であってはならず、0 単位は複数形であるべきであることを意味します。 (例: 年、月などをまだ表示していない場合は表示しない)

ticks メソッドは 1 か月を超えると機能しないため、ManagementObject を使用していますが、datetime の計算と分解の方法について混乱しています (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) と比較します。2 つを引くと、次のようになります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 へのクレジット。 TimeSpan w/.NET CFを使用して年齢を計算するには?

于 2011-03-02T17:22:24.567 に答える