changeset 12137:db60ce0f0bb3 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:16:20 +0000
parents d58ddfcf5f07
children 08fae3c17105
files bin/uptime
diffstat 1 files changed, 40 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /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))
+