7 package com.google.appinventor.components.runtime;
25 import java.util.Calendar;
26 import java.util.GregorianCalendar;
63 @DesignerComponent(version = YaVersion.CLOCK_COMPONENT_VERSION,
64 description =
"<p>Non-visible component that provides the instant in time using the internal clock on th"
65 +
"e phone. It can fire a timer at regularly set intervals and perform time calculations, manipulations, and conversions.</p> "
66 +
"<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"
67 +
"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. "
68 +
"To see all possible format, please see <a href=\"https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html\" _target=\"_blank\">here</a>. </p> ",
69 category = ComponentCategory.SENSORS,
71 iconName =
"images/clock.png")
76 private static final int DEFAULT_INTERVAL = 1000;
77 private static final boolean DEFAULT_ENABLED =
true;
80 private boolean timerAlwaysFires =
true;
81 private boolean onScreen =
false;
89 super(container.
$form());
90 timerInternal =
new TimerInternal(
this, DEFAULT_ENABLED, DEFAULT_INTERVAL);
116 description =
"The Timer event runs when the timer has gone off.")
118 if (timerAlwaysFires || onScreen) {
130 description =
"Interval between timer events in ms")
145 defaultValue = DEFAULT_INTERVAL +
"")
159 description =
"Fires timer if true")
161 return timerInternal.
Enabled();
171 defaultValue = DEFAULT_ENABLED ?
"True" :
"False")
174 timerInternal.
Enabled(enabled);
186 description =
"Will fire even when application is not showing on the "
189 return timerAlwaysFires;
202 timerAlwaysFires = always;
218 @
SimpleFunction (description =
"Returns the phone's internal time.")
223 @
SimpleFunction(description =
"Returns the current instant in time read from "
225 public static Calendar
Now() {
240 description =
"Returns an instant in time specified by MM/dd/YYYY hh:mm:ss or "
241 +
"MM/dd/YYYY or hh:mm.")
245 }
catch (IllegalArgumentException e) {
247 "Argument to MakeInstant should have form MM/dd/YYYY hh:mm:ss, or MM/dd/YYYY or hh:mm",
248 "Sorry to be so picky.");
260 @
SimpleFunction(description =
"Returns an instant in time specified by year, month, date in "
261 +
"UTC.\nValid values for the month field are 1-12 and 1-31 for the day field.")
262 public Calendar
MakeDate(
int year,
int month,
int day) {
263 int jMonth = month - 1;
265 GregorianCalendar cal =
new GregorianCalendar(year, jMonth, day);
266 cal.setLenient(
false);
271 }
catch (IllegalArgumentException e) {
287 @
SimpleFunction(description =
"Returns an instant in time specified by hour, minute, second in "
289 public Calendar
MakeTime(
int hour,
int minute,
int second) {
290 Calendar instant =
new GregorianCalendar();
292 instant.set(Calendar.HOUR_OF_DAY, hour);
293 instant.set(Calendar.MINUTE, minute);
294 instant.set(Calendar.SECOND, second);
295 }
catch (IllegalArgumentException e) {
314 description =
"Returns an instant in time specified by year, month, date, hour, minute, "
317 int jMonth = month - 1;
318 Calendar instant =
null;
320 instant =
new GregorianCalendar(year, jMonth, day);
321 instant.setLenient(
false);
326 }
catch (IllegalArgumentException e) {
333 instant.set(Calendar.HOUR_OF_DAY, hour);
334 instant.set(Calendar.MINUTE, minute);
335 instant.set(Calendar.SECOND, second);
337 }
catch (IllegalArgumentException e) {
352 @
SimpleFunction(description =
"Returns an instant in time specified by the milliseconds since "
356 instant.setTimeInMillis(millis);
367 @
SimpleFunction (description =
"Returns the instant in time measured as milliseconds since 1970.")
369 return instant.getTimeInMillis();
372 @
SimpleFunction(description =
"Returns an instant in time some duration after the argument")
373 public static Calendar
AddDuration(Calendar instant,
long quantity) {
374 Calendar newInstant = (Calendar) instant.clone();
379 @
SimpleFunction(description =
"Returns an instant in time some seconds after the given instant.")
380 public static Calendar
AddSeconds(Calendar instant,
int quantity) {
381 Calendar newInstant = (Calendar) instant.clone();
386 @
SimpleFunction(description =
"Returns an instant in time some minutes after the given instant.")
387 public static Calendar
AddMinutes(Calendar instant,
int quantity) {
388 Calendar newInstant = (Calendar) instant.clone();
393 @
SimpleFunction(description =
"Returns an instant in time some hours after the given instant.")
394 public static Calendar
AddHours(Calendar instant,
int quantity) {
395 Calendar newInstant = (Calendar) instant.clone();
396 Dates.
DateAdd(newInstant, Calendar.HOUR_OF_DAY, quantity);
400 @
SimpleFunction(description =
"Returns an instant in time some days after the given instant.")
401 public static Calendar
AddDays(Calendar instant,
int quantity) {
402 Calendar newInstant = (Calendar) instant.clone();
407 @
SimpleFunction(description =
"Returns An instant in time some weeks after the given instant.")
408 public static Calendar
AddWeeks(Calendar instant,
int quantity) {
409 Calendar newInstant = (Calendar) instant.clone();
410 Dates.
DateAdd(newInstant, Calendar.WEEK_OF_YEAR, quantity);
414 @
SimpleFunction(description =
"Returns an instant in time some months after the given instant.")
415 public static Calendar
AddMonths(Calendar instant,
int quantity) {
416 Calendar newInstant = (Calendar) instant.clone();
421 @
SimpleFunction(description =
"Returns an instant in time some years after the given instant.")
422 public static Calendar
AddYears(Calendar instant,
int quantity) {
423 Calendar newInstant = (Calendar) instant.clone();
435 @
SimpleFunction (description =
"Returns duration, which is milliseconds elapsed between "
437 public static
long Duration(Calendar start, Calendar end) {
438 return end.getTimeInMillis() - start.getTimeInMillis();
447 @
SimpleFunction (description =
"Converts the duration to the number of seconds.")
458 @
SimpleFunction (description =
"Converts the duration to the number of minutes.")
469 @
SimpleFunction (description =
"Converts the duration to the number of hours.")
480 @
SimpleFunction (description =
"Converts the duration to the number of days.")
491 @
SimpleFunction (description =
"Converts the duration to the number of weeks.")
502 @
SimpleFunction (description =
"Returns the second of the minute (0-59) from the instant.")
503 public static
int Second(Calendar instant) {
513 @
SimpleFunction(description =
"Returns the minute of the hour (0-59) from the instant.")
514 public static
int Minute(Calendar instant) {
524 @
SimpleFunction (description =
"Returns the hour of the day (0-23) from the instant.")
525 public static
int Hour(Calendar instant) {
535 @
SimpleFunction (description =
"Returns the day of the month (1-31) from the instant.")
546 @
SimpleFunction (description =
"Returns the day of the week represented as a "
547 +
"number from 1 (Sunday) to 7 (Saturday).")
558 @
SimpleFunction (description =
"Returns the name of the day of the week from the instant.")
569 @
SimpleFunction (description =
"Returns the month of the year represented as a "
570 +
"number from 1 to 12).")
571 public static
int Month(Calendar instant) {
581 @
SimpleFunction (description =
"Returns the name of the month from the instant, e.g., January, "
582 +
"February, March...")
594 public static
int Year(Calendar instant) {
607 @
SimpleFunction (description =
"Returns text representing the date and time of an"
608 +
" instant in the specified pattern")
612 }
catch (IllegalArgumentException e){
614 "Illegal argument for pattern in Clock.FormatDateTime. Acceptable values are empty string, MM/dd/YYYY hh:mm:ss a, MMM d, yyyy HH:mm "
615 +
"For all possible patterns, see https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html",
616 "Sorry to be so picky.");
629 @
SimpleFunction (description =
"Text representing the date of an instant in the specified pattern")
630 public static String
FormatDate(Calendar instant, String pattern) {
633 }
catch (IllegalArgumentException e){
635 "Illegal argument for pattern in Clock.FormatDate. Acceptable values are empty string, MM/dd/YYYY, or MMM d, yyyy. "
636 +
"For all possible patterns, see https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html"
637 ,
"Sorry to be so picky.");
649 @
SimpleFunction (description =
"Text representing the time of an instant")