GmtTimeZone.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.lang3.time;

  18. import java.util.Date;
  19. import java.util.Objects;
  20. import java.util.TimeZone;

  21. /**
  22.  * Custom time zone that contains offset from GMT.
  23.  *
  24.  * @since 3.7
  25.  */
  26. final class GmtTimeZone extends TimeZone {

  27.     private static final int MILLISECONDS_PER_MINUTE = 60 * 1000;
  28.     private static final int MINUTES_PER_HOUR = 60;
  29.     private static final int HOURS_PER_DAY = 24;

  30.     // Serializable!
  31.     static final long serialVersionUID = 1L;

  32.     private static StringBuilder twoDigits(final StringBuilder sb, final int n) {
  33.         return sb.append((char) ('0' + n / 10)).append((char) ('0' + n % 10));
  34.     }
  35.     private final int offset;

  36.     private final String zoneId;

  37.     GmtTimeZone(final boolean negate, final int hours, final int minutes) {
  38.         if (hours >= HOURS_PER_DAY) {
  39.             throw new IllegalArgumentException(hours + " hours out of range");
  40.         }
  41.         if (minutes >= MINUTES_PER_HOUR) {
  42.             throw new IllegalArgumentException(minutes + " minutes out of range");
  43.         }
  44.         final int milliseconds = (minutes + hours * MINUTES_PER_HOUR) * MILLISECONDS_PER_MINUTE;
  45.         offset = negate ? -milliseconds : milliseconds;
  46.         // @formatter:off
  47.         zoneId = twoDigits(twoDigits(new StringBuilder(9)
  48.             .append(TimeZones.GMT_ID)
  49.             .append(negate ? '-' : '+'), hours)
  50.             .append(':'), minutes)
  51.             .toString();
  52.         // @formatter:on
  53.     }

  54.     @Override
  55.     public boolean equals(final Object obj) {
  56.         if (this == obj) {
  57.             return true;
  58.         }
  59.         if (!(obj instanceof GmtTimeZone)) {
  60.             return false;
  61.         }
  62.         final GmtTimeZone other = (GmtTimeZone) obj;
  63.         return offset == other.offset && Objects.equals(zoneId, other.zoneId);
  64.     }

  65.     @Override
  66.     public String getID() {
  67.         return zoneId;
  68.     }

  69.     @Override
  70.     public int getOffset(final int era, final int year, final int month, final int day, final int dayOfWeek, final int milliseconds) {
  71.         return offset;
  72.     }

  73.     @Override
  74.     public int getRawOffset() {
  75.         return offset;
  76.     }

  77.     @Override
  78.     public int hashCode() {
  79.         return Objects.hash(offset, zoneId);
  80.     }

  81.     @Override
  82.     public boolean inDaylightTime(final Date date) {
  83.         return false;
  84.     }

  85.     @Override
  86.     public void setRawOffset(final int offsetMillis) {
  87.         throw new UnsupportedOperationException();
  88.     }

  89.     @Override
  90.     public String toString() {
  91.         return "[GmtTimeZone id=\"" + zoneId + "\",offset=" + offset + ']';
  92.     }

  93.     @Override
  94.     public boolean useDaylightTime() {
  95.         return false;
  96.     }
  97. }