diff options
author | Jo-Philipp Wich <jow@openwrt.org> | 2008-06-02 18:02:00 +0000 |
---|---|---|
committer | Jo-Philipp Wich <jow@openwrt.org> | 2008-06-02 18:02:00 +0000 |
commit | a29fb388f5ec4e7f780d43affadc94b9250f1351 (patch) | |
tree | b0e73c8dd60aa581a74aded8c1b106262f968a98 /libs/core | |
parent | f56890a5733b3c57d088b52bf91a77c44976ca99 (diff) |
* luci/core: util.lua: added parse_units() helper function
Diffstat (limited to 'libs/core')
-rw-r--r-- | libs/core/luasrc/util.lua | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/libs/core/luasrc/util.lua b/libs/core/luasrc/util.lua index cc8e7a70b..6b474f6f2 100644 --- a/libs/core/luasrc/util.lua +++ b/libs/core/luasrc/util.lua @@ -287,4 +287,47 @@ function validate(value, cast_number, cast_int) end return value -end
\ No newline at end of file +end + + +-- Parse units from a string and return integer value +function parse_units(ustr) + + local val = 0 + + -- unit map + local map = { + -- date stuff + y = 60 * 60 * 24 * 366, + m = 60 * 60 * 24 * 31, + w = 60 * 60 * 24 * 7, + d = 60 * 60 * 24, + h = 60 * 60, + + -- storage sizes + kb = 1024, + mb = 1024 * 1024, + gb = 1024 * 1024 * 1024, + + -- storage sizes (si) + kib = 1000, + mib = 1000 * 1000, + gib = 1000 * 1000 * 1000 + } + + -- parse input string + for spec in ustr:lower():gmatch("[0-9%.]+[a-zA-Z]*") do + + local num = spec:gsub("[^0-9%.]+$","") + local spn = spec:gsub("^[0-9%.]+", "") + + if map[spn] or map[spn:sub(1,1)] then + val = val + num * ( map[spn] or map[spn:sub(1,1)] ) + else + val = val + num + end + end + + + return val +end |