diff --git a/README.html b/README.html index 6c932837..6c7f0fe2 100644 --- a/README.html +++ b/README.html @@ -459,8 +459,7 @@ A richer version for use by JsePlatform is : Time is a represented as number of seconds since the epoch, -and most time and date formatting, locales, and other features -are not implemented. +and locales are not implemented.

Coroutine Library

The coroutine library is implemented using one JavaThread per coroutine. @@ -887,7 +886,7 @@ Files are no longer hosted at LuaForge.   3.0-beta2 @@ -902,6 +901,5 @@ Files are no longer hosted at LuaForge.
  • negative zero is treated as identical to integer value zero throughout luaj
  • lua compiled into java bytecode using luajc cannot use string.dump() or xpcall()
  • number formatting with string.format() is not supported -
  • os.time(), and os.date() not completely supported diff --git a/src/core/org/luaj/vm2/lib/OsLib.java b/src/core/org/luaj/vm2/lib/OsLib.java index 14b7a562..632eff4c 100644 --- a/src/core/org/luaj/vm2/lib/OsLib.java +++ b/src/core/org/luaj/vm2/lib/OsLib.java @@ -178,9 +178,8 @@ public class OsLib extends TwoArgFunction { String s = setlocale(args.optjstring(1,null), args.optjstring(2, "all")); return s!=null? valueOf(s): NIL; } - case TIME: { - return valueOf(time(args.arg1().isnil()? null: args.checktable(1))); - } + case TIME: + return valueOf(time(args.opttable(1, null))); case TMPNAME: return valueOf(tmpname()); } @@ -348,6 +347,7 @@ public class OsLib extends TwoArgFunction { y0.set(Calendar.HOUR_OF_DAY, 0); y0.set(Calendar.MINUTE, 0); y0.set(Calendar.SECOND, 0); + y0.set(Calendar.MILLISECOND, 0); return y0; } @@ -466,7 +466,21 @@ public class OsLib extends TwoArgFunction { * @return long value for the time */ protected double time(LuaTable table) { - return (new java.util.Date()).getTime() / 1000.; + java.util.Date d; + if (table == null) { + d = new java.util.Date(); + } else { + Calendar c = Calendar.getInstance(); + c.set(Calendar.YEAR, table.get("year").checkint()); + c.set(Calendar.MONTH, table.get("month").checkint()-1); + c.set(Calendar.DAY_OF_MONTH, table.get("day").checkint()); + c.set(Calendar.HOUR, table.get("hour").optint(12)); + c.set(Calendar.MINUTE, table.get("min").optint(0)); + c.set(Calendar.SECOND, table.get("sec").optint(0)); + c.set(Calendar.MILLISECOND, 0); + d = c.getTime(); + } + return d.getTime() / 1000.; } /** diff --git a/test/lua/luaj3.0-tests.zip b/test/lua/luaj3.0-tests.zip index 3760762d..40b70f02 100644 Binary files a/test/lua/luaj3.0-tests.zip and b/test/lua/luaj3.0-tests.zip differ diff --git a/test/lua/oslib.lua b/test/lua/oslib.lua index 2a4cf3c1..dd40a791 100644 --- a/test/lua/oslib.lua +++ b/test/lua/oslib.lua @@ -53,4 +53,8 @@ local tbl = os.date('*t', t) for i,k in ipairs({'year', 'month', 'day', 'hour', 'min', 'sec', 'wday', 'yday', 'isdst'}) do local v = tbl[k] print('k', type(k), k, 'v', type(v), v) -end \ No newline at end of file +end + +print('type(os.time())', type(os.time())) +print('os.time({year=1971, month=2, day=25})', os.time({year=1971, month=2, day=25})) +print('os.time({year=1971, month=2, day=25, hour=11, min=22, sec=33})', os.time({year=1971, month=2, day=25, hour=11, min=22, sec=33}))