From 29a81d53d65aaf33feac2badcdff8caeea01236b Mon Sep 17 00:00:00 2001 From: gnembon <41132274+gnembon@users.noreply.github.com> Date: Wed, 17 Jun 2020 11:10:59 -0300 Subject: [PATCH] unix_time and convert_date (#332) * very simple stuff * added unix_date and imporved unix_time * Update docs/scarpet/api/Auxiliary.md Co-authored-by: gnembon <41132274+gnembon@users.noreply.github.com> * Update Full.md * Dont 100% work, but It'll do untl I fix tomorrow * works, I think * changeing weekdays * removing weird stuff I didnt mean to do * previous commits * moving to expression.java because prof.gnembon said * moving away from LazyFUnctions cos gnembon doesn't like evaluating somewhere * updating docs * tweaks to dates Co-authored-by: Ghoulboy78 --- docs/scarpet/Full.md | 46 ++++++++++++++ docs/scarpet/language/SystemFunctions.md | 46 ++++++++++++++ .../java/carpet/script/CarpetExpression.java | 1 + src/main/java/carpet/script/Expression.java | 62 +++++++++++++++++++ .../java/carpet/script/value/ListValue.java | 7 +++ 5 files changed, 162 insertions(+) diff --git a/docs/scarpet/Full.md b/docs/scarpet/Full.md index 7142f19d..f82c80a7 100644 --- a/docs/scarpet/Full.md +++ b/docs/scarpet/Full.md @@ -921,6 +921,52 @@ flip_my_world_upside_down(); print(str('this took %d milliseconds',time()-start_time)) +### `unix_time()` + +Returns standard POSIX time as a number of milliseconds since the start of the epoch +(00:00 am and 0 seconds, 1 Jan 1970). +Unlike the previous function, this can be used to get exact time, but it varies from time zone to time zone. + +### `convert_date(milliseconds)` +### `convert_date(year, month, date, hours?, mins?, secs?)` +### `convert_date(l(year, month, date, hours?, mins?, secs?))` + +If called with a single argument, converts standard POSIX time to a list in the format: + +`l(year, month, date, hours, mins, secs, day_of_week, day_of_year, week_of_year)` + +eg: `convert_date(1592401346960) -> [2020, 6, 17, 10, 42, 26, 3, 169, 25]` + +Where the `6` stands for June, but `17` stands for 17th, `10` stands for 10am, +`42` stands for 42 minutes past the hour, and `26` stands for 26 seconds past the minute, +and `3` stands for Wednesday, `169` is the day of year, and `25` is a week of year. + +Run `convert_date(unix_time())` to get current time as list. + + +When called with a list, or with 3 or 6 arguments, returns standard POSIX time as a number of milliseconds since the + start of the epoch (1 Jan 1970), +using the time inputted into the function as opposed to the system time. + +Example editing: +
+date = convert_date(unix_time());
+
+months = l('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
+
+days = l('Mon','Tue','Wed','Thu','Fri','Sat','Sun');
+
+print(
+  str('Its %s, %d %s %d, %02d:%02d:%02d', 
+    days:(date:6-1), date:2, months:(date:1-1), date:0, date:3, date:4, date:5 
+  )
+)  
+
+ +This will give you a date: + +It is currently `hrs`:`mins` and `secs` seconds on the `date`th of `month`, `year` + ### `profile_expr(expression)` Returns number of times given expression can be run in 50ms time. Useful to profile and optimize your code. diff --git a/docs/scarpet/language/SystemFunctions.md b/docs/scarpet/language/SystemFunctions.md index 06e5ccdb..0e0eefc8 100644 --- a/docs/scarpet/language/SystemFunctions.md +++ b/docs/scarpet/language/SystemFunctions.md @@ -278,6 +278,52 @@ flip_my_world_upside_down(); print(str('this took %d milliseconds',time()-start_time)) +### `unix_time()` + +Returns standard POSIX time as a number of milliseconds since the start of the epoch +(00:00 am and 0 seconds, 1 Jan 1970). +Unlike the previous function, this can be used to get exact time, but it varies from time zone to time zone. + +### `convert_date(milliseconds)` +### `convert_date(year, month, date, hours?, mins?, secs?)` +### `convert_date(l(year, month, date, hours?, mins?, secs?))` + +If called with a single argument, converts standard POSIX time to a list in the format: + +`l(year, month, date, hours, mins, secs, day_of_week, day_of_year, week_of_year)` + +eg: `convert_date(1592401346960) -> [2020, 6, 17, 10, 42, 26, 3, 169, 25]` + +Where the `6` stands for June, but `17` stands for 17th, `10` stands for 10am, +`42` stands for 42 minutes past the hour, and `26` stands for 26 seconds past the minute, +and `3` stands for Wednesday, `169` is the day of year, and `25` is a week of year. + +Run `convert_date(unix_time())` to get current time as list. + + +When called with a list, or with 3 or 6 arguments, returns standard POSIX time as a number of milliseconds since the + start of the epoch (1 Jan 1970), +using the time inputted into the function as opposed to the system time. + +Example editing: +
+date = convert_date(unix_time());
+
+months = l('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
+
+days = l('Mon','Tue','Wed','Thu','Fri','Sat','Sun');
+
+print(
+  str('Its %s, %d %s %d, %02d:%02d:%02d', 
+    days:(date:6-1), date:2, months:(date:1-1), date:0, date:3, date:4, date:5 
+  )
+)  
+
+ +This will give you a date: + +It is currently `hrs`:`mins` and `secs` seconds on the `date`th of `month`, `year` + ### `profile_expr(expression)` Returns number of times given expression can be run in 50ms time. Useful to profile and optimize your code. diff --git a/src/main/java/carpet/script/CarpetExpression.java b/src/main/java/carpet/script/CarpetExpression.java index 0493460b..efec613d 100644 --- a/src/main/java/carpet/script/CarpetExpression.java +++ b/src/main/java/carpet/script/CarpetExpression.java @@ -138,6 +138,7 @@ import net.minecraft.world.dimension.DimensionType; import net.minecraft.world.gen.ChunkRandom; import org.apache.commons.lang3.tuple.Pair; +import java.util.Date; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; diff --git a/src/main/java/carpet/script/Expression.java b/src/main/java/carpet/script/Expression.java index a53f4eb5..2a445ee4 100644 --- a/src/main/java/carpet/script/Expression.java +++ b/src/main/java/carpet/script/Expression.java @@ -43,7 +43,9 @@ import org.apache.commons.lang3.text.WordUtils; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; +import java.util.Calendar; import java.util.Collections; +import java.util.GregorianCalendar; import java.util.HashMap; import java.util.IllegalFormatException; import java.util.Iterator; @@ -1790,6 +1792,66 @@ public class Expression return (cc, tt) -> time; }); + addLazyFunction("unix_time", 0, (c, t, lv) -> + { + Value time = new NumericValue(System.currentTimeMillis()); + return (cc, tt) -> time; + }); + + addFunction("convert_date", lv -> + { + int argsize = lv.size(); + if (lv.size() == 0) throw new InternalExpressionException("'convert_date' requires at least one parameter"); + Value value = lv.get(0); + if (argsize == 1 && !(value instanceof ListValue)) + { + Calendar cal = new GregorianCalendar(Locale.ROOT); + cal.setTimeInMillis(NumericValue.asNumber(value, "timestamp").getLong()); + int weekday = cal.get(Calendar.DAY_OF_WEEK)-1; + if (weekday == 0) weekday = 7; + Value retVal = ListValue.ofNums( + cal.get(Calendar.YEAR), + cal.get(Calendar.MONTH)+1, + cal.get(Calendar.DAY_OF_MONTH), + cal.get(Calendar.HOUR_OF_DAY), + cal.get(Calendar.MINUTE), + cal.get(Calendar.SECOND), + weekday, + cal.get(Calendar.DAY_OF_YEAR), + cal.get(Calendar.WEEK_OF_YEAR) + ); + return retVal; + } + else if(value instanceof ListValue) + { + lv = ((ListValue) value).getItems(); + argsize = lv.size(); + } + Calendar cal = new GregorianCalendar(0, 0, 0, 0, 0, 0); + + if (argsize == 3) + { + cal.set( + NumericValue.asNumber(lv.get(0)).getInt(), + NumericValue.asNumber(lv.get(1)).getInt()-1, + NumericValue.asNumber(lv.get(2)).getInt() + ); + } + else if (argsize == 6) + { + cal.set( + NumericValue.asNumber(lv.get(0)).getInt(), + NumericValue.asNumber(lv.get(1)).getInt()-1, + NumericValue.asNumber(lv.get(2)).getInt(), + NumericValue.asNumber(lv.get(3)).getInt(), + NumericValue.asNumber(lv.get(4)).getInt(), + NumericValue.asNumber(lv.get(5)).getInt() + ); + } + else throw new InternalExpressionException("Date conversion requires 3 arguments for Dates or 6 arguments, for time"); + return new NumericValue(cal.getTimeInMillis()); + }); + addLazyFunction("profile_expr", 1, (c, t, lv) -> { LazyValue lazy = lv.get(0); diff --git a/src/main/java/carpet/script/value/ListValue.java b/src/main/java/carpet/script/value/ListValue.java index e20d32b3..e5307ae1 100644 --- a/src/main/java/carpet/script/value/ListValue.java +++ b/src/main/java/carpet/script/value/ListValue.java @@ -98,6 +98,13 @@ public class ListValue extends AbstractListValue implements ContainerValueInterf { return ListValue.wrap(Arrays.asList(list)); } + public static ListValue ofNums(Number ... list) + { + List valList = new ArrayList<>(); + for (Number i : list) + valList.add(new NumericValue(i.doubleValue())); + return ListValue.wrap(valList); + } public static LazyValue lazyEmpty() {