Skip to content
Permalink
Browse files

Add Clock MakeDate, MakeTime, MakeInstantFromParts methods

Change-Id: Ibe249c4de10d8d8213c009728771062eabba9630
  • Loading branch information...
xueyuanw authored and ewpatton committed Mar 4, 2019
1 parent 4cd0cb0 commit 294905feb31ce8f2bd29b5950ac2b328646da35a
@@ -2690,6 +2690,10 @@ String newerVersionComponentException(String componentType, int srcCompVersion,
@Description("")
String millisParams();

@DefaultMessage("second")
@Description("")
String secondParams();

@DefaultMessage("functionName")
@Description("")
String functionNameParams();
@@ -3808,6 +3812,22 @@ String newerVersionComponentException(String componentType, int srcCompVersion,
@Description("")
String MakeInstantMethods();

@DefaultMessage("MakeInstantFromParts")
@Description("")
String MakeInstantFromPartsMethods();

@DefaultMessage("MakeDate")
@Description("")
String MakeDateMethods();

@DefaultMessage("MakeTime")
@Description("")
String MakeTimeMethods();

@DefaultMessage("secondParams")
@Description("")
String secondParamsMethods();

@DefaultMessage("MakeInstantFromMillis")
@Description("")
String MakeInstantFromMillisMethods();
@@ -721,12 +721,14 @@ private static int upgradeCheckBoxProperties(Map<String, JSONValue> componentPro

private static int upgradeClockProperties(Map<String, JSONValue> componentProperties,
int srcCompVersion) {
if (srcCompVersion < 3) {
if (srcCompVersion < 4) {
// (2) The FormatDate and FormatDateTime methods were modified to take another parameter of pattern.
// No properties need to be modified to upgrade to version 2.
// (3) Duration Support was added
// No properties need to be added to upgrade to version 3.
srcCompVersion = 3;
// (4) Added MakeDate, MakeTime, MakeInstantFromParts methods
// No properties need to be added to upgrade to version 4.
srcCompVersion = 4;
}
return srcCompVersion;
}
@@ -1407,7 +1407,10 @@ Blockly.Versioning.AllUpgradeMaps =
],

// Duration Support was added.
3: "noUpgrade"
3: "noUpgrade",

// MakeDate, MakeTime, MakeInstantFromParts methods added
4: "noUpgrade"

}, // End Clock upgraders

@@ -442,6 +442,8 @@ private YaVersion() {
// - TEXTING_COMPONENT_VERSION was incremented to 4
// For YOUNG_ANDROID_VERSION 177:
// - NOTIFIER_COMPONENT_VERSION was incremented to 6
// For YOUNG_ANDROID_VERSION 178:
// - CLOCK_COMPONENT_VERSION was incremented to 4

public static final int YOUNG_ANDROID_VERSION = 177;

@@ -668,7 +670,11 @@ private YaVersion() {
// For CLOCK_COMPONENT_VERSION 2:
// - The pattern parameter was added to the FormatDate and FormatDateTime.
// - Add Duration Support
public static final int CLOCK_COMPONENT_VERSION = 3;
// For CLOCK_COMPONENT_VERSION 3:
// - Duration Support was added
// For CLOCK_COMPONENT_VERSION 4:
// - MakeTime, MakeDate, MakeInstantFromParts methods were added
public static final int CLOCK_COMPONENT_VERSION = 4;

// For CONTACTPICKER_COMPONENT_VERSION 2:
// - The Alignment property was renamed to TextAlignment.
@@ -19,8 +19,17 @@
import com.google.appinventor.components.runtime.errors.YailRuntimeError;
import com.google.appinventor.components.runtime.util.Dates;
import com.google.appinventor.components.runtime.util.TimerInternal;
import com.google.appinventor.components.runtime.util.ErrorMessages;


import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import android.app.Dialog;
import android.app.TimePickerDialog;

/**
* Clock provides the phone's clock, a timer, calendar and time calculations.
@@ -207,6 +216,97 @@ public static Calendar MakeInstant(String from) {
}
}

/**
* An instant in time specified by number year, number month, number day
* @param year year integer
* @param month month integer
* @param day day integer
* @return Calendar instant
*/
@SimpleFunction(description = "Allows the user to set the clock to be "
+"a date value.\n" +
"Valid values for the month field are 1-12 and 1-31 for the day field.\n")
public Calendar MakeDate(int year, int month, int day) {
int jMonth = month - 1;
try {
GregorianCalendar cal = new GregorianCalendar(year, jMonth, day);
cal.setLenient(false);

// A non-lenient GregorianCalendar throws an exception upon
// calculating its time or calendar field values if any out-of-range field value has been set.
cal.getTime();
} catch (IllegalArgumentException e) {
form.dispatchErrorOccurredEvent(this, "MakeDate", ErrorMessages.ERROR_ILLEGAL_DATE);
}

Calendar instant = Dates.DateInstant(year, month, day);
return instant;
}

/**
* An instant in time specified by integer hour, integer minute, and integer second
* @param hour hour integer
* @param minute minute integer
* @param second second integer
* @return Calendar instant since 1/1/1970
*/
@SimpleFunction(description = "Allows the user to set the time of the clock - " +
"Valid format is hh:mm:ss\n")
public Calendar MakeTime(int hour, int minute, int second) {
Calendar instant = new GregorianCalendar();
try {
instant.set(Calendar.HOUR_OF_DAY, hour);
instant.set(Calendar.MINUTE, minute);
instant.set(Calendar.SECOND, second);
} catch (IllegalArgumentException e) {
form.dispatchErrorOccurredEvent(this, "MakeTime", ErrorMessages.ERROR_ILLEGAL_DATE);
}
return instant;
}

/**
* An instant in time specified by number year, number month, number day, number hour,
* number minute, number second
* @param year year integer
* @param month month integer
* @param day day integer
* @param hour hour integer
* @param minute minute integer
* @param second minute integer
* @return Calendar instant
*/
@SimpleFunction(
description = "Allows the user to set the date and time to be displayed when the clock opens.\n" +
"Valid values for the month field are 1-12 and 1-31 for the day field.\n")
public Calendar MakeInstantFromParts(int year, int month, int day, int hour, int minute, int second) {
int jMonth = month - 1;
Calendar instant = null;
try {
instant = new GregorianCalendar(year, jMonth, day);
instant.setLenient(false);

// A non-lenient GregorianCalendar throws an exception upon
// calculating its time or calendar field values if any out-of-range field value has been set.
instant.getTime();
} catch (IllegalArgumentException e) {
form.dispatchErrorOccurredEvent(this, "MakeInstantFromParts", ErrorMessages.ERROR_ILLEGAL_DATE);
}

instant = Dates.DateInstant(year, month, day);

try {
instant.set(Calendar.HOUR_OF_DAY, hour);
instant.set(Calendar.MINUTE, minute);
instant.set(Calendar.SECOND, second);

} catch (IllegalArgumentException e) {
form.dispatchErrorOccurredEvent(this, "MakeInstantFromParts", ErrorMessages.ERROR_ILLEGAL_DATE);
}

return instant;

}

/**
* Create an Calendar from ms since 1/1/1970 00:00:00.0000
* Probably should go in Calendar.
@@ -612,7 +612,13 @@ <h3> Methods </h3>
<dt> <code> number Hour(InstantInTime instant) </code> </dt>
<dd> Returns the hour of the day(0-23) from the instant </dd>
<dt> <code> InstantInTime MakeInstant(text from) </code> </dt>
<dd> Returns an instant specified by MM/dd/YYYY hh:mm:ss or MM/dd/YYYY or hh:mm. An example text input is "06/22/2015 12:18"</dd>
<dd> Returns an instant specified by MM/dd/YYYY hh:mm:ss or MM/dd/YYYY or hh:mm. An example text input is "06/22/2015 12:18" </dd>
<dt> <code> InstantInTime MakeDate(number year, number month, number day) </code> </dt>
<dd> Returns an instant in time specified by year, month, date in UTC. </dd>
<dt> <code> InstantInTime MakeTime(number hour, number minute, number second) </code> </dt>
<dd> Returns an instant in time specified by hour, minute, second in UTC. </dd>
<dt> <code> InstantInTime MakeInstantFromParts(number year, number month, number day, number hour, number minute, number second) </code> </dt>
<dd> Returns an instant in time specified by year, month, date, hour, minute, second in UTC. </dd>
<dt> <code> InstantInTime MakeInstantFromMillis(number millis) </code> </dt>
<dd> Returns an instant in time specified by the milliseconds since 1970 in UTC. </dd>
<dt> <code> number Minute(InstantInTime instant) </code> </dt>

0 comments on commit 294905f

Please sign in to comment.
You can’t perform that action at this time.