Mercurial Hosting > luan
view core/src/luan/modules/Time.luan @ 264:9e0d4452e649
implement URL style module names
git-svn-id: https://luan-java.googlecode.com/svn/trunk@265 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Wed, 29 Oct 2014 03:50:59 +0000 |
parents | 15122d724ce4 |
children | eb27e765affb |
line wrap: on
line source
-- incomplete, will add as needed import "luan:String" import "luan:Table" import "luan:Java" import "class:java.lang.System" import "class:java.util.Calendar" import "class:java.util.Date" import "class:java.text.SimpleDateFormat" function now() return System.currentTimeMillis() end -- add more as needed local fields = { year = Calendar.YEAR; month = Calendar.MONTH; day_of_month = Calendar.DAY_OF_MONTH; } function get( time, ... ) local cal = Calendar.getInstance() cal.setTimeInMillis(time) local rtn = {} for i, v in ipairs{...} do local fld = fields[v.lower()] fld or error("invalid field: "+v) local n = cal.get(fld) if fld == "month" then n = n + 1 end rtn[i] = n end return Table.unpack(rtn) end function format(time,pattern) return SimpleDateFormat.new(pattern).format(Date.new(time)) end function on( year, month, day, hour, minute, second, millis ) month = month - 1 local cal = Calendar.getInstance() cal.set( year, month, day, hour or 0, minute or 0, second or 0 ) cal.set( Calendar.MILLISECOND, millis or 0 ) return cal.getTimeInMillis() end function parse( pattern, source ) return SimpleDateFormat.new(pattern).parse(source).getTime() end