Search in sources :

Example 1 with Predicate

use of at.ac.tuwien.kr.alpha.api.externals.Predicate in project Alpha by alpha-asp.

the class AspStandardLibrary method datetimeIsEqual.

/**
 * Compares two datetimes and returns true iff the first datetime (dt1) is
 * equal to the second datetime (dt2). Both datetimes are represented as six
 * integers each, referring to years, months, days, hours, minutes and seconds
 * respectively.
 *
 * @param dt1Year   the year field for dt1
 * @param dt1Month  the month field for dt1
 * @param dt1Day    the day field for dt1
 * @param dt1Hour   the hour field for dt1
 * @param dt1Minute the minute field for dt1
 * @param dt1Second the second field for dt1
 * @param dt2Year   the year field for dt2
 * @param dt2Month  the month field for dt2
 * @param dt2Day    the day field for dt2
 * @param dt2Hour   the hour field for dt2
 * @param dt2Minute the minute field for dt2
 * @param dt2Second the second field for dt2
 * @return true if dt1 is equal to dt2, false otherwise
 */
@Predicate(name = "stdlib_datetime_is_equal")
public static boolean datetimeIsEqual(int dt1Year, int dt1Month, int dt1Day, int dt1Hour, int dt1Minute, int dt1Second, int dt2Year, int dt2Month, int dt2Day, int dt2Hour, int dt2Minute, int dt2Second) {
    LocalDateTime dt1 = LocalDateTime.of(dt1Year, dt1Month, dt1Day, dt1Hour, dt1Minute, dt1Second);
    LocalDateTime dt2 = LocalDateTime.of(dt2Year, dt2Month, dt2Day, dt2Hour, dt2Minute, dt2Second);
    return dt1.isEqual(dt2);
}
Also used : LocalDateTime(java.time.LocalDateTime) Predicate(at.ac.tuwien.kr.alpha.api.externals.Predicate)

Example 2 with Predicate

use of at.ac.tuwien.kr.alpha.api.externals.Predicate in project Alpha by alpha-asp.

the class AspStandardLibrary method datetimeToString.

/**
 * Formats a datetime value represented using six integers as a string according
 * to the given pattern. Valid format trings are those accepted by
 * {@link DateTimeFormatter.ofPattern}.
 *
 * @param year
 * @param month
 * @param day
 * @param hours
 * @param minutes
 * @param seconds
 * @param format
 * @return a string representing the given datetime in the format specified by
 *         the format string
 */
@Predicate(name = "stdlib_datetime_to_string")
public static Set<List<ConstantTerm<String>>> datetimeToString(int year, int month, int day, int hours, int minutes, int seconds, String format) {
    LocalDateTime datetime = LocalDateTime.of(year, month, day, hours, minutes, seconds);
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
    return Collections.singleton(Terms.asTermList(formatter.format(datetime)));
}
Also used : LocalDateTime(java.time.LocalDateTime) DateTimeFormatter(java.time.format.DateTimeFormatter) Predicate(at.ac.tuwien.kr.alpha.api.externals.Predicate)

Example 3 with Predicate

use of at.ac.tuwien.kr.alpha.api.externals.Predicate in project Alpha by alpha-asp.

the class AspStandardLibrary method datetimeIsBeforeOrEqual.

/**
 * Compares two datetimes and returns true iff the first datetime (dt1) is
 * before or equal to the second datetime (dt2). Both datetimes are represented
 * as six integers each, referring to years, months, days, hours, minutes and seconds
 * respectively.
 *
 * @param dt1Year   the year field for dt1
 * @param dt1Month  the month field for dt1
 * @param dt1Day    the day field for dt1
 * @param dt1Hour   the hour field for dt1
 * @param dt1Minute the minute field for dt1
 * @param dt1Second the second field for dt1
 * @param dt2Year   the year field for dt2
 * @param dt2Month  the month field for dt2
 * @param dt2Day    the day field for dt2
 * @param dt2Hour   the hour field for dt2
 * @param dt2Minute the minute field for dt2
 * @param dt2Second the second field for dt2
 * @return true if dt1 is before dt2 in time or both dt1 and dt2 denote the same
 *         point in time, false otherwise
 */
@Predicate(name = "stdlib_datetime_is_before_or_equal")
public static boolean datetimeIsBeforeOrEqual(int dt1Year, int dt1Month, int dt1Day, int dt1Hour, int dt1Minute, int dt1Second, int dt2Year, int dt2Month, int dt2Day, int dt2Hour, int dt2Minute, int dt2Second) {
    LocalDateTime dt1 = LocalDateTime.of(dt1Year, dt1Month, dt1Day, dt1Hour, dt1Minute, dt1Second);
    LocalDateTime dt2 = LocalDateTime.of(dt2Year, dt2Month, dt2Day, dt2Hour, dt2Minute, dt2Second);
    return dt1.isBefore(dt2) || dt1.isEqual(dt2);
}
Also used : LocalDateTime(java.time.LocalDateTime) Predicate(at.ac.tuwien.kr.alpha.api.externals.Predicate)

