@@ -155,29 +155,89 @@ pub fn get_uptime(_boot_time: Option<time_t>) -> UResult<i64> {
155155 Ok ( uptime as i64 / 1000 )
156156}
157157
158+ pub enum OutputFormat {
159+ HumanReadable ,
160+ PrettyPrint ,
161+ }
162+
163+ struct FormattedUptime {
164+ up_days : i64 ,
165+ up_hours : i64 ,
166+ up_mins : i64 ,
167+ }
168+
169+ impl FormattedUptime {
170+ fn new ( up_secs : i64 ) -> Self {
171+ let up_days = up_secs / 86400 ;
172+ let up_hours = ( up_secs - ( up_days * 86400 ) ) / 3600 ;
173+ let up_mins = ( up_secs - ( up_days * 86400 ) - ( up_hours * 3600 ) ) / 60 ;
174+
175+ FormattedUptime {
176+ up_days,
177+ up_hours,
178+ up_mins,
179+ }
180+ }
181+
182+ fn get_human_readable_uptime ( & self ) -> String {
183+ match self . up_days . cmp ( & 1 ) {
184+ std:: cmp:: Ordering :: Equal => format ! (
185+ "{} day, {:2}:{:02}" ,
186+ self . up_days, self . up_hours, self . up_mins
187+ ) ,
188+ std:: cmp:: Ordering :: Greater => format ! (
189+ "{} days, {:2}:{:02}" ,
190+ self . up_days, self . up_hours, self . up_mins
191+ ) ,
192+ _ => format ! ( "{:2}:{:02}" , self . up_hours, self . up_mins) ,
193+ }
194+ }
195+
196+ fn get_pretty_print_uptime ( & self ) -> String {
197+ let day_string = match self . up_days . cmp ( & 1 ) {
198+ std:: cmp:: Ordering :: Equal => format ! ( "{} day, " , self . up_days) ,
199+ std:: cmp:: Ordering :: Greater => format ! ( "{} days, " , self . up_days) ,
200+ _ => String :: new ( ) ,
201+ } ;
202+ let hour_string = match self . up_hours . cmp ( & 1 ) {
203+ std:: cmp:: Ordering :: Equal => format ! ( "{} hour, " , self . up_hours) ,
204+ std:: cmp:: Ordering :: Greater => format ! ( "{} hours, " , self . up_hours) ,
205+ _ => String :: new ( ) ,
206+ } ;
207+ let min_string = match self . up_mins . cmp ( & 1 ) {
208+ std:: cmp:: Ordering :: Equal => format ! ( "{} min" , self . up_mins) ,
209+ _ => format ! ( "{} mins" , self . up_mins) ,
210+ } ;
211+ format ! ( "{}{}{}" , day_string, hour_string, min_string)
212+ }
213+ }
214+
158215/// Get the system uptime in a human-readable format
159216///
160217/// # Arguments
161218///
162219/// boot_time: Option<time_t> - Manually specify the boot time, or None to try to get it from the system.
220+ /// output_format: OutputFormat - Selects the format of the output string.
163221///
164222/// # Returns
165223///
166224/// Returns a UResult with the uptime in a human-readable format(e.g. "1 day, 3:45") if successful, otherwise an UptimeError.
167225#[ inline]
168- pub fn get_formatted_uptime ( boot_time : Option < time_t > ) -> UResult < String > {
226+ pub fn get_formatted_uptime (
227+ boot_time : Option < time_t > ,
228+ output_format : OutputFormat ,
229+ ) -> UResult < String > {
169230 let up_secs = get_uptime ( boot_time) ?;
170231
171232 if up_secs < 0 {
172233 Err ( UptimeError :: SystemUptime ) ?;
173234 }
174- let up_days = up_secs / 86400 ;
175- let up_hours = ( up_secs - ( up_days * 86400 ) ) / 3600 ;
176- let up_mins = ( up_secs - ( up_days * 86400 ) - ( up_hours * 3600 ) ) / 60 ;
177- match up_days. cmp ( & 1 ) {
178- std:: cmp:: Ordering :: Equal => Ok ( format ! ( "{up_days:1} day, {up_hours:2}:{up_mins:02}" ) ) ,
179- std:: cmp:: Ordering :: Greater => Ok ( format ! ( "{up_days:1} days {up_hours:2}:{up_mins:02}" ) ) ,
180- _ => Ok ( format ! ( "{up_hours:2}:{up_mins:02}" ) ) ,
235+
236+ let formatted_uptime = FormattedUptime :: new ( up_secs) ;
237+
238+ match output_format {
239+ OutputFormat :: HumanReadable => Ok ( formatted_uptime. get_human_readable_uptime ( ) ) ,
240+ OutputFormat :: PrettyPrint => Ok ( formatted_uptime. get_pretty_print_uptime ( ) ) ,
181241 }
182242}
183243
0 commit comments