Example usage for org.joda.time DateTimeZone getStandardOffset

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

Introduction

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

Prototype

public abstract int getStandardOffset(long instant);

Source Link

Document

Gets the raw millisecond offset to add to UTC.

Usage

From source file:com.funambol.common.pim.converter.CachedTimeZoneHelper.java

License:Open Source License

@Override
public List<Property> getXVCalendarProperties() {

    List<Property> properties = new ArrayList<Property>();
    Map<Integer, String> cache;
    String id = toID();/*w  w  w .  ja v  a2 s.  co  m*/

    synchronized (cache10) { // Acquires lock on cache10

        // Checks the cache for elements needed
        cache: if (cache10.containsKey(id)) { // cached

            cache = cache10.get(id);

            if (!cache.containsKey(FIXED_KEY)) { // not a fixed time zone
                DateTimeZone dtz = DateTimeZone.forID(id);
                int since = TimeZoneHelper.year(fixFrom(dtz, dtz.getStandardOffset(from), from));
                int until = TimeZoneHelper.year(to);
                for (int year = since; year <= until; year++) {
                    if (!cache.containsKey(Integer.valueOf(year))) {
                        break cache; // not enough transitions in cache
                    }
                }
                // All transitions are in cache: they will just be retrieved
                properties.add(new Property("TZ", cache.get(TZ_KEY)));
                for (int year = since; year <= until; year++) {
                    properties.add(new Property("DAYLIGHT", cache.get(Integer.valueOf(year))));
                }
                return properties;

            } else { // a fixed time zone
                properties.add(new Property("TZ", cache.get(TZ_KEY)));
                properties.add(new Property("DAYLIGHT", "FALSE"));
                return properties;
            }

        } else { // not in cache, must be created
            cache = new Hashtable<Integer, String>();
            cache10.put(id, cache);
        }

        // Creates elements missing in the cache
        Property tz = getTZ();
        properties.add(tz);
        cache.put(TZ_KEY, tz.getValue());

        List<Property> dayLightList = getDaylightList();
        properties.addAll(dayLightList);
        for (Property dayLight : dayLightList) {
            Integer year = Integer.valueOf(year(dayLight));
            if (!cache.containsKey(year)) {
                cache.put(year, dayLight.getValue());
            }
        }

    } // Releases lock on cache10

    return properties;
}

From source file:com.funambol.common.pim.converter.TimeZoneHelper.java

License:Open Source License

/**
 * Extract time-zone information from a zoneinfo (Olson database) ID and
 * saves them in the TimeZoneHelper fields.
 *
 * @param id the time zone ID according to the zoneinfo (Olson) database
 * @param from the start of the relevant time interval for the generation of
 *             transitions (an istant expressed as a long)
 * @param to the end of the relevant time interval for the generation of
 *           transitions (an istant expressed as a long)
 *//*from   ww w . j a va  2s  .  com*/
protected void processID(String id, long from, long to) {

    DateTimeZone tz = DateTimeZone.forID(id);
    if (name == null) { // The name could have been set already using TZID
                        // and in this case it is important not to change it
        name = id; // The Olson ID is perfect as a unique name
    }
    basicOffset = tz.getStandardOffset(from);
    transitions.clear();

    if (!tz.isFixed()) {

        long oldFrom = from;
        from = fixFrom(tz, basicOffset, oldFrom);

        //@todo Consider case when to go beyond last transition (cycle 
        //could become endless)
        while (tz.getStandardOffset(to) != tz.getOffset(to)) {
            to = tz.nextTransition(to);
        }

        while ((from <= to) && (oldFrom != from)) {
            transitions.add(new TimeZoneTransition(tz.getOffset(from), from, id));
            oldFrom = from;
            from = tz.nextTransition(oldFrom);
        }
    }
}

From source file:com.funambol.common.pim.converter.TimeZoneHelper.java

License:Open Source License

private boolean matchesID(String idToCheck) {

    DateTimeZone tz;

    try {//from   www  . j a v  a 2  s  .  co  m
        tz = DateTimeZone.forID(idToCheck);
    } catch (IllegalArgumentException e) { // the ID is not recognized
        return false;
    }

    if (getTransitions().size() == 0) { // No transitions
        if (tz.getStandardOffset(REFERENCE_TIME) != basicOffset) {
            return false; // Offsets don't match: wrong guess
        }
        if (tz.isFixed() || (REFERENCE_TIME == tz.nextTransition(REFERENCE_TIME))) {
            return true; // A right fixed or currently-fixed time zone
                         // has been found
        }
        return false; // Wrong guess
    }

    long t = getTransitions().get(0).getTime() - 1;
    if (tz.getStandardOffset(t) != basicOffset) {
        return false; // Wrong guess
    }

    for (TimeZoneTransition transition : getTransitions()) {
        t = tz.nextTransition(t);
        if (!isClose(t, transition.getTime())) {
            return false; // Wrong guess
        }
        if (tz.getOffset(t) != transition.getOffset()) {
            return false; // Wrong guess
        }
    }
    return true; // A right non-fixed time zone has been found

}