Example 4 with Predicate

use of at.ac.tuwien.kr.alpha.api.externals.Predicate in project Alpha by alpha-asp.

the class AspStandardLibrary method datetimeIsBefore.

/**
 * Compares two datetimes and returns true iff the first datetime (dt1) is
 * before the second datetime (dt2). Both datetimes are represented as six
 * integers each, referring to years, months, days, hours, minutes and seconds
 * respectively.
 *
 * @param dt1Year   the year field for dt1
 * @param dt1Month  the month field for dt1
 * @param dt1Day    the day field for dt1
 * @param dt1Hour   the hour field for dt1
 * @param dt1Minute the minute field for dt1
 * @param dt1Second the second field for dt1
 * @param dt2Year   the year field for dt2
 * @param dt2Month  the month field for dt2
 * @param dt2Day    the day field for dt2
 * @param dt2Hour   the hour field for dt2
 * @param dt2Minute the minute field for dt2
 * @param dt2Second the second field for dt2
 * @return true if dt1 is before dt2 in time, false otherwise
 */
@Predicate(name = "stdlib_datetime_is_before")
public static boolean datetimeIsBefore(int dt1Year, int dt1Month, int dt1Day, int dt1Hour, int dt1Minute, int dt1Second, int dt2Year, int dt2Month, int dt2Day, int dt2Hour, int dt2Minute, int dt2Second) {
    LocalDateTime dt1 = LocalDateTime.of(dt1Year, dt1Month, dt1Day, dt1Hour, dt1Minute, dt1Second);
    LocalDateTime dt2 = LocalDateTime.of(dt2Year, dt2Month, dt2Day, dt2Hour, dt2Minute, dt2Second);
    return dt1.isBefore(dt2);
}
Also used : LocalDateTime(java.time.LocalDateTime) Predicate(at.ac.tuwien.kr.alpha.api.externals.Predicate)

Example 5 with Predicate

use of at.ac.tuwien.kr.alpha.api.externals.Predicate in project Alpha by alpha-asp.

the class AspStandardLibrary method datetimeParse.

/**
 * Parses a string representing a datetime without time-zone and returns the
 * year, month, day, hours, minutes and seconds as separate symbolic integer
 * terms.
 * Example:
 *
 * <pre>
 * 		A valid ground instance of the atom &stdlib_datetime_parse[DTSTR, "dd.mm.yyyy hh:MM:ss"](YEAR, MONTH, DAY, HOUR, MIN, SEC)
 * 		would be: &stdlib_datetime_parse["20.05.2020 01:19:13", "dd.mm.yyyy hh:MM:ss"](2020, 5, 20, 1, 19, 13)
 * </pre>
 *
 * Timezones are not supported by this function. Datetime values are parsed
 * using {@link LocalDateTime.parse}.
 *
 * @param datetime a string representing a datetime without time zone
 *                 information
 * @param format   a format string that is accepted by {@link DateTimeFormatter}
 * @return a 6-value integer tuple of format (YEAR, MONTH, DAY, HOUR, MIN, SEC)
 */
@Predicate(name = "stdlib_datetime_parse")
public static Set<List<ConstantTerm<Integer>>> datetimeParse(String dtstr, String format) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
    LocalDateTime datetime = LocalDateTime.parse(dtstr, formatter);
    List<ConstantTerm<Integer>> terms = Terms.asTermList(datetime.getYear(), datetime.getMonth().getValue(), datetime.getDayOfMonth(), datetime.getHour(), datetime.getMinute(), datetime.getSecond());
    return Collections.singleton(terms);
}
Also used : LocalDateTime(java.time.LocalDateTime) ConstantTerm(at.ac.tuwien.kr.alpha.api.terms.ConstantTerm) DateTimeFormatter(java.time.format.DateTimeFormatter) Predicate(at.ac.tuwien.kr.alpha.api.externals.Predicate)

Aggregations

Predicate (at.ac.tuwien.kr.alpha.api.externals.Predicate)5 LocalDateTime (java.time.LocalDateTime)5 DateTimeFormatter (java.time.format.DateTimeFormatter)2 ConstantTerm (at.ac.tuwien.kr.alpha.api.terms.ConstantTerm)1