1 //
2 // Copyright (c) 2013-2017 Carsten Sonne Larsen <cs@innolan.net>
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 
22 using System;
23 using System.Globalization;
24 using System.Text;
25 
26 namespace Ntp.Analyzer.Monitor.Server.TextCommand
27 {
28     internal sealed class UptimeCommand : MonitorTextCommand
29     {
UptimeCommand(string[] args)30         public UptimeCommand(string[] args)
31             : base(args)
32         {
33         }
34 
ExecuteTextCommand()35         protected override string ExecuteTextCommand()
36         {
37             TimeSpan runtime = DateTime.Now.Subtract(ApplicationState.StartupTime);
38 
39             var builder = new StringBuilder();
40 
41             builder.Append("Daemon was started ");
42             builder.Append(ApplicationState.StartupTime.ToShortDateString());
43             builder.Append(" ");
44             builder.Append(TimeZone.CurrentTimeZone.StandardName);
45             builder.Append(" ");
46             builder.Append(ApplicationState.StartupTime.ToLongTimeString());
47             builder.AppendLine();
48 
49             builder.Append("Uptime ");
50             builder.Append(FormatTimespan(runtime));
51             builder.AppendLine();
52 
53             return builder.ToString();
54         }
55 
FormatTimespan(TimeSpan time)56         private string FormatTimespan(TimeSpan time)
57         {
58             string days;
59 
60             if (time.TotalDays >= 1 && time.TotalDays < 2)
61             {
62                 days = "1 day ";
63             }
64             else if (time.TotalDays >= 2)
65             {
66                 days = time.TotalDays.ToString(CultureInfo.InvariantCulture) + " days ";
67             }
68             else
69             {
70                 days = string.Empty;
71             }
72 
73             string hours;
74 
75             if (time.Hours >= 1 && time.Hours < 2)
76             {
77                 hours = "1 hour ";
78             }
79             else if (time.Hours >= 2)
80             {
81                 hours = time.Hours.ToString(CultureInfo.InvariantCulture) + " hours ";
82             }
83             else
84             {
85                 hours = string.Empty;
86             }
87 
88             string minutes;
89 
90             if (time.Minutes >= 1 && time.Minutes < 2)
91             {
92                 minutes = "1 minute ";
93             }
94             else if (time.Minutes >= 2)
95             {
96                 minutes = time.Minutes.ToString(CultureInfo.InvariantCulture) + " minutes ";
97             }
98             else
99             {
100                 minutes = string.Empty;
101             }
102 
103             string seconds;
104 
105             if (time.Seconds >= 1 && time.Seconds < 2)
106             {
107                 seconds = "1 second";
108             }
109             else if (time.Seconds >= 2)
110             {
111                 seconds = time.Seconds.ToString(CultureInfo.InvariantCulture) + " seconds";
112             }
113             else
114             {
115                 seconds = string.Empty;
116             }
117 
118             return string.Concat(days, hours, minutes, seconds);
119         }
120     }
121 }