Mercurial > repo
view bin/uptime @ 12139:c2f4b787425f draft
<b_jonas> fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime
author | HackEso <hackeso@esolangs.org> |
---|---|
date | Sun, 17 Nov 2019 19:17:14 +0000 |
parents | db60ce0f0bb3 |
children | 2826c0c87d92 |
line wrap: on
line source
#!/usr/bin/python3 boottime = 1245511822 import sys, 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: print("uptime from procps-ng 3.3.15") 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(0) 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))