# HG changeset patch # User HackEso # Date 1574018180 0 # Node ID db60ce0f0bb349aa9fa9706c150e1edc17aeb66d # Parent d58ddfcf5f072d4987b66dfb948690ec48bf5bbe fetch /hackenv/bin/uptime https://hack.esolangs.org/get/bin/uptime diff -r d58ddfcf5f07 -r db60ce0f0bb3 bin/uptime --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bin/uptime Sun Nov 17 19:16:20 2019 +0000 @@ -0,0 +1,40 @@ +#!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)) +