View Javadoc
1   /*
2    * Copyright 2020-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.driver.linux.proc;
6   
7   import oshi.annotation.concurrent.ThreadSafe;
8   import oshi.util.FileUtil;
9   import oshi.util.ParseUtil;
10  import oshi.util.platform.linux.ProcPath;
11  
12  /**
13   * Utility to read system uptime from {@code /proc/uptime}
14   */
15  @ThreadSafe
16  public final class UpTime {
17  
18      private UpTime() {
19      }
20  
21      /**
22       * Parses the first value in {@code /proc/uptime} for seconds since boot
23       *
24       * @return Seconds since boot
25       */
26      public static double getSystemUptimeSeconds() {
27          String uptime = FileUtil.getStringFromFile(ProcPath.UPTIME);
28          int spaceIndex = uptime.indexOf(' ');
29          if (spaceIndex < 0) {
30              // No space, error
31              return 0d;
32          }
33          return ParseUtil.parseDoubleOrDefault(uptime.substring(0, spaceIndex), 0d);
34      }
35  }