Fixed incorrectness in os.day(). Added os.epoch()

This commit is contained in:
Daniel Ratcliffe 2017-05-06 23:18:50 +01:00
parent 58713caa73
commit cad10fa2c7
4 changed files with 114 additions and 70 deletions

View File

@ -185,9 +185,36 @@ public String[] getMethodNames()
"day",
"cancelTimer",
"cancelAlarm",
"epoch"
};
}
private float getTimeForCalendar(Calendar c)
{
float time = c.get(Calendar.HOUR_OF_DAY);
time += (float)c.get(Calendar.MINUTE) / 60.0f;
time += (float)c.get(Calendar.SECOND) / (60.0f * 60.0f);
return time;
}
private int getDayForCalendar(Calendar c)
{
GregorianCalendar g = (c instanceof GregorianCalendar) ? (GregorianCalendar)c : new GregorianCalendar();
int year = c.get(Calendar.YEAR);
int day = 0;
for( int y=1970; y<year; ++y )
{
day += g.isLeapYear(y) ? 366 : 365;
}
day += c.get(Calendar.DAY_OF_YEAR);
return day;
}
private long getEpochForCalendar(Calendar c)
{
return c.getTime().getTime();
}
@Override
public Object[] callMethod( ILuaContext context, int method, Object[] args ) throws LuaException
{
@ -290,79 +317,63 @@ public Object[] callMethod( ILuaContext context, int method, Object[] args ) thr
}
case 11:
{
// m_time
if (args.length == 0) {
// time
String param = "ingame";
if (args.length > 0 && args[0] != null) {
if (!(args[0] instanceof String)) {
throw new LuaException("Expected string");
}
param = (String)args[0];
}
if (param.equals("utc")) {
// Get Hour of day (UTC)
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
return new Object[] {getTimeForCalendar(c)};
} else if (param.equals("local")) {
// Get Hour of day (local time)
Calendar c = Calendar.getInstance();
return new Object[] {getTimeForCalendar(c)};
} else if (param.equals("ingame")) {
// Get ingame hour
synchronized (m_alarms) {
return new Object[]{m_time};
}
}
else if (args.length > 0 && args[0] != null && args[0] instanceof String) {
String param = (String) args[0];
//Get Hour of day (UTC)
if (param.equals("utc")) {
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
float hourOfDay = c.get(Calendar.HOUR_OF_DAY);
hourOfDay += ((float)c.get(Calendar.MINUTE)/60)+(float)(c.get(Calendar.SECOND)/60*60);
return new Object[] {hourOfDay};
}
//Get Hour of day (local time)
else if (param.equals("local")) {
Calendar c = Calendar.getInstance();
float hourOfDay = c.get(Calendar.HOUR_OF_DAY);
hourOfDay += ((float)c.get(Calendar.MINUTE)/60)+(float)(c.get(Calendar.SECOND)/60*60);
return new Object[] {hourOfDay};
}
//Get ingame hour
else if (param.equals("ingame")) {
return callMethod(context, method, new Object[0]);
}
//Get timestamp (without mills)
else if (param.equals("timestamp")) {
long timestamp = Calendar.getInstance().getTimeInMillis()/1000;
return new Object[]{timestamp};
}
else {
throw new LuaException("Unsupported operation");
}
}
else {
throw new LuaException("Expected string");
} else {
throw new LuaException("Unsupported operation");
}
}
case 12:
{
// day
if (args.length == 0 ) {
String param = "ingame";
if (args.length > 0 && args[0] != null) {
if (!(args[0] instanceof String)) {
throw new LuaException("Expected string");
}
param = (String)args[0];
}
if (param.equals("utc")) {
// Get numbers of days since 1970-01-01 (utc)
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
return new Object[] {getDayForCalendar(c)};
} else if (param.equals("local")) {
// Get numbers of days since 1970-01-01 (local time)
Calendar c = Calendar.getInstance();
return new Object[] {getDayForCalendar(c)};
} else if (param.equals("ingame")){
// Get game day
synchronized (m_alarms) {
return new Object[]{m_day};
}
}
else if (args.length > 0 && args[0] != null && args[0] instanceof String) {
String param = (String) args[0];
//Get numbers of days since 1970-01-01 (utc)
if (param.equals("utc")) {
long timestamp = Calendar.getInstance().getTimeInMillis();
timestamp /= 86400000; //Secounds of a day
return new Object[] {timestamp};
}
//Get numbers of days since 1970-01-01 (local time)
else if (param.equals("local")) {
long timestamp = Calendar.getInstance().getTimeInMillis();
int offset = TimeZone.getDefault().getRawOffset();
timestamp += offset; //Add TZOffset to mills
timestamp /= 86400000; //Secounds of a day
return new Object[] {timestamp};
}
//Get game day
else if (param.equals("ingame")){
return callMethod(context, method, new Object[0]); //Normal os.day()
}
else {
throw new LuaException("Unsupported operation");
}
}
else {
throw new LuaException("Expected string");
} else {
throw new LuaException("Unsupported operation");
}
}
case 13:
@ -399,6 +410,38 @@ else if (param.equals("ingame")){
}
return null;
}
case 15:
{
// epoch
String param = "ingame";
if (args.length > 0 && args[0] != null) {
if (!(args[0] instanceof String)) {
throw new LuaException("Expected string");
}
param = (String)args[0];
}
if (param.equals("utc")) {
// Get utc epoch
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
return new Object[] {getEpochForCalendar(c)};
} else if (param.equals("local")) {
// Get local epoch
Calendar c = Calendar.getInstance();
return new Object[] {getEpochForCalendar(c)};
} else if (param.equals("ingame")){
// Get in-game epoch
synchronized (m_alarms) {
return new Object[] {
m_day * 86400000 + (int)(m_time * 3600000.0f)
};
}
} else {
throw new LuaException("Unsupported operation");
}
}
default:
{
return null;

View File

@ -2,10 +2,10 @@ New Features in ComputerCraft 1.80:
* Added .getResponseHeaders() to HTTP responses.
* Return a HTTP response when a HTTP error occurs.
* Added a GUI to change ComputerCraft config options
* os.time( ... ) now accept parameters to get real timestamp and real hour of day
* os.day ( ... ) now accept parameters to get real day since 1970-01-01
* Monitor text now glows in the dark
* Added a GUI to change ComputerCraft config options.
* os.time() and os.day() now accept parameters to give the real world time.
* Added os.epoch()
* Monitor text now glows in the dark.
New Features in ComputerCraft 1.79:

View File

@ -14,6 +14,7 @@ os.cancelTimer( token )
os.sleep( timeout )
os.time( [source] )
os.day( [source] )
os.epoch( [source] )
os.setAlarm( time )
os.cancelAlarm( token )
os.shutdown()

View File

@ -2,9 +2,9 @@ New Features in ComputerCraft 1.80:
* Added .getResponseHeaders() to HTTP responses.
* Return a HTTP response when a HTTP error occurs.
* Added a GUI to change ComputerCraft config options
* os.time( ... ) now accept parameters to get real timestamp and real hour of day
* os.day ( ... ) now accept parameters to get real day since 1970-01-01
* Monitor text now glows in the dark
* Added a GUI to change ComputerCraft config options.
* os.time() and os.day() now accept parameters to give the real world time.
* Added os.epoch()
* Monitor text now glows in the dark.
Type "help changelog" to see the full version history.