Example usage for org.joda.time DateTimeZone forID

List of usage examples for org.joda.time DateTimeZone forID

Introduction

In this page you can find the example usage for org.joda.time DateTimeZone forID.

Prototype

@FromString
public static DateTimeZone forID(String id) 

Source Link

Document

Gets a time zone instance for the specified time zone id.

Usage

From source file:io.spikex.notifier.Notifier.java

License:Apache License

@Override
public void handle(final Message<JsonObject> message) {
    ///*from  w w  w  . j a va  2  s.co m*/
    // Find matching rule
    //
    m_logger.debug("Received: {}", message.body());
    DateTime now = DateTime.now();
    String timezone = now.getZone().getID();
    Map<String, CronEntry> schedules = m_config.getSchedules();
    List<Rule> rules = m_config.getRules();
    for (Rule rule : rules) {
        //
        // Matching schedule and tags?
        //
        CronEntry entry = schedules.get(rule.getSchedule());
        if (!timezone.equals(entry.getTimezone())) {
            timezone = entry.getTimezone();
            now = DateTime.now(DateTimeZone.forID(timezone));
        }
        // We must make a copy (we want to replace @message with the translated template contents)
        JsonObject event = new JsonObject().mergeIn(message.body());
        if (entry.isDefined(now) && rule.match(event)) {

            List<String> destinations = rule.getDestinations();
            m_logger.debug("Rule \"{}\" matched - template: {} notifying: {}", rule.getName(),
                    rule.getTemplate(), destinations);

            //
            // Add optional fields (if missing) - so that templates work correctly
            //
            if (!event.containsField(EVENT_FIELD_NOTIF_SUCCESS)) {
                event.putBoolean(EVENT_FIELD_NOTIF_SUCCESS, false);
            }
            if (!event.containsField(EVENT_FIELD_NOTIF_WARNING)) {
                event.putBoolean(EVENT_FIELD_NOTIF_WARNING, false);
            }
            if (!event.containsField(EVENT_FIELD_NOTIF_DANGER)) {
                event.putBoolean(EVENT_FIELD_NOTIF_DANGER, false);
            }

            //
            // Translate subject and fill/execute template
            //
            String subject = m_variables.translate(event, rule.getSubject());
            Mustache template = m_templates.get(rule.getTemplate());
            StringWriter writer = new StringWriter();
            Map scopes = new HashMap(event.toMap());
            scopes.putAll(m_functions); // Functions
            template.execute(writer, scopes);
            List<String> resolvedDestinations = resolveDestinations(destinations);

            //
            // Fill event with notification info
            //
            event.putString(EVENT_FIELD_TITLE, subject);
            event.putString(EVENT_FIELD_MESSAGE, writer.toString());
            event.putArray(EVENT_FIELD_DESTINATIONS, new JsonArray(resolvedDestinations));

            if (m_map != null && m_queue != null) {

                // Calculate event hash
                String hash = XXHash32.hashAsHex(event.toString(), HASH_SALT);

                m_logger.info("Storing notification: {} priority: {} destinations: {} subject: {} hash: {}",
                        event.getString(EVENT_FIELD_ID, ""), event.getString(EVENT_FIELD_PRIORITY, ""),
                        resolvedDestinations, subject, hash);

                if (m_map.putIfAbsent(hash, event) == null) {
                    m_queue.add(event);
                } else {
                    m_logger.info("Ignoring duplicate event: {}", hash);
                }

            } else {
                // Log event
                m_logger.error("Notification map has not been initialized. " + "Could not handle event: {}",
                        event.toString());
            }
        }
    }
}

From source file:io.warp10.script.functions.ADDDAYS.java

License:Apache License

