001    package biweekly.util;
002    
003    import java.util.regex.Matcher;
004    import java.util.regex.Pattern;
005    
006    /*
007     Copyright (c) 2013, Michael Angstadt
008     All rights reserved.
009    
010     Redistribution and use in source and binary forms, with or without
011     modification, are permitted provided that the following conditions are met: 
012    
013     1. Redistributions of source code must retain the above copyright notice, this
014     list of conditions and the following disclaimer. 
015     2. Redistributions in binary form must reproduce the above copyright notice,
016     this list of conditions and the following disclaimer in the documentation
017     and/or other materials provided with the distribution. 
018    
019     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023     ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029     */
030    
031    /**
032     * Represents a UTC offset.
033     * @author Michael Angstadt
034     */
035    public final class UtcOffset {
036            private final int hour;
037            private final int minute;
038    
039            /**
040             * Creates a new UTC offset.
041             * @param hour the hour component (may be negative)
042             * @param minute the minute component (must be between 0 and 59)
043             */
044            public UtcOffset(int hour, int minute) {
045                    this.hour = hour;
046                    this.minute = minute;
047            }
048    
049            /**
050             * Parses a UTC offset from a string.
051             * @param text the text to parse (e.g. "-0500")
052             * @return the parsed UTC offset
053             * @throws IllegalArgumentException if the text cannot be parsed
054             */
055            public static UtcOffset parse(String text) {
056                    Pattern timeZoneRegex = Pattern.compile("^([-\\+])?(\\d{1,2})(:?(\\d{2}))?$");
057                    Matcher m = timeZoneRegex.matcher(text);
058    
059                    if (!m.find()) {
060                            throw new IllegalArgumentException("Offset string is not in ISO8610 format: " + text);
061                    }
062    
063                    String sign = m.group(1);
064                    boolean positive;
065                    if ("-".equals(sign)) {
066                            positive = false;
067                    } else {
068                            positive = true;
069                    }
070    
071                    String hourStr = m.group(2);
072                    int hourOffset = Integer.parseInt(hourStr);
073                    if (!positive) {
074                            hourOffset *= -1;
075                    }
076    
077                    String minuteStr = m.group(4);
078                    int minuteOffset = (minuteStr == null) ? 0 : Integer.parseInt(minuteStr);
079    
080                    return new UtcOffset(hourOffset, minuteOffset);
081            }
082    
083            /**
084             * Gets the hour component.
085             * @return the hour component
086             */
087            public int getHour() {
088                    return hour;
089            }
090    
091            /**
092             * Gets the minute component.
093             * @return the minute component
094             */
095            public int getMinute() {
096                    return minute;
097            }
098    
099            /**
100             * Converts this offset to its ISO string representation using "basic"
101             * format.
102             * @return the ISO string representation (e.g. "-0500")
103             */
104            @Override
105            public String toString() {
106                    return toString(false);
107            }
108    
109            /**
110             * Converts this offset to its ISO string representation.
111             * @param extended true to use extended format (e.g. "-05:00"), false to use
112             * basic format (e.g. "-0500")
113             * @return the ISO string representation
114             */
115            public String toString(boolean extended) {
116                    StringBuilder sb = new StringBuilder();
117    
118                    boolean positive = hour >= 0;
119                    sb.append(positive ? '+' : '-');
120    
121                    int hour = Math.abs(this.hour);
122                    if (hour < 10) {
123                            sb.append('0');
124                    }
125                    sb.append(hour);
126    
127                    if (extended) {
128                            sb.append(':');
129                    }
130    
131                    if (minute < 10) {
132                            sb.append('0');
133                    }
134                    sb.append(minute);
135    
136                    return sb.toString();
137            }
138    
139            @Override
140            public int hashCode() {
141                    final int prime = 31;
142                    int result = 1;
143                    result = prime * result + hour;
144                    result = prime * result + minute;
145                    return result;
146            }
147    
148            @Override
149            public boolean equals(Object obj) {
150                    if (this == obj)
151                            return true;
152                    if (obj == null)
153                            return false;
154                    if (getClass() != obj.getClass())
155                            return false;
156                    UtcOffset other = (UtcOffset) obj;
157                    if (hour != other.hour)
158                            return false;
159                    if (minute != other.minute)
160                            return false;
161                    return true;
162            }
163    }