List of usage examples for org.joda.time DateTimeZone getOffset
public final int getOffset(ReadableInstant instant)
From source file:com.funambol.common.pim.converter.TimeZoneHelper.java
License:Open Source License
private boolean matchesID(String idToCheck) { DateTimeZone tz; try {/*from ww w .ja va 2s . c o 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.funambol.common.pim.converter.TimeZoneHelper.java
License:Open Source License
protected long fixFrom(DateTimeZone tz, int standardOffset, long from) { if (standardOffset != tz.getOffset(from)) { // NB: this must NOT be // a call to getBasicOffset(), because that method may be // overriden by the cached implementation(s) of this class. do {// w w w. ja v a 2 s . c o m from = tz.previousTransition(from) + 1; } while ((from != 0) && (standardOffset == tz.getOffset(from))); } else { from = tz.nextTransition(from); } return from; }
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 w ww . j a v a2 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.google.sampling.experiential.server.EventRetriever.java
License:Open Source License
public static Date adjustTimeToTimezoneIfNecesssary(String tz, Date responseTime) { if (responseTime == null) { return null; }// w w w. j av a 2 s . co m DateTimeZone timezone = null; if (tz != null) { timezone = DateTimeZone.forID(tz); } if (timezone != null && responseTime.getTimezoneOffset() != timezone.getOffset(responseTime.getTime())) { responseTime = new DateTime(responseTime).withZone(timezone).toDate(); } return responseTime; }
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:com.squarespace.template.plugins.PluginDateUtils.java
License:Apache License
public static void humanizeDate(long instantMs, long baseMs, String tzId, boolean showSeconds, StringBuilder buf) {//from ww w. j a va 2 s . c om DateTimeZone timeZone = DateTimeZone.forID(tzId); int offset = timeZone.getOffset(instantMs); Duration delta = new Duration(baseMs - instantMs + offset); int days = (int) delta.getStandardDays(); int years = (int) Math.floor(days / 365.0); if (years > 0) { humanizeDatePlural(years, DatePartType.YEAR, buf); return; } int months = (int) Math.floor(days / 30.0); if (months > 0) { humanizeDatePlural(months, DatePartType.MONTH, buf); return; } int weeks = (int) Math.floor(days / 7.0); if (weeks > 0) { humanizeDatePlural(weeks, DatePartType.WEEK, buf); return; } if (days > 0) { humanizeDatePlural(days, DatePartType.DAY, buf); return; } int hours = (int) delta.getStandardHours(); if (hours > 0) { humanizeDatePlural(hours, DatePartType.HOUR, buf); return; } int mins = (int) delta.getStandardMinutes(); if (mins > 0) { humanizeDatePlural(mins, DatePartType.MINUTE, buf); return; } int secs = (int) delta.getStandardSeconds(); if (showSeconds) { humanizeDatePlural(secs, DatePartType.SECOND, buf); return; } buf.append("less than a minute ago"); }
From source file:com.thinkbiganalytics.metadata.jobrepo.nifi.provenance.NifiStatsJmsReceiver.java
License:Apache License
/** * the BulletinDTO comes back from nifi as a Date object in the year 1970 * We need to convert this to the current date and account for DST * * @param b the bulletin/* www . j av a 2 s .c om*/ */ private DateTime getAdjustBulletinDateTime(BulletinDTO b) { DateTimeZone defaultZone = DateTimeZone.getDefault(); int currentOffsetMillis = defaultZone.getOffset(DateTime.now().getMillis()); double currentOffsetHours = (double) currentOffsetMillis / 1000d / 60d / 60d; long bulletinOffsetMillis = DateTimeZone.getDefault().getOffset(b.getTimestamp().getTime()); double bulletinOffsetHours = (double) bulletinOffsetMillis / 1000d / 60d / 60d; DateTime adjustedTime = new DateTime(b.getTimestamp()).withDayOfYear(DateTime.now().getDayOfYear()) .withYear(DateTime.now().getYear()); int adjustedHours = 0; if (currentOffsetHours != bulletinOffsetHours) { adjustedHours = new Double(bulletinOffsetHours - currentOffsetHours).intValue(); adjustedTime = adjustedTime.plusHours(-adjustedHours); } return adjustedTime; }
From source file:io.prestosql.operator.scalar.DateTimeFunctions.java
License:Apache License
private static long timeAtTimeZone(ConnectorSession session, long timeWithTimeZone, TimeZoneKey timeZoneKey) { DateTimeZone sourceTimeZone = getDateTimeZone(unpackZoneKey(timeWithTimeZone)); DateTimeZone targetTimeZone = getDateTimeZone(timeZoneKey); long millis = unpackMillisUtc(timeWithTimeZone); // STEP 1. Calculate source UTC millis in session start millis += valueToSessionTimeZoneOffsetDiff(session.getStartTime(), sourceTimeZone); // STEP 2. Calculate target UTC millis in 1970 millis -= valueToSessionTimeZoneOffsetDiff(session.getStartTime(), targetTimeZone); // STEP 3. Make sure that value + offset is in 0 - 23:59:59.999 long localMillis = millis + targetTimeZone.getOffset(0); // Loops up to 2 times in total while (localMillis > TimeUnit.DAYS.toMillis(1)) { millis -= TimeUnit.DAYS.toMillis(1); localMillis -= TimeUnit.DAYS.toMillis(1); }/*from w w w . ja v a2 s . c om*/ while (localMillis < 0) { millis += TimeUnit.DAYS.toMillis(1); localMillis += TimeUnit.DAYS.toMillis(1); } return packDateTimeWithZone(millis, timeZoneKey); }
From source file:io.prestosql.operator.scalar.DateTimeFunctions.java
License:Apache License
private static long valueToSessionTimeZoneOffsetDiff(long millisUtcSessionStart, DateTimeZone timeZone) { return timeZone.getOffset(0) - timeZone.getOffset(millisUtcSessionStart); }
From source file:jp.furplag.util.time.DateTimeUtils.java
License:Apache License
/** * calculate Chronological Julian Day represented by specified instant. * * @param instant//from w w w . j av a 2 s . c o m * @param zone * @return */ public static double toCJD(final Object instant, final DateTimeZone zone) { DateTime then = toDT(instant, zone); return toAJD(then.plusMillis(zone.getOffset(then))) + .5d; }