@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {

    Object top = stack.pop();/*from   w w w  .j  av  a  2s  . c  om*/

    if (!(top instanceof Long)) {
        throw new WarpScriptException(getName() + " expects a number of days on top of the stack.");
    }

    int days = ((Number) top).intValue();

    top = stack.pop();

    String tz = null;

    if (top instanceof String) {
        tz = top.toString();
        top = stack.pop();
        if (!(top instanceof Long)) {
            throw new WarpScriptException(
                    getName() + " operates on a tselements list, timestamp, or timestamp and timezone.");
        }
    } else if (!(top instanceof List) && !(top instanceof Long)) {
        throw new WarpScriptException(
                getName() + " operates on a tselements list, timestamp, or timestamp and timezone.");
    }

    if (top instanceof Long) {
        long instant = ((Number) top).longValue();

        if (null == tz) {
            tz = "UTC";
        }

        DateTimeZone dtz = DateTimeZone.forID(null == tz ? "UTC" : tz);

        DateTime dt = new DateTime(instant / Constants.TIME_UNITS_PER_MS, dtz);

        dt = dt.plusDays(days);

        long ts = dt.getMillis() * Constants.TIME_UNITS_PER_MS + (instant % Constants.TIME_UNITS_PER_MS);

        stack.push(ts);
    } else {
        List<Object> elts = new ArrayList<Object>((List<Object>) top);

        int year = ((Number) elts.get(0)).intValue();
        int month = ((Number) elts.get(1)).intValue();
        int day = ((Number) elts.get(2)).intValue();

        if (days < 0) {
            while (days < 0) {
                days++;
                day = day - 1;
                if (day < 1) {
                    month--;
                    if (month < 1) {
                        year--;
                        month = 12;
                    }
                    if (1 == month || 3 == month || 5 == month || 7 == month || 8 == month || 10 == month
                            || 12 == month) {
                        day = 31;
                    } else if (4 == month || 6 == month || 9 == month || 11 == month) {
                        day = 30;
                    } else if (0 == year % 100 || 0 != year % 4) {
                        day = 28;
                    } else {
                        day = 29;
                    }
                }
            }
        } else {
            while (days > 0) {
                days--;
                day = day + 1;

                if ((1 == month || 3 == month || 5 == month || 7 == month || 8 == month || 10 == month
                        || 12 == month) && day > 31) {
                    month++;
                    day = 1;
                } else if ((4 == month || 6 == month || 9 == month || 11 == month) && day > 30) {
                    month++;
                    day = 1;
                } else if (2 == month && (0 == year % 100 || 0 != year % 4) && day > 28) {
                    month++;
                    day = 1;
                } else if (2 == month && day > 29) {
                    month++;
                    day = 1;
                }

                if (month > 12) {
                    month = 1;
                    year++;
                }
            }
        }

        elts.set(0, (long) year);
        elts.set(1, (long) month);
        elts.set(2, (long) day);

        stack.push(elts);
    }

    return stack;
}

From source file:io.warp10.script.functions.ADDMONTHS.java

License:Apache License

@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {

    Object top = stack.pop();/* w  w w  .j a v  a2  s  .c o  m*/

    if (!(top instanceof Long)) {
        throw new WarpScriptException(getName() + " expects a number of months on top of the stack.");
    }

    int months = ((Number) top).intValue();

    top = stack.pop();

    String tz = null;

    if (top instanceof String) {
        tz = top.toString();
        top = stack.pop();
        if (!(top instanceof Long)) {
            throw new WarpScriptException(
                    getName() + " operates on a tselements list, timestamp, or timestamp and timezone.");
        }
    } else if (!(top instanceof List) && !(top instanceof Long)) {
        throw new WarpScriptException(
                getName() + " operates on a tselements list, timestamp, or timestamp and timezone.");
    }

    if (top instanceof Long) {
        long instant = ((Number) top).longValue();

        if (null == tz) {
            tz = "UTC";
        }

        DateTimeZone dtz = DateTimeZone.forID(null == tz ? "UTC" : tz);

        DateTime dt = new DateTime(instant / Constants.TIME_UNITS_PER_MS, dtz);

        dt = dt.plusMonths(months);

        long ts = dt.getMillis() * Constants.TIME_UNITS_PER_MS + (instant % Constants.TIME_UNITS_PER_MS);

        stack.push(ts);
    } else {
        List<Object> elts = new ArrayList<Object>((List<Object>) top);

        int year = ((Number) elts.get(0)).intValue();
        int month = ((Number) elts.get(1)).intValue();

        if (months < 0) {
            while (months < 0) {
                months++;
                month = month - 1;
                if (month < 1) {
                    month = 12;
                    year--;
                }
            }
        } else {
            while (months > 0) {
                months--;
                month = month + 1;
                if (month > 12) {
                    month = 1;
                    year++;
                }
            }
        }

        elts.set(0, (long) year);
        elts.set(1, (long) month);

        // Now check that the month is compatible with the day

        if (elts.size() > 2) {
            int day = ((Number) elts.get(2)).intValue();

            if (2 == month && day > 28) {
                if ((0 != year % 4) || (0 == year % 100)) {
                    elts.set(2, (long) 28);
                } else {
                    elts.set(2, (long) 29);
                }
            } else if (day > 30 && (4 == month || 6 == month || 9 == month || 11 == month)) {
                elts.set(2, (long) 30);
            }
        }

        stack.push(elts);
    }

    return stack;
}

