Re: off topic: uptime question
-----BEGIN PGP SIGNED MESSAGE-----
On Tue, Oct 24, 2017 at 12:50:03AM +0900, Byung-Hee HWANG (황병희, 黃炳熙) wrote:
> #+BEGIN_SRC ruby
> require "date"
> uptime_data = IO.read('/proc/uptime').split.to_i
> sboot = Time.now - uptime_data
> if uptime_data >= 86400
> uptime = Time.at(uptime_data).utc.strftime("%d days %H:%M")
> uptime = Time.at(uptime_data).utc.strftime("%H:%M")
> puts uptime.to_s + " (" + sboot.to_s + ")"
> Then i do ruby cmd and shell cmd at Ubuntu (chromebook).
> Ruby: 28 days 09:23 (Tue Sep 26 15:19:58 +0900 2017)
> Bash: 00:43:33 up 27 days, 9:23, 0 users, load average: 0.01, 0.10, 0.21
> Why the result is different? Ruby result is more 1 day than shell result.
> Guide me please... thanks in advance^^^
> Sincerely, Byung-Hee.
This is more of a Ruby question. *If* (a big if, I don't know Ruby that well)
strftime is similar to the Unix library function strftime, the "%d" placeholder
you use there stands for the "day of the month", which counts from one, not from
zero. This function is made for the formatting of *dates*, not *time intervals.
That would mean that you might get even funnier results if your uptime
is more than (around) 30-31 days.
If that is the case I fear you'll have to find other conversion functions
or write up something yourself.
- -- tomás
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)
-----END PGP SIGNATURE-----