From source file:com.google.ical.compat.jodatime.TimeZoneConverter.java

License:Apache License

/**
 * return a <code>java.util.Timezone</code> object that delegates to
 * the given Joda <code>DateTimeZone</code>.
 *//*from  www . j a v a 2 s .  co m*/
public static TimeZone toTimeZone(final DateTimeZone dtz) {

    TimeZone tz = new TimeZone() {
        @Override
        public void setRawOffset(int n) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean useDaylightTime() {
            long firstTransition = MILLIS_SINCE_1_JAN_2000_UTC;
            return firstTransition != dtz.nextTransition(firstTransition);
        }

        @Override
        public boolean inDaylightTime(Date d) {
            long t = d.getTime();
            return dtz.getStandardOffset(t) != dtz.getOffset(t);
        }

        @Override
        public int getRawOffset() {
            return dtz.getStandardOffset(0);
        }

        @Override
        public int getOffset(long instant) {
            // This method is not abstract, but it normally calls through to the
            // method below.
            // It's optimized here since there's a direct equivalent in
            // DateTimeZone.
            // DateTimeZone and java.util.TimeZone use the same
            // epoch so there's no translation of instant required.
            return dtz.getOffset(instant);
        }

        @Override
        public int getOffset(int era, int year, int month, int day, int dayOfWeek, int milliseconds) {
            int millis = milliseconds; // milliseconds is day in standard time
            int hour = millis / MILLISECONDS_PER_HOUR;
            millis %= MILLISECONDS_PER_HOUR;
            int minute = millis / MILLISECONDS_PER_MINUTE;
            millis %= MILLISECONDS_PER_MINUTE;
            int second = millis / MILLISECONDS_PER_SECOND;
            millis %= MILLISECONDS_PER_SECOND;
            if (era == GregorianCalendar.BC) {
                year = -(year - 1);
            }

            // get the time in UTC in case a timezone has changed it's standard
            // offset, e.g. rid of a half hour from UTC.
            DateTime dt = null;
            try {
                dt = new DateTime(year, month + 1, day, hour, minute, second, millis, dtz);
            } catch (IllegalArgumentException ex) {
                // Java does not complain if you try to convert a Date that does not
                // exist due to the offset shifting forward, but Joda time does.
                // Since we're trying to preserve the semantics of TimeZone, shift
                // forward over the gap so that we're on a time that exists.
                // This assumes that the DST correction is one hour long or less.
                if (hour < 23) {
                    dt = new DateTime(year, month + 1, day, hour + 1, minute, second, millis, dtz);
                } else { // Some timezones shift at midnight.
                    Calendar c = new GregorianCalendar();
                    c.clear();
                    c.setTimeZone(TimeZone.getTimeZone("UTC"));
                    c.set(year, month, day, hour, minute, second);
                    c.add(Calendar.HOUR_OF_DAY, 1);
                    int year2 = c.get(Calendar.YEAR), month2 = c.get(Calendar.MONTH),
                            day2 = c.get(Calendar.DAY_OF_MONTH), hour2 = c.get(Calendar.HOUR_OF_DAY);
                    dt = new DateTime(year2, month2 + 1, day2, hour2, minute, second, millis, dtz);
                }
            }
            // since millis is in standard time, we construct the equivalent
            // GMT+xyz timezone and use that to convert.
            int offset = dtz.getStandardOffset(dt.getMillis());
            DateTime stdDt = new DateTime(year, month + 1, day, hour, minute, second, millis,
                    DateTimeZone.forOffsetMillis(offset));
            return getOffset(stdDt.getMillis());
        }

        @Override
        public String toString() {
            return dtz.toString();
        }

        @Override
        public boolean equals(Object that) {
            if (!(that instanceof TimeZone)) {
                return false;
            }
            TimeZone thatTz = (TimeZone) that;
            return getID().equals(thatTz.getID()) && hasSameRules(thatTz);
        }

        @Override
        public int hashCode() {
            return getID().hashCode();
        }

        private static final long serialVersionUID = 58752546800455L;
    };
    // Now fix the tzids.  DateTimeZone has a bad habit of returning
    // "+06:00" when it should be "GMT+06:00"
    String newTzid = cleanUpTzid(dtz.getID());
    tz.setID(newTzid);
    return tz;
}

From source file:com.microsoft.exchange.utils.TimeZoneHelper.java

