2011-03-02 55 views
3

我想返回一個字符串,這是語法正確的,以顯示當前的正常運行時間。例如,3年,7個月,11天,1小時,16分鐘和零秒,這意味着單數單位不應該是複數,零單位應該是複數,但是如果它還沒有出現則不應該顯示零(例如,不顯示年,月等,如果它還沒有發生)在語法上糾正正常運行時間在C#

由於ticks方法將不會工作超過一個月,我使用ManagementObject,但我很困惑如何做的日期時間計算和分解(我對C#很新,所以我正在做一個實用程序,做各種功能,所以我可以學習各種各樣的東西。

這是我現在所擁有的,它不是' t heck很多...

任何幫助,非常感謝。

 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 
    } 
+1

只是可以肯定,你從來沒有打算以其他語言做這項工作嗎? – 2011-03-02 16:33:21

回答

4

不要」轉換爲字符串 - 使用最後重啓時間,你已經擁有並將其與當前時間(DateTime.Now)的DateTime。兩者相減,你會得到一個TimeSpan

TimeSpan upDuration = DateTime.Now - boot; 

TimeSpan具有性質日,時,分,你現在可以用它來建立你的字符串。

+0

這就是我的意思 – msarchet 2011-03-02 16:36:35

+0

哦,我明白了,因爲啓動超出了範圍,我只是在循環之前添加了一個構造函數,以便在循環之外訪問啓動。感謝您的幫助! – 2011-03-02 17:17:34

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的積分。 How to calculate age using TimeSpan w/ .NET CF?