From: Stefan Schantl <stefan.schantl@ipfire.org>
To: development@lists.ipfire.org
Subject: [PATCH 04/19] general-functions.pl: Add formatBytes() function.
Date: Mon, 13 Apr 2020 09:45:35 +0200 [thread overview]
Message-ID: <20200413074550.2735-4-stefan.schantl@ipfire.org> (raw)
In-Reply-To: <20200413074550.2735-1-stefan.schantl@ipfire.org>
[-- Attachment #1: Type: text/plain, Size: 1295 bytes --]
This function can be used to convert an amount of bytes to a
humand-readable format.
For example "3221225472" will become "3MB".
Signed-off-by: Stefan Schantl <stefan.schantl(a)ipfire.org>
---
config/cfgroot/general-functions.pl | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/config/cfgroot/general-functions.pl b/config/cfgroot/general-functions.pl
index 41a0eac2d..692e072c2 100644
--- a/config/cfgroot/general-functions.pl
+++ b/config/cfgroot/general-functions.pl
@@ -1261,4 +1261,29 @@ sub get_nameservers () {
return &uniq(@nameservers);
}
+# Function to format a string containing the amount of bytes to
+# something human-readable.
+sub formatBytes {
+ # Private array which contains the units.
+ my @units = qw(B KB MB GB TB PB);
+
+ my $bytes = shift;
+ my $unit;
+
+ # Loop through the array of units.
+ foreach my $element (@units) {
+ # Break loop if the bytes are less than the next unit.
+ last if $bytes < 1024;
+
+ # Divide bytes amount with 1024.
+ $bytes /= 1024;
+
+ # Assign current processed element to unit.
+ $unit = $element;
+ }
+
+ # Return the divided and rounded bytes count and the unit.
+ return sprintf("%.2f %s", $bytes, $unit);
+}
+
1;
--
2.26.0
next prev parent reply other threads:[~2020-04-13 7:45 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-04-13 7:45 [PATCH 01/19] openvpn: Add WUI page for client usage statistics Stefan Schantl
2020-04-13 7:45 ` [PATCH 02/19] OpenVPN: Fix query when selecting sessions only Stefan Schantl
2020-04-13 7:45 ` [PATCH 03/19] ovpnclients.dat: Fix hard coded language string Stefan Schantl
2020-04-13 7:45 ` Stefan Schantl [this message]
2020-04-13 7:45 ` [PATCH 05/19] ovpnclients.dat: Display traffic details in a human-readable format Stefan Schantl
2020-04-13 7:45 ` [PATCH 06/19] Langs: Add strings for disconnect, sent and recieved Stefan Schantl
2020-04-13 7:45 ` [PATCH 07/19] ovpnclients.dat: Add table header Stefan Schantl
2020-04-13 7:45 ` [PATCH 08/19] ovpnclients.dat: Convert timestamps into localtime Stefan Schantl
2020-04-13 7:45 ` [PATCH 09/19] ovpnclients.dat: Display a notice if there are no entries Stefan Schantl
2020-04-13 7:45 ` [PATCH 10/19] ovpnclients.dat: Display error when the to date is not later than the from date Stefan Schantl
2020-04-13 7:45 ` [PATCH 11/19] ovpnclients.dat: Do not perform DB actions if there is an error message Stefan Schantl
2020-04-13 7:45 ` [PATCH 12/19] general-functions.pl: formatBytes() Fix computing the correct unit Stefan Schantl
2020-04-13 7:45 ` [PATCH 13/19] ovpnclients.dat: Fix type in received Stefan Schantl
2020-04-13 7:45 ` [PATCH 14/19] ovpnclients.dat: Align traffic values to the right side Stefan Schantl
2020-04-13 7:45 ` [PATCH 15/19] OpenVPN: Capitalise some headings and labels Stefan Schantl
2020-04-13 7:45 ` [PATCH 16/19] OpenVPN Log: Add connection duration Stefan Schantl
2020-04-13 7:45 ` [PATCH 17/19] Add ovpnclients page to log menu Stefan Schantl
2020-04-13 7:45 ` [PATCH 18/19] Langs/en.pl: Add duration Stefan Schantl
2020-04-13 7:45 ` [PATCH 19/19] Langs/de.pl: Add translations for OpenVPN roadwarrior connection log Stefan Schantl
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200413074550.2735-4-stefan.schantl@ipfire.org \
--to=stefan.schantl@ipfire.org \
--cc=development@lists.ipfire.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox