List of usage examples for org.joda.time DateTimeZone forOffsetMillis
public static DateTimeZone forOffsetMillis(int millisOffset)
From source file:arjdbc.util.DateTimeUtils.java
License:Open Source License
public static RubyTime parseDateTime(final ThreadContext context, final String str) throws IllegalArgumentException { boolean hasDate = false; int year = 2000; int month = 1; int day = 1;// www . j a v a 2 s. c o m boolean hasTime = false; int minute = 0; int hour = 0; int second = 0; int millis = 0; long nanos = 0; DateTimeZone zone = null; boolean bcEra = false; // We try to parse these fields in order; all are optional // (but some combinations don't make sense, e.g. if you have // both date and time then they must be whitespace-separated). // At least one of date and time must be present. // leading whitespace // yyyy-mm-dd // whitespace // hh:mm:ss // whitespace // timezone in one of the formats: +hh, -hh, +hh:mm, -hh:mm // whitespace // if date is present, an era specifier: AD or BC // trailing whitespace final int len = str.length(); int start = nonSpaceIndex(str, 0, len); // Skip leading whitespace int end = nonDigitIndex(str, start, len); // Possibly read date. if (end < len && str.charAt(end) == '-') { hasDate = true; // year year = extractIntValue(str, start, end); start = end + 1; // Skip '-' // month end = nonDigitIndex(str, start, len); month = extractIntValue(str, start, end); char sep = str.charAt(end); if (sep != '-') { throw new IllegalArgumentException("expected date to be dash-separated, got '" + sep + "'"); } start = end + 1; // Skip '-' // day of month end = nonDigitIndex(str, start, len); day = extractIntValue(str, start, end); start = nonSpaceIndex(str, end, len); // Skip trailing whitespace } // Possibly read time. if (start < len && Character.isDigit(str.charAt(start))) { hasTime = true; // hours end = nonDigitIndex(str, start, len); hour = extractIntValue(str, start, end); //sep = str.charAt(end); //if ( sep != ':' ) { // throw new IllegalArgumentException("expected time to be colon-separated, got '" + sep + "'"); //} start = end + 1; // Skip ':' // minutes end = nonDigitIndex(str, start, len); minute = extractIntValue(str, start, end); //sep = str.charAt(end); //if ( sep != ':' ) { // throw new IllegalArgumentException("expected time to be colon-separated, got '" + sep + "'"); //} start = end + 1; // Skip ':' // seconds end = nonDigitIndex(str, start, len); second = extractIntValue(str, start, end); start = end; // Fractional seconds. if (start < len && str.charAt(start) == '.') { end = nonDigitIndex(str, start + 1, len); // Skip '.' int numlen = end - (start + 1); if (numlen <= 3) { millis = extractIntValue(str, start + 1, end); for (; numlen < 3; ++numlen) millis *= 10; } else { nanos = extractIntValue(str, start + 1, end); for (; numlen < 9; ++numlen) nanos *= 10; } start = end; } start = nonSpaceIndex(str, start, len); // Skip trailing whitespace } // Possibly read timezone. char sep = start < len ? str.charAt(start) : '\0'; if (sep == '+' || sep == '-') { int zoneSign = (sep == '-') ? -1 : 1; int hoursOffset, minutesOffset, secondsOffset; end = nonDigitIndex(str, start + 1, len); // Skip +/- hoursOffset = extractIntValue(str, start + 1, end); start = end; if (start < len && str.charAt(start) == ':') { end = nonDigitIndex(str, start + 1, len); // Skip ':' minutesOffset = extractIntValue(str, start + 1, end); start = end; } else { minutesOffset = 0; } secondsOffset = 0; if (start < len && str.charAt(start) == ':') { end = nonDigitIndex(str, start + 1, len); // Skip ':' secondsOffset = extractIntValue(str, start + 1, end); start = end; } // Setting offset does not seem to work correctly in all // cases.. So get a fresh calendar for a synthetic timezone // instead int offset = zoneSign * hoursOffset * 60; if (offset < 0) { offset = offset - Math.abs(minutesOffset); } else { offset = offset + minutesOffset; } offset = (offset * 60 + secondsOffset) * 1000; zone = DateTimeZone.forOffsetMillis(offset); start = nonSpaceIndex(str, start, len); // Skip trailing whitespace } if (hasDate && start < len) { final char e1 = str.charAt(start); if (e1 == 'A' && str.charAt(start + 1) == 'D') { bcEra = false; start += 2; } else if (e1 == 'B' && str.charAt(start + 1) == 'C') { bcEra = true; start += 2; } } if (start < len) { throw new IllegalArgumentException( "trailing junk: '" + str.substring(start, len - start) + "' on '" + str + "'"); } if (!hasTime && !hasDate) { throw new IllegalArgumentException("'" + str + "' has neither date nor time"); } if (bcEra) year = -1 * year; if (zone == null) { zone = isDefaultTimeZoneUTC(context) ? DateTimeZone.UTC : DateTimeZone.getDefault(); } DateTime dateTime = new DateTime(year, month, day, hour, minute, second, millis, zone); return RubyTime.newTime(context.runtime, dateTime, nanos); }
From source file:com.alliander.osgp.domain.core.valueobjects.smartmetering.CosemDateTime.java
License:Open Source License
/** * Returns this {@link CosemDateTime} as {@link DateTime} if the date, time * and deviation are specified.//from w ww. j av a2 s . c o m * * @return this {@link CosemDateTime} as {@link DateTime}, or {@code null} * if not {@link #isDateTimeSpecified()}. * * @see #isDateTimeSpecified() */ public DateTime asDateTime() { if (!this.isDateTimeSpecified()) { return null; } final LocalDateTime localDateTime = this.asLocalDateTime(); final DateTimeZone zone = DateTimeZone.forOffsetMillis(-this.deviation * MILLISECONDS_PER_MINUTE); return localDateTime.toDateTime(zone); }
From source file:com.barchart.feed.ddf.historical.api.DDF_Query.java
License:BSD License
private final CharSequence renderTime(/* local */DateTime time) { if (time == null) { time = NULL_TIME;/*from www . jav a 2s. c o m*/ } if (instrument == null) { return time.toString(); } else { final DateTimeZone zone = DateTimeZone.forOffsetMillis((int) (instrument.timeZoneOffset())); return time.withZone(zone).toString(); } }
From source file:com.barchart.feed.ddf.historical.provider.EntryBar.java
License:BSD License
@Override public String csvEntry() { final StringBuilder text = new StringBuilder(128); text.append(index);/*from ww w. jav a2 s . co m*/ text.append(ASCII.STRING_COMMA); text.append(inst.symbol()); text.append(ASCII.STRING_COMMA); text.append(millisUTC); text.append(ASCII.STRING_COMMA); text.append(new DateTime(millisUTC, DateTimeZone.forOffsetMillis((int) inst.timeZoneOffset()))); text.append(ASCII.STRING_COMMA); text.append(getTradeDay()); text.append(ASCII.STRING_COMMA); text.append(encodeMantissa(priceOpenMantissa, priceExponent())); text.append(ASCII.STRING_COMMA); text.append(encodeMantissa(priceHighMantissa, priceExponent())); text.append(ASCII.STRING_COMMA); text.append(encodeMantissa(priceLowMantissa, priceExponent())); text.append(ASCII.STRING_COMMA); text.append(encodeMantissa(priceCloseMantissa, priceExponent())); text.append(ASCII.STRING_COMMA); text.append(encodeLong(sizeVolume)); return text.toString(); }
From source file:com.barchart.feed.ddf.historical.provider.EntryTicksDetail.java
License:BSD License
@Override public String csvEntry() { final StringBuilder text = new StringBuilder(128); text.append(index);/*from w ww.ja v a 2 s . c o m*/ text.append(ASCII.STRING_COMMA); text.append(inst.symbol()); text.append(ASCII.STRING_COMMA); text.append(millisUTC); text.append(ASCII.STRING_COMMA); text.append(new DateTime(millisUTC, DateTimeZone.forOffsetMillis((int) inst.timeZoneOffset()))); text.append(ASCII.STRING_COMMA); text.append(getTradeDay()); text.append(ASCII.STRING_COMMA); text.append(getSession()); text.append(ASCII.STRING_COMMA); text.append(encodeMantissa(priceTradeMantissa, priceExponent())); text.append(ASCII.STRING_COMMA); text.append(encodeLong(sizeTrade)); return text.toString(); }
From source file:com.barchart.feed.ddf.historical.provider.EntryTicksTrend.java
License:BSD License
@Override public String csvEntry() { final StringBuilder text = new StringBuilder(128); text.append(index);// ww w. j av a2s . c om text.append(ASCII.STRING_COMMA); text.append(inst.symbol()); text.append(ASCII.STRING_COMMA); text.append(millisUTC); text.append(ASCII.STRING_COMMA); text.append(new DateTime(millisUTC, DateTimeZone.forOffsetMillis((int) inst.timeZoneOffset()))); text.append(ASCII.STRING_COMMA); text.append(getTradeDay()); text.append(ASCII.STRING_COMMA); text.append(getSession()); text.append(ASCII.STRING_COMMA); text.append(encodeMantissa(priceSupport(), priceExponent())); text.append(ASCII.STRING_COMMA); text.append(encodeMantissa(priceResistance(), priceExponent())); return text.toString(); }
From source file:com.byoutline.secretsauce.utils.JdkBasedTimeZoneProvider.java
License:Apache License
public DateTimeZone getZone(final String id) { if (id == null) { return null; }/*ww w . j av a 2 s .c om*/ return DateTimeZone.forOffsetMillis(TimeZone.getTimeZone(id).getOffset(System.currentTimeMillis())); }
From source file:com.clicktravel.common.random.Randoms.java
License:Apache License
/** * @return A random date & time with a random time offset, between approximately year 1970 and year 6429. The time * offset is between -12:00 and +12:00 and is rounded to a 15 minute division. *//*ww w. j a v a 2 s. c o m*/ public static DateTime randomDateTime() { final long timeMillis = (randomLong() & 0x0000_7fff_ffff_ffffL) + DAY_MILLIS; // prevent underflow final int offsetTotalMinutes = 15 * (randomInt(96) - 48); final DateTimeZone dateTimeZone = DateTimeZone.forOffsetMillis(1000 * 60 * offsetTotalMinutes); return new DateTime(timeMillis, dateTimeZone); }
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>. */// w w w . java 2 s .c o 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.moosemorals.weather.LocationFetcherIT.java
License:Open Source License
@Test public void basics() throws Exception { FetchResult result = new LocationFetcher.Builder().setApiKey(apiKey).setQuery("NE6").build().fetch(); assertNotNull(result, "Result should not be null"); requestsPerDay = result.getRequestsPerDay(); requestsPerSecond = result.getRequestsPerSecond(); assertEquals(result.getError(), null, "Error should be null"); assertEquals(result.getWeather(), null, "Weather should be null"); LocationReport report = result.getLocation(); assertNotNull(report, "Report should not be null"); assertEquals(report.getLocations().size(), 10); Location location = report.getLocations().get(0); assertEquals(location.getName(), "Walkergate"); assertEquals(location.getRegion(), "Tyne and Wear"); assertEquals(location.getCountry(), "United Kingdom"); assertEquals(location.getPopulation(), 0); assertEquals(location.getLatitude(), 54.974, 0.1); assertEquals(location.getLongitude(), -1.572, 0.1); // This one changes with local time. Poop. DateTimeZone London = DateTimeZone .forOffsetMillis(DateTimeZone.forID("Europe/London").getOffset(new DateTime())); assertEquals(location.getTimezone(), London); }