Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

Swift – System Uptime

Posted on October 18, 2017October 18, 2017 By David Kittell

I specifically use this in an iOS app but you should be able to use this in iOS or Mac OS

The first function will get the Uptime of the system in seconds while the second function will convert the seconds into a easier to read/understand format.

func uptime() -> time_t {
        var boottime = timeval()
        var mib: [Int32] = [CTL_KERN, KERN_BOOTTIME]
        var size = MemoryLayout<timeval>.stride
        
        var now = time_t()
        var uptime: time_t = -1
        
        time(&now)
        if (sysctl(&mib, 2, &boottime, &size, nil, 0) != -1 && boottime.tv_sec != 0) {
            uptime = now - boottime.tv_sec
        }
        return uptime
    }
   
func PrintSecondsToHumanReadable (seconds:Int) -> String  {
        let sDays = String((seconds / 86400)) + " days"
        let sHours = String((seconds % 86400) / 3600) + " hours"
        let sMinutes = String((seconds % 3600) / 60) + " minutes"
        let sSeconds = String((seconds % 3600) % 60) + " seconds"
        
        var sHumanReadable = ""
        
        if ((seconds / 86400) > 0)
        {
            sHumanReadable = sDays + ", " + sHours + ", " + sMinutes + ", " + sSeconds
        }
        else if (((seconds % 86400) / 3600) > 0)
        {
            sHumanReadable = sHours + ", " + sMinutes + ", " + sSeconds
        }
        else if (((seconds % 3600) / 60) > 0)
        {
            sHumanReadable = sMinutes + ", " + sSeconds
        }
        else if (((seconds % 3600) % 60) > 0)
        {
            sHumanReadable = sSeconds
        }
        return sHumanReadable
    }
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Code Swift

Post navigation

Previous post
Next post

Related Posts

UNIX – SED Replace Tab with Space(s)

Posted on December 20, 2017

sed ‘s/\t/ /g’ tab-file.txt > no-tab-file.txt Originally found at https://linuxconfig.org/replace-all-tab-characters-with-spaces All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just…

Read More

UNIX – Create Sudoers Access

Posted on August 5, 2016August 5, 2016

# Variables – Start username="dkittell" # Variables – Stop su # type in sudo password chmod -v +w /etc/sudoers && echo -e "$username ALL=(ALL) ALL" >> /etc/sudoers && chmod -v -w /etc/sudoers # Variables – Start username="dkittell" # Variables – Stop su # type in sudo password # Remove Sudoers…

Read More

Delimiter Split

Posted on September 30, 2013October 26, 2015

IF OBJECT_ID(N’dbo.fnSplit’) IS NOT NULL DROP FUNCTION dbo.fnSplit GO CREATE FUNCTION dbo.fnSplit( @sInputList VARCHAR(8000) — List of delimited items , @sDelimiter VARCHAR(8000) = ‘,’ — delimiter that separates items ) RETURNS @List TABLE (item VARCHAR(8000)) BEGIN DECLARE @sItem VARCHAR(8000) WHILE CHARINDEX(@sDelimiter,@sInputList,0) <> 0 BEGIN SELECT @sItem=RTRIM(LTRIM(SUBSTRING(@sInputList,1,CHARINDEX(@sDelimiter,@sInputList,0)-1))), @sInputList=RTRIM(LTRIM(SUBSTRING(@sInputList,CHARINDEX(@sDelimiter,@sInputList,0)+LEN(@sDelimiter),LEN(@sInputList)))) IF LEN(@sItem) >…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes