Unverified Commit edaebb9e authored by Farzeen Harunani's avatar Farzeen Harunani Committed by Jeffrey I. Schiller

Added more flexibility to DateValue and FormatDateTime

Fixes #1204. Also fix some typos in the documentation.

Change-Id: I7811a2ed368478e3306c9f54ee347f7adfd33fea
parent eda075ad
...@@ -1111,7 +1111,7 @@ public interface OdeMessages extends Messages { ...@@ -1111,7 +1111,7 @@ public interface OdeMessages extends Messages {
String horizontalAlignmentChoiceLeft(); String horizontalAlignmentChoiceLeft();
@DefaultMessage("Right") @DefaultMessage("Right")
@Description("Text for horizontal alignemt choice 'Right'") @Description("Text for horizontal alignment choice 'Right'")
String horizontalAlignmentChoiceRight(); String horizontalAlignmentChoiceRight();
@DefaultMessage("Center") @DefaultMessage("Center")
...@@ -5841,9 +5841,9 @@ public interface OdeMessages extends Messages { ...@@ -5841,9 +5841,9 @@ public interface OdeMessages extends Messages {
+ "regularly set intervals and perform time calculations, " + "regularly set intervals and perform time calculations, "
+ "manipulations, and conversions.</p> <p>Methods to convert an " + "manipulations, and conversions.</p> <p>Methods to convert an "
+ "instant to text are also available. Acceptable patterns are " + "instant to text are also available. Acceptable patterns are "
+ "empty string, MM/DD/YYYY HH:mm:ss a, or MMM d, yyyy " + "empty string, MM/dd/YYYY hh:mm:ss a, or MMM d, yyyy "
+ "HH:mm. The empty string will provide the default format, " + "HH:mm. The empty string will provide the default format, "
+ "which is \"MMM d, yyyy HH:mm:ss a\" for FormatDateTime \"MMM " + "which is \"MMM d, yyyy hh:mm:ss a\" for FormatDateTime \"MMM "
+ "d, yyyy\" for FormatDate. To see all possible format, " + "d, yyyy\" for FormatDate. To see all possible format, "
+ "please see <a " + "please see <a "
+ "href=\"https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html\" " + "href=\"https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html\" "
...@@ -6247,7 +6247,7 @@ public interface OdeMessages extends Messages { ...@@ -6247,7 +6247,7 @@ public interface OdeMessages extends Messages {
@DefaultMessage("<p>It appears that <b>" + "%1" + @DefaultMessage("<p>It appears that <b>" + "%1" +
"</b> has had all blocks removed.</p><p>" + "</b> has had all blocks removed.</p><p>" +
"<ul><li>You can save the enpty screen, and then all those blocks will be " + "<ul><li>You can save the empty screen, and then all those blocks will be " +
"permanently gone from the project.</li>" + "permanently gone from the project.</li>" +
"<li>Alternatively, you can restore the previously saved version " + "<li>Alternatively, you can restore the previously saved version " +
"of the project.</li></ul></p>") "of the project.</li></ul></p>")
......
...@@ -196,13 +196,13 @@ public final class Clock extends AndroidNonvisibleComponent ...@@ -196,13 +196,13 @@ public final class Clock extends AndroidNonvisibleComponent
* @return date * @return date
*/ */
@SimpleFunction( @SimpleFunction(
description = "An instant in time specified by MM/DD/YYYY hh:mm:ss or MM/DD/YYYY or hh:mm") description = "An instant in time specified by MM/dd/YYYY hh:mm:ss or MM/dd/YYYY or hh:mm")
public static Calendar MakeInstant(String from) { public static Calendar MakeInstant(String from) {
try { try {
return Dates.DateValue(from); return Dates.DateValue(from);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
throw new YailRuntimeError( throw new YailRuntimeError(
"Argument to MakeInstant should have form MM/DD/YYYY hh:mm:ss, or MM/DD/YYYY or hh:mm", "Argument to MakeInstant should have form MM/dd/YYYY hh:mm:ss, or MM/dd/YYYY or hh:mm",
"Sorry to be so picky."); "Sorry to be so picky.");
} }
} }
...@@ -459,7 +459,7 @@ public final class Clock extends AndroidNonvisibleComponent ...@@ -459,7 +459,7 @@ public final class Clock extends AndroidNonvisibleComponent
* Converts and formats an instant into a string of date and time with the specified pattern. * * Converts and formats an instant into a string of date and time with the specified pattern. *
* *
* @param instant instant to format * @param instant instant to format
* @param pattern format of the date and time e.g. MM/DD/YYYY HH:mm:ss a, MMM d, yyyy HH:mm * @param pattern format of the date and time e.g. MM/dd/YYYY hh:mm:ss a, MMM d, yyyy HH:mm
* @return formatted instant * @return formatted instant
*/ */
@SimpleFunction (description = "Text representing the date and time of an" @SimpleFunction (description = "Text representing the date and time of an"
...@@ -469,7 +469,7 @@ public final class Clock extends AndroidNonvisibleComponent ...@@ -469,7 +469,7 @@ public final class Clock extends AndroidNonvisibleComponent
return Dates.FormatDateTime(instant, pattern); return Dates.FormatDateTime(instant, pattern);
} catch (IllegalArgumentException e){ } catch (IllegalArgumentException e){
throw new YailRuntimeError( throw new YailRuntimeError(
"Illegal argument for pattern in Clock.FormatDateTime. Acceptable values are empty string, MM/DD/YYYY HH:mm:ss a, MMM d, yyyy HH:mm " "Illegal argument for pattern in Clock.FormatDateTime. Acceptable values are empty string, MM/dd/YYYY hh:mm:ss a, MMM d, yyyy HH:mm "
+ "For all possible patterns, see https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html", + "For all possible patterns, see https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html",
"Sorry to be so picky."); "Sorry to be so picky.");
} }
......
...@@ -9,6 +9,7 @@ package com.google.appinventor.components.runtime.util; ...@@ -9,6 +9,7 @@ package com.google.appinventor.components.runtime.util;
import com.google.appinventor.components.annotations.SimpleFunction; import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleObject; import com.google.appinventor.components.annotations.SimpleObject;
import java.util.Date;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
...@@ -126,26 +127,28 @@ public final class Dates { ...@@ -126,26 +127,28 @@ public final class Dates {
@SimpleFunction @SimpleFunction
public static Calendar DateValue(String value) { public static Calendar DateValue(String value) {
Calendar date = new GregorianCalendar(); Calendar date = new GregorianCalendar();
try { date.setTime(tryParseDate(value));
DateFormat dateTimeFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); return date;
dateTimeFormat.setLenient(true); }
date.setTime(dateTimeFormat.parse(value));
} catch (ParseException e) { private static Date tryParseDate(String value) {
String[] formats = {
"MM/dd/yyyy hh:mm:ss a",
"MM/dd/yyyy HH:mm:ss",
"MM/dd/yyyy hh:mm a",
"MM/dd/yyyy HH:mm",
"MM/dd/yyyy",
"hh:mm:ss a",
"HH:mm:ss",
"hh:mm a",
"HH:mm"
};
for (String format : formats) {
try { try {
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy"); return new SimpleDateFormat(format).parse(value);
dateFormat.setLenient(true); } catch (ParseException e) {}
date.setTime(dateFormat.parse(value));
} catch (ParseException e1) {
try {
DateFormat dateFormat = new SimpleDateFormat("HH:mm");
dateFormat.setLenient(true);
date.setTime(dateFormat.parse(value));
} catch (ParseException pe) {
throw new IllegalArgumentException("illegal date/time format in function DateValue()");
}
}
} }
return date; throw new IllegalArgumentException("illegal date/time format in function DateValue()");
} }
/** /**
...@@ -191,14 +194,14 @@ public final class Dates { ...@@ -191,14 +194,14 @@ public final class Dates {
* @see SimpleDateFormat * @see SimpleDateFormat
* *
* @param date date to format * @param date date to format
* @param pattern format of the date and time e.g. MM/DD/YYYY HH:mm:ss a, MMM d, yyyy HH:mm * @param pattern format of the date and time e.g. MM/dd/YYYY hh:mm:ss a, MMM d, yyyy HH:mm
* @return formatted date * @return formatted date
*/ */
@SimpleFunction @SimpleFunction
public static String FormatDateTime(Calendar date, String pattern) { public static String FormatDateTime(Calendar date, String pattern) {
SimpleDateFormat formatdate = new SimpleDateFormat(); SimpleDateFormat formatdate = new SimpleDateFormat();
if (pattern.length() == 0) { if (pattern.length() == 0) {
formatdate.applyPattern("MMM d, yyyy HH:mm:ss a"); formatdate.applyPattern("MMM d, yyyy hh:mm:ss a");
} else { } else {
formatdate.applyPattern(pattern); formatdate.applyPattern(pattern);
} }
......
...@@ -539,7 +539,7 @@ none ...@@ -539,7 +539,7 @@ none
<h2 id="Clock"> Clock </h2> <h2 id="Clock"> Clock </h2>
<img src="images/clock.png" alt="" /> <img src="images/clock.png" alt="" />
<p>Non-visible component that provides the instant in time using the internal clock on the phone.It can fire a timer at regularly set intervals and perform time calculations, manipulations, and conversions.</p> <p>Non-visible component that provides the instant in time using the internal clock on the phone.It can fire a timer at regularly set intervals and perform time calculations, manipulations, and conversions.</p>
<p>Methods to convert an instant to text are also available. Acceptable patterns are empty string, MM/DD/YYYY HH:mm:ss a, or MMM d, yyyy HH:mm. The empty string will provide the default format, which is "MMM d, yyyy HH:mm:ss a" for FormatDateTime, "MMM d, yyyy" for FormatDate. To see all possible format, please see <a href="https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html">here</a>. </p> <p>Methods to convert an instant to text are also available. Acceptable patterns are empty string, MM/dd/YYYY HH:mm:ss a, or MMM d, yyyy HH:mm. The empty string will provide the default format, which is "MMM d, yyyy HH:mm:ss a" for FormatDateTime, "MMM d, yyyy" for FormatDate. To see all possible format, please see <a href="https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html">here</a>. </p>
<p> Date and Time are formatted with InstantInTime and Duration. </p> <p> Date and Time are formatted with InstantInTime and Duration. </p>
<ul> <ul>
<li> <strong> Instant </strong> : consists of Year, Month, DayOfMonth, Hour, Minute, and Second. <li> <strong> Instant </strong> : consists of Year, Month, DayOfMonth, Hour, Minute, and Second.
...@@ -604,7 +604,7 @@ none ...@@ -604,7 +604,7 @@ none
<dt> <code> number Hour(InstantInTime instant) </code> </dt> <dt> <code> number Hour(InstantInTime instant) </code> </dt>
<dd> Returns the hour of the day(0-23) from the instant </dd> <dd> Returns the hour of the day(0-23) from the instant </dd>
<dt> <code> InstantInTime MakeInstant(text from) </code> </dt> <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 MakeInstantFromMillis(number millis) </code> </dt> <dt> <code> InstantInTime MakeInstantFromMillis(number millis) </code> </dt>
<dd> Returns an instant in time specified by the milliseconds since 1970. </dd> <dd> Returns an instant in time specified by the milliseconds since 1970. </dd>
<dt> <code> number Minute(InstantInTime instant) </code> </dt> <dt> <code> number Minute(InstantInTime instant) </code> </dt>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment