I want to return a string, that is grammatically correct to display the current uptime. For example, 3 years, 7 months, 11 days, 1 hour, 16 minutes and zero seconds, meaning singular units should not be plural, and zero units should be plural, though zero shouldn't be display if it didn't occur yet (e.g., don't display years, months, etc., if it didn't occur yet)
Since the ticks method won't work beyond a month, I am using ManagementObject, but I am confused on how to do the datetime calculation and break down (I am very new to C# so I am making a utility that does a variety of functions so I can learn a variety of things.
this is what i have now, and it isn't a heck a lot...
any help is greatly appreciated.
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
}
Searching internet I've found something that led me into these pseudo-classes. Might work...
Credits from base-code to Bob Scola. How to calculate age using TimeSpan w/ .NET CF?