1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/net/fortuna/ical4j/model/UtcOffset.java Tue Feb 10 18:12:00 2015 +0100 1.3 @@ -0,0 +1,166 @@ 1.4 +/** 1.5 + * Copyright (c) 2012, Ben Fortuna 1.6 + * All rights reserved. 1.7 + * 1.8 + * Redistribution and use in source and binary forms, with or without 1.9 + * modification, are permitted provided that the following conditions 1.10 + * are met: 1.11 + * 1.12 + * o Redistributions of source code must retain the above copyright 1.13 + * notice, this list of conditions and the following disclaimer. 1.14 + * 1.15 + * o Redistributions in binary form must reproduce the above copyright 1.16 + * notice, this list of conditions and the following disclaimer in the 1.17 + * documentation and/or other materials provided with the distribution. 1.18 + * 1.19 + * o Neither the name of Ben Fortuna nor the names of any other contributors 1.20 + * may be used to endorse or promote products derived from this software 1.21 + * without specific prior written permission. 1.22 + * 1.23 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.24 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.25 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.26 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 1.27 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 1.28 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1.29 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1.30 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 1.31 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 1.32 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 1.33 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.34 + */ 1.35 +package net.fortuna.ical4j.model; 1.36 + 1.37 +import java.io.IOException; 1.38 +import java.io.Serializable; 1.39 +import java.text.DecimalFormat; 1.40 +import java.text.NumberFormat; 1.41 + 1.42 +import net.fortuna.ical4j.util.Dates; 1.43 + 1.44 +import org.apache.commons.lang.builder.HashCodeBuilder; 1.45 +import org.apache.commons.logging.Log; 1.46 +import org.apache.commons.logging.LogFactory; 1.47 + 1.48 +/** 1.49 + * $Id$ 1.50 + * 1.51 + * Created on 26/06/2005 1.52 + * 1.53 + * Represents a timezone offset from UTC time. 1.54 + * 1.55 + * @author Ben Fortuna 1.56 + */ 1.57 +public class UtcOffset implements Serializable { 1.58 + 1.59 + private static final long serialVersionUID = 5883111996721531728L; 1.60 + 1.61 + private static final int HOUR_START_INDEX = 1; 1.62 + 1.63 + private static final int HOUR_END_INDEX = 3; 1.64 + 1.65 + private static final int MINUTE_START_INDEX = 3; 1.66 + 1.67 + private static final int MINUTE_END_INDEX = 5; 1.68 + 1.69 + private static final int SECOND_START_INDEX = 5; 1.70 + 1.71 + private static final int SECOND_END_INDEX = 7; 1.72 + 1.73 + private static final NumberFormat HOUR_FORMAT = new DecimalFormat("00"); 1.74 + 1.75 + private static final NumberFormat MINUTE_FORMAT = new DecimalFormat("00"); 1.76 + 1.77 + private static final NumberFormat SECOND_FORMAT = new DecimalFormat("00"); 1.78 + 1.79 + private long offset; 1.80 + 1.81 + /** 1.82 + * @param value a string representation of an offset 1.83 + */ 1.84 + public UtcOffset(final String value) { 1.85 + 1.86 + if (value.length() < MINUTE_END_INDEX) { 1.87 + throw new IllegalArgumentException("Invalid UTC offset [" + value 1.88 + + "] - must be of the form: (+/-)HHMM[SS]"); 1.89 + } 1.90 + 1.91 + final boolean negative = value.charAt(0) == '-'; 1.92 + 1.93 + if (!negative && !(value.charAt(0) == '+')) { 1.94 + throw new IllegalArgumentException("UTC offset value must be signed"); 1.95 + } 1.96 + 1.97 + offset = 0; 1.98 + offset += Integer.parseInt(value.substring(HOUR_START_INDEX, 1.99 + HOUR_END_INDEX)) 1.100 + * Dates.MILLIS_PER_HOUR; 1.101 + offset += Integer.parseInt(value.substring(MINUTE_START_INDEX, 1.102 + MINUTE_END_INDEX)) 1.103 + * Dates.MILLIS_PER_MINUTE; 1.104 + if (value.length() == SECOND_END_INDEX) { 1.105 + offset += Integer.parseInt(value.substring(SECOND_START_INDEX, 1.106 + SECOND_END_INDEX)) 1.107 + * Dates.MILLIS_PER_SECOND; 1.108 + } 1.109 + if (negative) { 1.110 + offset = -offset; 1.111 + } 1.112 + } 1.113 + 1.114 + /** 1.115 + * @param offset an offset value in milliseconds 1.116 + */ 1.117 + public UtcOffset(final long offset) { 1.118 + this.offset = (long) Math.floor(offset / (double) Dates.MILLIS_PER_SECOND) * Dates.MILLIS_PER_SECOND; 1.119 + } 1.120 + 1.121 + /** 1.122 + * {@inheritDoc} 1.123 + */ 1.124 + public final String toString() { 1.125 + final StringBuffer b = new StringBuffer(); 1.126 + long remainder = Math.abs(offset); 1.127 + 1.128 + if (offset < 0) { 1.129 + b.append('-'); 1.130 + } 1.131 + else { 1.132 + b.append('+'); 1.133 + } 1.134 + b.append(HOUR_FORMAT.format(remainder / Dates.MILLIS_PER_HOUR)); 1.135 + 1.136 + remainder = remainder % Dates.MILLIS_PER_HOUR; 1.137 + b.append(MINUTE_FORMAT.format(remainder / Dates.MILLIS_PER_MINUTE)); 1.138 + 1.139 + remainder = remainder % Dates.MILLIS_PER_MINUTE; 1.140 + if (remainder > 0) { 1.141 + b.append(SECOND_FORMAT.format(remainder / Dates.MILLIS_PER_SECOND)); 1.142 + } 1.143 + return b.toString(); 1.144 + } 1.145 + 1.146 + /** 1.147 + * @return Returns the offset. 1.148 + */ 1.149 + public final long getOffset() { 1.150 + return offset; 1.151 + } 1.152 + 1.153 + /** 1.154 + * {@inheritDoc} 1.155 + */ 1.156 + public final boolean equals(final Object arg0) { 1.157 + if (arg0 instanceof UtcOffset) { 1.158 + return getOffset() == ((UtcOffset) arg0).getOffset(); 1.159 + } 1.160 + return super.equals(arg0); 1.161 + } 1.162 + 1.163 + /** 1.164 + * {@inheritDoc} 1.165 + */ 1.166 + public final int hashCode() { 1.167 + return new HashCodeBuilder().append(getOffset()).toHashCode(); 1.168 + } 1.169 +}