mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-01-26 00:46:54 +00:00
Put time functions into the OS API
This commit is contained in:
parent
45803e4a49
commit
e0e81a9b18
@ -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,74 @@ 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"));
|
||||
return new Object[] {c.get(Calendar.HOUR_OF_DAY)};
|
||||
}
|
||||
//Get Hour of day (local time)
|
||||
else if (param.equals("local")) {
|
||||
Calendar c = Calendar.getInstance();
|
||||
return new Object[] {c.get(Calendar.HOUR_OF_DAY)};
|
||||
}
|
||||
//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:
|
||||
|
@ -1,111 +0,0 @@
|
||||
package dan200.computercraft.core.apis;
|
||||
|
||||
import org.luaj.vm2.LuaTable;
|
||||
import org.luaj.vm2.LuaValue;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.TimeZone;
|
||||
|
||||
|
||||
public class TimeAPI extends LuaTable {
|
||||
|
||||
private TimeZone timeZone;
|
||||
private Calendar calendarInstance;
|
||||
|
||||
public TimeAPI() {
|
||||
timeZone = TimeZone.getDefault();
|
||||
calendarInstance = Calendar.getInstance( timeZone );
|
||||
}
|
||||
|
||||
public void setTimeZone(String timeZoneId) {
|
||||
timeZone = TimeZone.getTimeZone(timeZoneId);
|
||||
calendarInstance = Calendar.getInstance( timeZone );
|
||||
}
|
||||
|
||||
public LuaValue getResult(int method) {
|
||||
|
||||
//timestamp like PHP without mills!
|
||||
|
||||
Object returnValue = null;
|
||||
int calendarValue = -1;
|
||||
int addValue = 0;
|
||||
|
||||
switch (method) {
|
||||
case 1:
|
||||
//getDay()
|
||||
calendarValue = Calendar.DAY_OF_MONTH;
|
||||
break;
|
||||
case 2:
|
||||
//getMonth()
|
||||
calendarValue = Calendar.MONTH;
|
||||
addValue = 1;
|
||||
break;
|
||||
case 3:
|
||||
//getYear()
|
||||
calendarValue = Calendar.YEAR;
|
||||
break;
|
||||
case 4:
|
||||
//getHour()
|
||||
calendarValue = Calendar.HOUR_OF_DAY;
|
||||
break;
|
||||
case 5:
|
||||
//getMinute()
|
||||
calendarValue = Calendar.MINUTE;
|
||||
break;
|
||||
case 6:
|
||||
//getSecound()
|
||||
calendarValue = Calendar.SECOND;
|
||||
break;
|
||||
case 7:
|
||||
//getTime()
|
||||
return wrap( Math.floor( calendarInstance.getTimeInMillis() / 1000 ) );
|
||||
}
|
||||
|
||||
if (calendarValue != -1 && returnValue == null) {
|
||||
return wrap( calendarInstance.get(calendarValue)+addValue );
|
||||
}
|
||||
else
|
||||
return LuaValue.NIL;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LuaValue get(LuaValue luaValue) {
|
||||
|
||||
String searched = luaValue.toString();
|
||||
|
||||
if (searched.equals("day"))
|
||||
return getResult(1);
|
||||
else if (searched.equals("month"))
|
||||
return getResult(2);
|
||||
else if (searched.equals("year"))
|
||||
return getResult(3);
|
||||
else if (searched.equals("hour"))
|
||||
return getResult(4);
|
||||
else if (searched.equals("minute"))
|
||||
return getResult(5);
|
||||
else if (searched.equals("second"))
|
||||
return getResult(6);
|
||||
else if (searched.equals("timezone"))
|
||||
return wrap(timeZone.getID());
|
||||
else
|
||||
return super.get(luaValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(LuaValue key, LuaValue value) {
|
||||
if (key.toString().equals("timezone")) {
|
||||
setTimeZone( value.toString() );
|
||||
}
|
||||
else {
|
||||
super.set(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
private LuaValue wrap(double number) {
|
||||
return LuaValue.valueOf(number);
|
||||
}
|
||||
|
||||
private LuaValue wrap(String value) {
|
||||
return LuaValue.valueOf(value);
|
||||
}
|
||||
}
|
@ -1,92 +0,0 @@
|
||||
package dan200.computercraft.core.apis;
|
||||
|
||||
import dan200.computercraft.api.lua.ILuaContext;
|
||||
import dan200.computercraft.api.lua.LuaException;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Created by timia2109 (Tim Ittermann) on 04.05.2017.
|
||||
*
|
||||
* @author timia2109 (Tim Ittermann)
|
||||
*/
|
||||
public class TimeUtilsAPI implements ILuaAPI {
|
||||
|
||||
private static final String DEFAULT_TIME_FORMAT = "yyyy-MM-dd";
|
||||
|
||||
private String formatDate(String pattern, Date date) {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
|
||||
return sdf.format(date);
|
||||
}
|
||||
|
||||
private long parseDate(String pattern, String value) throws LuaException {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
|
||||
long timestamp;
|
||||
try {
|
||||
timestamp = sdf.parse(value).getTime();
|
||||
} catch (ParseException parseException) {
|
||||
throw new LuaException("Date can't parsed");
|
||||
}
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getNames() {
|
||||
return new String[]{
|
||||
"timeutils"
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startup() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void advance(double _dt) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shutdown() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getMethodNames() {
|
||||
return new String[]{
|
||||
"format",
|
||||
"parse"
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object[] callMethod(ILuaContext context, int method, Object[] args) throws LuaException, InterruptedException {
|
||||
Calendar cal = Calendar.getInstance();
|
||||
|
||||
if (args.length > 0 && args[0] != null && args[0] instanceof Double) {
|
||||
long argTime = ((Double) args[0]).longValue() * 1000;
|
||||
cal.setTimeInMillis(argTime);
|
||||
}
|
||||
|
||||
switch (method) {
|
||||
case 0:
|
||||
String format = DEFAULT_TIME_FORMAT;
|
||||
if (args.length == 1 && args[0] instanceof String) {
|
||||
format = (String) args[0];
|
||||
} else if (args.length == 2 && args[1] instanceof String) {
|
||||
format = (String) args[1];
|
||||
}
|
||||
return new Object[]{formatDate(format, cal.getTime())};
|
||||
case 1:
|
||||
//parse(String pattern, String input)
|
||||
if (args.length < 2 || args[0] == null || args[1] == null || !(args[0] instanceof String) || !(args[1] instanceof String)) {
|
||||
throw new LuaException("String expected!");
|
||||
}
|
||||
String pattern = (String) args[0],
|
||||
input = (String) args[1];
|
||||
return new Object[]{parseDate(pattern, input) / 1000};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -615,7 +615,6 @@ public class Computer
|
||||
m_apis.add( new PeripheralAPI( m_apiEnvironment ) );
|
||||
m_apis.add( new OSAPI( m_apiEnvironment ) );
|
||||
m_apis.add( new BitAPI( m_apiEnvironment ) );
|
||||
m_apis.add( new TimeUtilsAPI() );
|
||||
//m_apis.add( new BufferAPI( m_apiEnvironment ) );
|
||||
if( ComputerCraft.http_enable )
|
||||
{
|
||||
|
@ -121,9 +121,6 @@ public class LuaJLuaMachine implements ILuaMachine
|
||||
|
||||
m_softAbortMessage = null;
|
||||
m_hardAbortMessage = null;
|
||||
|
||||
//Add to use lua setters at the TimeAPI
|
||||
m_globals.set("time", new TimeAPI());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -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
|
||||
|
||||
New Features in ComputerCraft 1.79:
|
||||
|
||||
|
@ -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()
|
||||
|
@ -3,5 +3,7 @@ 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
|
||||
|
||||
Type "help changelog" to see the full version history.
|
||||
|
Loading…
Reference in New Issue
Block a user