1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-03-13 15:08:12 +00:00

Merge pull request #183 from timia2109/master

Add real world time support
This commit is contained in:
Daniel Ratcliffe 2017-05-06 22:26:14 +01:00 committed by GitHub
commit 3e3c1239b0
4 changed files with 74 additions and 12 deletions

View File

@ -10,10 +10,7 @@ import dan200.computercraft.api.lua.ILuaContext;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.shared.util.StringUtil;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.*;
public class OSAPI implements ILuaAPI
{
@ -294,17 +291,78 @@ public class OSAPI implements ILuaAPI
case 11:
{
// m_time
synchronized( m_alarms )
{
return new Object[] { m_time };
if (args.length == 0) {
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");
}
}
case 12:
{
// day
synchronized( m_alarms )
{
return new Object[] { m_day };
if (args.length == 0 ) {
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");
}
}
case 13:

View File

@ -3,6 +3,8 @@ 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
New Features in ComputerCraft 1.79:

View File

@ -12,8 +12,8 @@ os.clock()
os.startTimer( timeout )
os.cancelTimer( token )
os.sleep( timeout )
os.time()
os.day()
os.time( [source] )
os.day( [source] )
os.setAlarm( time )
os.cancelAlarm( token )
os.shutdown()

View File

@ -3,6 +3,8 @@ 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
Type "help changelog" to see the full version history.