From source file:io.warp10.script.functions.ADDYEARS.java

License:Apache License

@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {

    Object top = stack.pop();//  w  w  w  . j av  a 2  s .  c  om

    if (!(top instanceof Long)) {
        throw new WarpScriptException(getName() + " expects a number of years on top of the stack.");
    }

    int years = ((Number) top).intValue();

    top = stack.pop();

    String tz = null;

    if (top instanceof String) {
        tz = top.toString();
        top = stack.pop();
        if (!(top instanceof Long)) {
            throw new WarpScriptException(
                    getName() + " operates on a tselements list, timestamp, or timestamp and timezone.");
        }
    } else if (!(top instanceof List) && !(top instanceof Long)) {
        throw new WarpScriptException(
                getName() + " operates on a tselements list, timestamp, or timestamp and timezone.");
    }

    if (top instanceof Long) {
        long instant = ((Number) top).longValue();

        if (null == tz) {
            tz = "UTC";
        }

        DateTimeZone dtz = DateTimeZone.forID(null == tz ? "UTC" : tz);

        DateTime dt = new DateTime(instant / Constants.TIME_UNITS_PER_MS, dtz);

        dt = dt.plusYears(years);

        long ts = dt.getMillis() * Constants.TIME_UNITS_PER_MS + (instant % Constants.TIME_UNITS_PER_MS);

        stack.push(ts);
    } else {
        List<Object> elts = new ArrayList<Object>((List<Object>) top);

        int year = ((Number) elts.get(0)).intValue();
        year += years;

        elts.set(0, (long) year);

        // Now check if we are in ferbuary and if this is coherent with
        // a possibly non leap year

        if (elts.size() > 2) {
            int month = ((Number) elts.get(1)).intValue();
            int day = ((Number) elts.get(2)).intValue();

            if (2 == month && day > 28) {
                if ((0 != year % 4) || (0 == year % 100)) {
                    elts.set(2, (long) 28);
                }
            }
        }

        stack.push(elts);
    }

    return stack;
}

From source file:io.warp10.script.functions.FROMTSELEMENTS.java

License:Apache License

@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {

    Object obj = stack.peek();/*ww  w. j  a va  2 s  .  co  m*/

    String tz = null;

    if (obj instanceof String) {
        tz = (String) obj;
        stack.pop();
    } else if (!(obj instanceof List)) {
        throw new WarpScriptException(getName()
                + " operates on a list of timestamp elements or a list of timestamp elements + timezone.");
    }

    DateTimeZone dtz = this.tzones.get(tz);

    if (null == dtz) {
        dtz = DateTimeZone.forID(null == tz ? "UTC" : tz);
        this.tzones.put(tz, dtz);
    }

    obj = stack.pop();

    if (!(obj instanceof List)) {
        throw new WarpScriptException(getName()
                + " operates on a list of timestamp elements or a list of timestamp elements + timezone.");
    }

    List<Object> elts = (List) obj;

    DateTime dt = new DateTime(elts.size() > 0 ? ((Number) elts.get(0)).intValue() : 1970,
            elts.size() > 1 ? ((Number) elts.get(1)).intValue() : 1,
            elts.size() > 2 ? ((Number) elts.get(2)).intValue() : 1,
            elts.size() > 3 ? ((Number) elts.get(3)).intValue() : 0,
            elts.size() > 4 ? ((Number) elts.get(4)).intValue() : 0,
            elts.size() > 5 ? ((Number) elts.get(5)).intValue() : 0, dtz);

    // Retrieve timestamp in ms

    long tsms = (dt.getMillis() / 1000L) * Constants.TIME_UNITS_PER_S;

    if (elts.size() > 6) {
        tsms += ((Number) elts.get(6)).intValue() % Constants.TIME_UNITS_PER_S;
    }

    stack.push(tsms);

    return stack;
}

From source file:io.warp10.script.functions.ISO8601.java

License:Apache License

