Mercurial Hosting > luan
changeset 1079:53e77f60b789
add Time.count
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 10 Nov 2016 21:15:28 -0700 |
parents | 99f1c125926f |
children | 0d884377e923 |
files | src/luan/modules/Time.luan |
diffstat | 1 files changed, 27 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
diff -r 99f1c125926f -r 53e77f60b789 src/luan/modules/Time.luan --- a/src/luan/modules/Time.luan Thu Nov 10 20:41:03 2016 -0700 +++ b/src/luan/modules/Time.luan Thu Nov 10 21:15:28 2016 -0700 @@ -4,6 +4,8 @@ local Luan = require "luan:Luan.luan" local error = Luan.error local ipairs = Luan.ipairs or error() +local Math = require "luan:Math.luan" +local floor = Math.floor or error() local Table = require "luan:Table.luan" local System = require "java:java.lang.System" local Calendar = require "java:java.util.Calendar" @@ -29,8 +31,8 @@ cal.setTimeInMillis(time) local rtn = {} for i, v in ipairs{...} do - local fld = fields[v.lower()] - fld or error("invalid field: "+v) + local fld = fields[v] + fld or error("invalid field: "..v) local n = cal.get(fld) if fld == "month" then n = n + 1 @@ -68,4 +70,27 @@ return SimpleDateFormat.new(pattern).parse(source).getTime() end + +local count_times = { + days = M.period{days=1} + hours = M.period{hours=1} + minutes = M.period{minutes=1} + seconds = M.period{seconds=1} +} + +function M.count( time, ... ) + local cal = Calendar.getInstance() + cal.setTimeInMillis(time) + local rtn = {} + for i, v in ipairs{...} do + local t = count_times[v] + t or error("invalid field: "..v) + rtn[#rtn+1] = floor(time/t) + time = time%t + end + rtn[#rtn+1] = time + return Table.unpack(rtn) +end + + return M