mirror of
				https://github.com/SquidDev-CC/CC-Tweaked
				synced 2025-10-30 05:12:58 +00:00 
			
		
		
		
	Fixed incorrectness in os.day(). Added os.epoch()
This commit is contained in:
		| @@ -185,9 +185,36 @@ public class OSAPI implements ILuaAPI | |||||||
|             "day", |             "day", | ||||||
|             "cancelTimer", |             "cancelTimer", | ||||||
|             "cancelAlarm", |             "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 |     @Override | ||||||
|     public Object[] callMethod( ILuaContext context, int method, Object[] args ) throws LuaException |     public Object[] callMethod( ILuaContext context, int method, Object[] args ) throws LuaException | ||||||
|     { |     { | ||||||
| @@ -290,79 +317,63 @@ public class OSAPI implements ILuaAPI | |||||||
|             } |             } | ||||||
|             case 11: |             case 11: | ||||||
|             { |             { | ||||||
|                 // m_time |                 // time | ||||||
|                 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 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) { |                     synchronized (m_alarms) { | ||||||
|                         return new Object[]{m_time}; |                         return new Object[]{m_time}; | ||||||
|                     } |                     } | ||||||
|                 } |  | ||||||
|                 else if (args.length > 0 && args[0] != null && args[0] instanceof String) { |                 } else { | ||||||
|                     String param = (String) args[0]; |                     throw new LuaException("Unsupported operation"); | ||||||
|                     //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: |             case 12: | ||||||
|             { |             { | ||||||
|                 // day |                 // 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) { |                     synchronized (m_alarms) { | ||||||
|                         return new Object[]{m_day}; |                         return new Object[]{m_day}; | ||||||
|                     } |                     } | ||||||
|                 } |  | ||||||
|                 else if (args.length > 0 && args[0] != null && args[0] instanceof String) { |                 } else { | ||||||
|                     String param = (String) args[0]; |                     throw new LuaException("Unsupported operation"); | ||||||
|                     //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: |             case 13: | ||||||
| @@ -399,6 +410,38 @@ public class OSAPI implements ILuaAPI | |||||||
|                 } |                 } | ||||||
|                 return null; |                 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: |             default: | ||||||
|             { |             { | ||||||
|                 return null; |                 return null; | ||||||
|   | |||||||
| @@ -2,10 +2,10 @@ New Features in ComputerCraft 1.80: | |||||||
|  |  | ||||||
| * Added .getResponseHeaders() to HTTP responses. | * Added .getResponseHeaders() to HTTP responses. | ||||||
| * Return a HTTP response when a HTTP error occurs. | * Return a HTTP response when a HTTP error occurs. | ||||||
| * Added a GUI to change ComputerCraft config options | * Added a GUI to change ComputerCraft config options. | ||||||
| * os.time( ... ) now accept parameters to get real timestamp and real hour of day | * os.time() and os.day() now accept parameters to give the real world time. | ||||||
| * os.day ( ... ) now accept parameters to get real day since 1970-01-01 | * Added os.epoch() | ||||||
| * Monitor text now glows in the dark | * Monitor text now glows in the dark. | ||||||
|  |  | ||||||
| New Features in ComputerCraft 1.79: | New Features in ComputerCraft 1.79: | ||||||
|  |  | ||||||
|   | |||||||
| @@ -14,6 +14,7 @@ os.cancelTimer( token ) | |||||||
| os.sleep( timeout ) | os.sleep( timeout ) | ||||||
| os.time( [source] ) | os.time( [source] ) | ||||||
| os.day( [source] ) | os.day( [source] ) | ||||||
|  | os.epoch( [source] ) | ||||||
| os.setAlarm( time ) | os.setAlarm( time ) | ||||||
| os.cancelAlarm( token ) | os.cancelAlarm( token ) | ||||||
| os.shutdown() | os.shutdown() | ||||||
|   | |||||||
| @@ -2,9 +2,9 @@ New Features in ComputerCraft 1.80: | |||||||
|  |  | ||||||
| * Added .getResponseHeaders() to HTTP responses. | * Added .getResponseHeaders() to HTTP responses. | ||||||
| * Return a HTTP response when a HTTP error occurs. | * Return a HTTP response when a HTTP error occurs. | ||||||
| * Added a GUI to change ComputerCraft config options | * Added a GUI to change ComputerCraft config options. | ||||||
| * os.time( ... ) now accept parameters to get real timestamp and real hour of day | * os.time() and os.day() now accept parameters to give the real world time. | ||||||
| * os.day ( ... ) now accept parameters to get real day since 1970-01-01 | * Added os.epoch() | ||||||
| * Monitor text now glows in the dark | * Monitor text now glows in the dark. | ||||||
|  |  | ||||||
| Type "help changelog" to see the full version history. | Type "help changelog" to see the full version history. | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Daniel Ratcliffe
					Daniel Ratcliffe