@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {

    Object obj = stack.peek();/*from  www . j  a v a2  s  . c  o m*/

    String tz = null;

    if (obj instanceof String) {
        tz = (String) obj;
        stack.pop();
    } else if (!(obj instanceof Long)) {
        throw new WarpScriptException(getName() + " operates on a timestamp or a timestamp + timezone.");
    }

    obj = stack.pop();

    if (!(obj instanceof Long)) {
        throw new WarpScriptException(getName() + " operates on a timestamp or a timestamp + timezone.");
    }

    long ts = (long) obj;

    DateTimeFormatter dtf;

    //
    // Set the timezone
    //

    if (null == tz) {
        dtf = this.dtf.withZoneUTC();
    } else {
        dtf = this.dtf.withZone(DateTimeZone.forID(tz));
    }

    long millis = ts / Constants.TIME_UNITS_PER_MS;

    String dt = dtf.print(millis);

    if (Constants.TIME_UNITS_PER_MS > 1) {
        //
        // Add sub millisecond string
        //

        StringBuilder sb = new StringBuilder();

        sb.append(dt, 0, 23);

        long subms = Constants.TIME_UNITS_PER_MS;

        subms += ts % Constants.TIME_UNITS_PER_MS;

        String str = Long.toString(subms);

        sb.append(str, 1, str.length());

        sb.append(dt, 23, dt.length());

        stack.push(sb.toString());
    } else {
        stack.push(dt);
    }

    return stack;
}

From source file:io.warp10.script.functions.TSELEMENTS.java

License:Apache License

@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {

    Object obj = stack.peek();/*from w  ww  . ja va  2s .c  o m*/

    String tz = null;

    if (obj instanceof String) {
        tz = (String) obj;
        stack.pop();
    } else if (!(obj instanceof Long)) {
        throw new WarpScriptException(getName() + " operates on a timestamp or a timestamp + timezone.");
    }

    DateTimeZone dtz = this.tzones.get(tz);

    if (null == dtz) {
        dtz = DateTimeZone.forID(null == tz ? "UTC" : tz);
        this.tzones.put(tz, dtz);
    }

    obj = stack.pop();

    if (!(obj instanceof Long)) {
        throw new WarpScriptException(getName() + " operates on a timestamp or a timestamp + timezone.");
    }

    long ts = (long) obj;

    // Convert ts to milliseconds

    long tsms = ts / Constants.TIME_UNITS_PER_MS;

    DateTime dt = new DateTime(tsms, dtz);

    // Extract components into an array

    List<Long> elements = new ArrayList<Long>();

    elements.add((long) dt.getYear());
    elements.add((long) dt.getMonthOfYear());
    elements.add((long) dt.getDayOfMonth());
    elements.add((long) dt.getHourOfDay());
    elements.add((long) dt.getMinuteOfHour());
    elements.add((long) dt.getSecondOfMinute());
    elements.add(ts % Constants.TIME_UNITS_PER_S);
    elements.add((long) dt.getDayOfYear());
    elements.add((long) dt.getDayOfWeek());
    elements.add((long) dt.getWeekOfWeekyear());

    stack.push(elements);

    return stack;
}

From source file:io.warp10.script.mapper.MapperDayOfMonth.java

License:Apache License

public MapperDayOfMonth(String name, Object timezone) {
    super(name);//from w  ww  .  j av  a2s  .  c  o  m

    if (timezone instanceof String) {
        this.dtz = DateTimeZone.forID(timezone.toString());
    } else if (timezone instanceof Number) {
        this.dtz = DateTimeZone.forOffsetMillis(((Number) timezone).intValue());
    } else {
        this.dtz = DateTimeZone.UTC;
    }
}

From source file:io.warp10.script.mapper.MapperDayOfWeek.java

License:Apache License

public MapperDayOfWeek(String name, Object timezone) {
    super(name);//from  w ww.  j ava 2s. co m
    if (timezone instanceof String) {
        this.dtz = DateTimeZone.forID(timezone.toString());
    } else if (timezone instanceof Number) {
        this.dtz = DateTimeZone.forOffsetMillis(((Number) timezone).intValue());
    } else {
        this.dtz = DateTimeZone.UTC;
    }
}

From source file:io.warp10.script.mapper.MapperHourOfDay.java

License:Apache License

public MapperHourOfDay(String name, Object timezone) {
    super(name);/*from  w  w  w.  j  a va 2s . c  o  m*/
    if (timezone instanceof String) {
        this.dtz = DateTimeZone.forID(timezone.toString());
    } else if (timezone instanceof Number) {
        this.dtz = DateTimeZone.forOffsetMillis(((Number) timezone).intValue());
    } else {
        this.dtz = DateTimeZone.UTC;
    }
}