view bin/uptime @ 12256:821155c00e34 draft

<fizzie> ` sed -e \'s|wisdom|bin|\' < ../bin/culprits > ../bin/cblprits; chmod a+x ../bin/cblprits
author HackEso <hackeso@esolangs.org>
date Sat, 07 Dec 2019 23:36:22 +0000
parents f35125e00403
children
line wrap: on
line source

#!/usr/bin/python3
# (/usr/bin/uptime -p) is broken, so here's a reimplementation
boottime = 1245511822
import sys, os, time
helpmsg = "\nUsage:\n uptime [options]\n\nOptions:\n -p, --pretty   show uptime in pretty format\n -h, --help     display this help and exit\n -s, --since    system up since\n -V, --version  output version information and exit\n\nFor more details see uptime(1)."
opt_p, opt_s = 0, 0
for a in sys.argv[1:]:
    if "--" == a:
        break
    if "-" == a[0]:
        for c in a[1:]:
            if "p" == c:
                opt_p = 1
            elif "s" == c:
                opt_s = 1
            elif "V" == c:
                os.execl("/usr/bin/uptime", "/usr/bin/uptime", "-V")
                print("uptime from procps-ng 2.95.1")
                sys.exit(0)
            elif "h" == c:
                print(helpmsg, file = sys.stdout)
                sys.exit(0)
            else:
                print("uptime: invalid option -- '" + c + "'\n" + helpmsg, file = sys.stderr)
                sys.exit(1)
if opt_s:
    print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(boottime)))
else:
    now = time.time()
    uptime_s = now - boottime
    uptime_d, uptime_md = divmod(uptime_s, 60*60*24)
    uptime_h, uptime_mh = divmod(uptime_md, 60*60)
    uptime_m = uptime_mh // 60
    if opt_p:
        print("up %d day, %d hours, %d minutes" % 
            (uptime_d, uptime_h, uptime_m))
    else:
        nowfmt = time.strftime("%H:%M:%S", time.localtime(now))
        loadavg = ", ".join(open("/proc/loadavg").read().split()[0:3])
        print(" %s up %d day, %2d:%02d,  0 users,  load average: %s" %
            (nowfmt, uptime_d, uptime_h, uptime_m, loadavg))