License:Open Source License

/**
 * Converts the given Java time zone into into the corresponding EWS representation.
 *
 * @param tz time zone to convert//  w  w  w . ja v  a  2  s . c om
 * @return the resulting SerializableTimeZone object
 */
public static SerializableTimeZone toSerializableTimeZone(TimeZone tz) {
    long now = DateTimeUtils.currentTimeMillis();
    DateTimeZone zone = DateTimeZone.forTimeZone(tz);
    int standardOffset = zone.getStandardOffset(now);
    SerializableTimeZone result = new SerializableTimeZone();
    result.setBias(toBias(standardOffset));
    // check if the zone has no transitions
    if (zone.isFixed()) {
        // fake transitions for fixed zones
        result.setStandardTime(FIXED_STANDARD_TIME);
        result.setDaylightTime(FIXED_DAYLIGHT_TIME);
    } else {
        // it is assumed that 2 subsequent transition will have both STD and DST
        long transition = setTransitionTime(zone, now, result);
        setTransitionTime(zone, transition, result);
    }
    return result;
}

From source file:com.microsoft.exchange.utils.TimeZoneHelper.java

License:Open Source License

private static SerializableTimeZoneTime toSerializableTimeZoneTime(DateTimeZone zone, long transition) {
    int standardOffset = zone.getStandardOffset(transition);
    long offset = zone.getOffset(transition);
    int bias = toBias(offset - standardOffset);
    DateTime time = new DateTime(transition, zone);
    short month = (short) time.getMonthOfYear();
    short day = (short) time.getDayOfMonth();
    DayOfWeekType dayOfWeek = toDayOfWeek(time.getDayOfWeek());
    return new SerializableTimeZoneTime(bias, time.toString("HH:mm:ss"), day, month, dayOfWeek,
            time.toString("yyyy"));
}

From source file:it.geosolutions.geobatch.destination.common.utils.TimeUtils.java

License:Open Source License

/**
 * Obtain hour offset of the zone//  ww  w . j  a v a  2  s .  co  m
 * 
 * @param zone to obtain it
 * @return hour offset
 */
public static int getHour(DateTimeZone zone) {
    return (int) Math.floor(zone.getStandardOffset(0) / MILLISECONDS_PER_HOUR);
}

From source file:it.geosolutions.geobatch.destination.common.utils.TimeUtils.java

License:Open Source License

/**
 * Obtain minutes offset of the zone (need to be conbined with
 * {@link TimeUtils#getHour(DateTimeZone)})
 * //  w  w w. j  a  v  a2s  .  c  o  m
 * @param zone to obtain it
 * @return minutes offset
 */
public static int getMinutes(DateTimeZone zone) {
    return (zone.getStandardOffset(0) - (getHour(zone) * MILLISECONDS_PER_HOUR)) * MILLISECONDS_PER_MINUTE;
}

From source file:nodatime.jodadump.JodaDump.java

License:Open Source License

private static void dumpZone(String id, DateTimeZone zone) {
    // TODO: Reinstate this when Noda Time understands aliases better.
    // See issue 32 for more details.
    // if (!id.equals(zone.getID())) {
    //    System.out.println(id + " == " + zone.getID());
    //    System.out.println();
    //    return;
    // }//from  www  . j  a  v a  2s .  c o  m
    System.out.println(id);
    long now = START.getMillis();
    while (now < END.getMillis()) {
        int standardOffset = zone.getStandardOffset(now);
        int wallOffset = zone.getOffset(now);

        System.out.printf("%s  %s  %s%n", INSTANT_FORMAT.print(now), printOffset(standardOffset),
                printOffset(wallOffset - standardOffset));
        // TODO: Output the name when we've got parity in handling
        // of auto-addition for summer/winter in Noda Time.
        long next = zone.nextTransition(now);
        if (next <= now) {
            break;
        }
        now = next;
    }
    System.out.println();
}

From source file:org.killbill.billing.util.account.AccountDateTimeUtils.java

License:Apache License

public static DateTimeZone getFixedOffsetTimeZone(final DateTimeZone referenceDateTimeZone,
        final Entity account) {
    final DateTime referenceDateTime = getReferenceDateTime(account);

    // Check if DST was in effect at the reference date time
    final boolean shouldUseDST = !referenceDateTimeZone.isStandardOffset(referenceDateTime.getMillis());
    if (shouldUseDST) {
        return DateTimeZone.forOffsetMillis(referenceDateTimeZone.getOffset(referenceDateTime.getMillis()));
    } else {/*  w w  w. j a  va 2  s .com*/
        return DateTimeZone
                .forOffsetMillis(referenceDateTimeZone.getStandardOffset(referenceDateTime.getMillis()));
    }
}