Search in sources :

Example 21 with PeriodFormatter

use of org.joda.time.format.PeriodFormatter in project FlareBot by FlareBot.

the class UpdateCommand method onCommand.

@Override
public void onCommand(User sender, GuildWrapper guild, TextChannel channel, Message message, String[] args, Member member) {
    if (PerGuildPermissions.isCreator(sender)) {
        if (args.length == 0) {
            update(false, channel);
        } else if (args.length == 1) {
            if (args[0].equalsIgnoreCase("force")) {
                update(true, channel);
            } else if (args[0].equalsIgnoreCase("no-active-channels")) {
                channel.sendMessage("I will now update to the latest version when no channels are playing music!").queue();
                if (Getters.getConnectedVoiceChannels() == 0) {
                    update(true, channel);
                } else {
                    if (!queued.getAndSet(true)) {
                        FlareBot.NOVOICE_UPDATING.set(true);
                    } else
                        channel.sendMessage("There is already an update queued!").queue();
                }
            } else if (args[0].equalsIgnoreCase("schedule")) {
                if (!queued.getAndSet(true)) {
                    FlareBot.instance().scheduleUpdate();
                    MessageUtils.sendSuccessMessage("Update scheduled for 12PM GMT!", channel);
                } else {
                    MessageUtils.sendErrorMessage("There is already an update queued!", channel);
                }
            } else if (args[0].equalsIgnoreCase("cancel")) {
                if (!queued.getAndSet(true)) {
                    MessageUtils.sendErrorMessage("There is no update queued!", channel);
                } else {
                    if (Scheduler.cancelTask("Scheduled-Update")) {
                        MessageUtils.sendSuccessMessage("Cancelled Update!", channel);
                    } else {
                        MessageUtils.sendErrorMessage("Could not cancel update!", channel);
                    }
                }
            } else {
                if (!queued.getAndSet(true)) {
                    Period p;
                    try {
                        PeriodFormatter formatter = new PeriodFormatterBuilder().appendDays().appendSuffix("d").appendHours().appendSuffix("h").appendMinutes().appendSuffix("m").appendSeconds().appendSuffix("s").toFormatter();
                        p = formatter.parsePeriod(args[0]);
                        new FlareBotTask("Scheduled-Update") {

                            @Override
                            public void run() {
                                update(true, channel);
                            }
                        }.delay(TimeUnit.SECONDS.toMillis(p.toStandardSeconds().getSeconds()));
                    } catch (IllegalArgumentException e) {
                        channel.sendMessage("That is an invalid time option!").queue();
                        return;
                    }
                    channel.sendMessage("I will now update to the latest version in " + p.toStandardSeconds().getSeconds() + " seconds.").queue();
                } else {
                    channel.sendMessage("There is already an update queued!").queue();
                }
            }
        }
    }
}
Also used : PeriodFormatterBuilder(org.joda.time.format.PeriodFormatterBuilder) PeriodFormatter(org.joda.time.format.PeriodFormatter) Period(org.joda.time.Period) FlareBotTask(stream.flarebot.flarebot.scheduler.FlareBotTask)

Example 22 with PeriodFormatter

use of org.joda.time.format.PeriodFormatter in project FlareBot by FlareBot.

the class GeneralUtils method parseTime.

/**
 * Parses time from string.
 * The input can be in these formats
 * s
 * m:s
 * h:m:s
 * XhXmXs
 * Examples:
 * 5 - Would be 5 seconds
 * 5s - Would also be 5 seconds
 * 1:10 - Would be 1 minute and 10 seconds
 * 1m10s - Would also be 1 minute and 10 seconds
 * 1:00:00 - Would be an hour
 * 1h - Would also be an hour
 *
 * @param time The time string to parse.
 * @return The long representing the time entered in millis from when this method was ran.
 */
public static Long parseTime(String time) {
    Matcher digitMatcher = timeRegex.matcher(time);
    if (digitMatcher.matches()) {
        try {
            return new PeriodFormatterBuilder().appendHours().appendSuffix(":").appendMinutes().appendSuffix(":").appendSeconds().toFormatter().parsePeriod(time).toStandardDuration().getMillis();
        } catch (IllegalArgumentException e) {
            return null;
        }
    }
    PeriodFormatter formatter = new PeriodFormatterBuilder().appendHours().appendSuffix("h").appendMinutes().appendSuffix("m").appendSeconds().appendSuffix("s").toFormatter();
    Period period;
    try {
        period = formatter.parsePeriod(time);
    } catch (IllegalArgumentException e) {
        return null;
    }
    return period.toStandardDuration().getMillis();
}
Also used : PeriodFormatterBuilder(org.joda.time.format.PeriodFormatterBuilder) PeriodFormatter(org.joda.time.format.PeriodFormatter) Matcher(java.util.regex.Matcher) Period(org.joda.time.Period)

Aggregations

PeriodFormatter (org.joda.time.format.PeriodFormatter)22 PeriodFormatterBuilder (org.joda.time.format.PeriodFormatterBuilder)15 Period (org.joda.time.Period)13 DateTime (org.joda.time.DateTime)7 Duration (org.joda.time.Duration)4 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 RuleSetBean (org.akaza.openclinica.domain.rule.RuleSetBean)2 ExpressionObjectWrapper (org.akaza.openclinica.domain.rule.expression.ExpressionObjectWrapper)2 ExpressionProcessor (org.akaza.openclinica.domain.rule.expression.ExpressionProcessor)2 MalformedURLException (java.net.MalformedURLException)1 UnknownHostException (java.net.UnknownHostException)1 MessageFormat (java.text.MessageFormat)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Locale (java.util.Locale)1 Matcher (java.util.regex.Matcher)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1