Package org.joda.time

Examples of org.joda.time.DateTimeZone


    }

    public static TimezoneMap fromConsensusTimezoneMap(final TreeMap<String, TimeZone> consensusTimezoneMap) {
        TimezoneMap timezoneMap = new TimezoneMap();
        Long holdStartMillis=null;
        DateTimeZone holdDateTimezone=null;
        Long lastEndMillis = null;

        for (String date : consensusTimezoneMap.keySet()) {
            final TimeZone timeZone = consensusTimezoneMap.get(date);
            final DateTimeZone dateTimeZone = DateTimeZone.forTimeZone(timeZone);
            final DateTime thisStartDateTime = TimeUtils.dateFormatter.withZone(dateTimeZone).parseDateTime(date);
            final long thisStartMillis = thisStartDateTime.getMillis();

            // Check if we need to flush a prior segment
            if(holdStartMillis!=null && holdDateTimezone!=dateTimeZone) {
View Full Code Here


            else {
                timespentInTimezoneMap.put(span.getValue(), timeSpent);
            }
        }
        long maxTimespent = Long.MIN_VALUE;
        DateTimeZone mainTimezone = null;
        for (DateTimeZone dateTimeZone : timespentInTimezoneMap.keySet()) {
            if (timespentInTimezoneMap.get(dateTimeZone) > maxTimespent) {
                maxTimespent = timespentInTimezoneMap.get(dateTimeZone);
                mainTimezone = dateTimeZone;
            }
View Full Code Here

        long minStartMillis = utcStartMillis - DateTimeConstants.MILLIS_PER_DAY/2;
        long maxStartMillis = utcStartMillis + DateTimeConstants.MILLIS_PER_DAY/2;

        TimespanSegment<DateTimeZone> minTimespan = this.queryPoint(minStartMillis);
        TimespanSegment<DateTimeZone> maxTimespan = this.queryPoint(maxStartMillis);
        DateTimeZone realTz = null;
        DateTime realDateStart = null;

        // Check if they agree
        if(minTimespan==maxTimespan) {
            // Ok, they agree so we're good, just use the consensus timezone
View Full Code Here

        // We need the real timezone; The ARBITRARY time intervals used by SimpleTimeInterval don't know it.
        // We could either create a more general TimerInterval which knows the timezone info, or do a hack like this:
        //if(timeInterval.getTimeUnit()!= TimeUnit.ARBITRARY) {
        //    metadataService.getTimeZone(guestId, dateString);
        //}
        DateTimeZone dateTimeZone = DateTimeZone.forTimeZone(timeZone);

        // Compute the start and end of that date in milliseconds for comparing
        // and truncating start/end
        LocalDate facetLocalDate = LocalDate.parse(facet.date);
        long dateStart = facetLocalDate.toDateTimeAtStartOfDay(dateTimeZone).getMillis();
View Full Code Here

                    final String tzName = jsonRecord.getString("value");
                    final String startDateStr = jsonRecord.getString("startDate");
                    final String endDateStr = jsonRecord.optString("endDate");
                    DateTime startDate;
                    DateTime endDate;
                    DateTimeZone tz;

                    tz = DateTimeZone.forID(tzName);
                    startDate = tzmapFormatter.parseDateTime(startDateStr);
                    if(endDateStr.equals("")) {
                        // Last entry in table has no endDate, set it to be one day in the future
View Full Code Here

            return getTimeZone(runtime, tz.toString());
        }
    }
    
    public static DateTimeZone getTimeZone(Ruby runtime, String zone) {
        DateTimeZone cachedZone = runtime.getTimezoneCache().get(zone);

        if (cachedZone != null) return cachedZone;

        String originalZone = zone;
        if (NON_JVM_TZNAME.containsKey(zone.toUpperCase())) {
            zone = NON_JVM_TZNAME.get(zone.toUpperCase());
        }
        TimeZone tz = TimeZone.getTimeZone(zone);

        // Value of "TZ" property is of a bit different format,
        // which confuses the Java's TimeZone.getTimeZone(id) method,
        // and so, we need to convert it.

        Matcher tzMatcher = TZ_PATTERN.matcher(zone);
        if (tzMatcher.matches()) {                   
            String sign = tzMatcher.group(2);
            String hours = tzMatcher.group(3);
            String minutes = tzMatcher.group(4);
               
            // GMT+00:00 --> Etc/GMT, see "MRI behavior"
            // comment below.
            if (("00".equals(hours) || "0".equals(hours)) &&
                    (minutes == null || ":00".equals(minutes) || ":0".equals(minutes))) {
                zone = "Etc/GMT";
            } else {
                // Invert the sign, since TZ format and Java format
                // use opposite signs, sigh... Also, Java API requires
                // the sign to be always present, be it "+" or "-".
                sign = ("-".equals(sign)? "+" : "-");

                // Always use "GMT" since that's required by Java API.
                zone = "GMT" + sign + hours;

                if (minutes != null) {
                    zone += minutes;
                }
            }
           
            tz = TimeZone.getTimeZone(zone);
        } else {
            if (LONG_TZNAME.containsKey(zone)) tz.setID(LONG_TZNAME.get(zone.toUpperCase()));
        }

        // MRI behavior: With TZ equal to "GMT" or "UTC", Time.now
        // is *NOT* considered as a proper GMT/UTC time:
        //   ENV['TZ']="GMT"
        //   Time.now.gmt? ==> false
        //   ENV['TZ']="UTC"
        //   Time.now.utc? ==> false
        // Hence, we need to adjust for that.
        if ("GMT".equalsIgnoreCase(zone) || "UTC".equalsIgnoreCase(zone)) {
            zone = "Etc/" + zone;
            tz = TimeZone.getTimeZone(zone);
        }

        DateTimeZone dtz = DateTimeZone.forTimeZone(tz);
        runtime.getTimezoneCache().put(originalZone, dtz);
        return dtz;
    }
View Full Code Here

            // Java needs the sign inverted
            String sgn = "+".equals(sign) ? "-" : "+";
            zone = "GMT" + sgn + hours + minutes;
        }

        DateTimeZone dtz = getTimeZone(context.runtime, zone);
        return newTime(context.runtime, dt.withZone(dtz), nsec);
    }
View Full Code Here

    private static RubyTime createTime(IRubyObject recv, IRubyObject[] args, boolean gmt) {
        Ruby runtime = recv.getRuntime();
        int len = ARG_SIZE;
        Boolean isDst = null;

        DateTimeZone dtz;
        if (gmt) {
            dtz = DateTimeZone.UTC;
        } else if (args.length == 10 && args[9] instanceof RubyString) {
            dtz = getTimeZone(runtime, ((RubyString) args[9]).toString());
        } else {
            dtz = getLocalTimeZone(runtime);
        }
        if (args.length == 10) {
            if (args[8] instanceof RubyBoolean) {
                isDst = ((RubyBoolean) args[8]).isTrue();
            }
            args = new IRubyObject[] { args[5], args[4], args[3], args[2], args[1], args[0], runtime.getNil() };
        } else {
            // MRI accepts additional wday argument which appears to be ignored.
            len = args.length;

            if (len < ARG_SIZE) {
                IRubyObject[] newArgs = new IRubyObject[ARG_SIZE];
                System.arraycopy(args, 0, newArgs, 0, args.length);
                for (int i = len; i < ARG_SIZE; i++) {
                    newArgs[i] = runtime.getNil();
                }
                args = newArgs;
                len = ARG_SIZE;
            }
        }

        if (args[0] instanceof RubyString) {
            args[0] = RubyNumeric.str2inum(runtime, (RubyString) args[0], 10, false);
        }

        int year = (int) RubyNumeric.num2long(args[0]);
        int month = 1;

        if (len > 1) {
            if (!args[1].isNil()) {
                IRubyObject tmp = args[1].checkStringType();
                if (!tmp.isNil()) {
                    String monthString = tmp.toString().toLowerCase();
                    Integer monthInt = MONTHS_MAP.get(monthString);

                    if (monthInt != null) {
                        month = monthInt;
                    } else {
                        try {
                            month = Integer.parseInt(monthString);
                        } catch (NumberFormatException nfExcptn) {
                            throw runtime.newArgumentError("Argument out of range.");
                        }
                    }
                } else {
                    month = (int) RubyNumeric.num2long(args[1]);
                }
            }
            if (1 > month || month > 12) {
                throw runtime.newArgumentError("Argument out of range: for month: " + month);
            }
        }

        int[] int_args = { 1, 0, 0, 0, 0, 0 };

        for (int i = 0; int_args.length >= i + 2; i++) {
            if (!args[i + 2].isNil()) {
                if (!(args[i + 2] instanceof RubyNumeric)) {
                    args[i + 2] = args[i + 2].callMethod(
                            runtime.getCurrentContext(), "to_i");
                }

                long value = RubyNumeric.num2long(args[i + 2]);
                if (time_min[i] > value || value > time_max[i]) {
                    throw runtime.newArgumentError("argument out of range.");
                }
                int_args[i] = (int) value;
            }
        }

        if (!runtime.is1_9()) {
            if (0 <= year && year < 39) {
                year += 2000;
            } else if (69 <= year && year < 139) {
                year += 1900;
            }
        }

        DateTime dt;
        // set up with min values and then add to allow rolling over
        try {
            dt = new DateTime(year, 1, 1, 0, 0, 0, 0, DateTimeZone.UTC);

            dt = dt.plusMonths(month - 1)
                    .plusDays(int_args[0] - 1)
                    .plusHours(int_args[1])
                    .plusMinutes(int_args[2])
                    .plusSeconds(int_args[3]);

            // 1.9 will observe fractional seconds *if* not given usec
            if (runtime.is1_9() && !args[5].isNil()
                    && args[6].isNil()) {
                double millis = RubyFloat.num2dbl(args[5]);
                int int_millis = (int) (millis * 1000) % 1000;
                dt = dt.plusMillis(int_millis);
            }

            dt = dt.withZoneRetainFields(dtz);

            // we might need to perform a DST correction
            if (isDst != null) {
                // the instant at which we will ask dtz what the difference between DST and
                // standard time is
                long offsetCalculationInstant = dt.getMillis();

                // if we might be moving this time from !DST -> DST, the offset is assumed
                // to be the same as it was just before we last moved from DST -> !DST
                if (dtz.isStandardOffset(dt.getMillis())) {
                    offsetCalculationInstant = dtz.previousTransition(offsetCalculationInstant);
                }

                int offset = dtz.getStandardOffset(offsetCalculationInstant)
                        - dtz.getOffset(offsetCalculationInstant);

                if (!isDst && !dtz.isStandardOffset(dt.getMillis())) {
                    dt = dt.minusMillis(offset);
                }
                if (isDst && dtz.isStandardOffset(dt.getMillis())) {
                    dt = dt.plusMillis(offset);
                }
            }
        } catch (org.joda.time.IllegalFieldValueException e) {
            throw runtime.newArgumentError("time out of range");
View Full Code Here

  }

  @Test
  public void testGroupByWithTimeZone()
  {
    DateTimeZone tz = DateTimeZone.forID("America/Los_Angeles");

    GroupByQuery query = GroupByQuery.builder()
                                     .setDataSource(QueryRunnerTestHelper.dataSource)
                                     .setInterval("2011-03-31T00:00:00-07:00/2011-04-02T00:00:00-07:00")
                                     .setDimensions(
View Full Code Here

  }

  @Test
  public void testPeriodDaylightSaving() throws Exception
  {
    final DateTimeZone tz = DateTimeZone.forID("America/Los_Angeles");
    final DateTime baseTime = new DateTime("2012-11-04T00:00:00", tz);
    assertSame(
        Lists.newArrayList(
            new DateTime("2012-11-04T00:00:00.000-07:00"),
            new DateTime("2012-11-05T00:00:00.000-08:00"),
View Full Code Here

TOP

Related Classes of org.joda.time.DateTimeZone

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.