Implement optional table argument to os.time()

This commit is contained in:
James Roseborough
2013-07-14 18:53:03 +00:00
parent 9179e74e36
commit a552494b72
4 changed files with 25 additions and 9 deletions

View File

@@ -459,8 +459,7 @@ A richer version for use by <em>JsePlatform</em> is :
</pre>
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.
<h3>Coroutine Library</h3>
The <em>coroutine</em> library is implemented using one JavaThread per coroutine.
@@ -887,7 +886,7 @@ Files are no longer hosted at LuaForge.
<tr valign="top"><td>&nbsp;&nbsp;<b>3.0-beta2</b></td><td><ul>
<li>LuaValue.checkfunction() now returns LuaFunction.</li>
<li>Fix os.time() to return a number of seconds.</li>
<li>Implement most '%' formatting types for os.date().</li>
<li>Implement formatting with os.date(), and table argument for os.time()..</li>
</ul></td></tr>
</table></td></tr></table>
@@ -902,6 +901,5 @@ Files are no longer hosted at LuaForge.
<li>negative zero is treated as identical to integer value zero throughout luaj
<li>lua compiled into java bytecode using luajc cannot use string.dump() or xpcall()
<li>number formatting with string.format() is not supported
<li>os.time(), and os.date() not completely supported
</ul>

View File

@@ -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.;
}
/**

Binary file not shown.

View File

@@ -54,3 +54,7 @@ for i,k in ipairs({'year', 'month', 'day', 'hour', 'min', 'sec', 'wday', 'yday',
local v = tbl[k]
print('k', type(k), k, 'v', type(v), v)
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}))